/* auteur: Nadim Saikali */
/* Date de création: 15/12/2009 */
var minigallery = {
	pool : [], nbImage : 0,
	gallery : null,
	oParent : null,
	elementW : 0, actualPos : 0, parentW : 0, galleryW : 0, goingTo : 0, timer: null, pos: 0, num : 0,
	fleches : {},
	set : function(divId, nombreDimages){
		this.num = 0;
		this.nbImage = nombreDimages;
		this.gallery = document.getElementById(divId);
		this.fleches = { 'previous' : document.getElementById("previous"), 'next' : document.getElementById("next") };

		this.pool = this.gallery.getElementsByTagName('a');
		if (this.pool.length>0) {
			var elementMargin = parseFloat((window.ActiveXObject)? this.pool[0].currentStyle.marginLeft : getComputedStyle(this.pool[0], null).marginLeft);
			this.elementW = this.pool[0].offsetWidth+elementMargin;

			for(var i=0;i<this.pool.length; i++){//positionnement <a> et construction
				this.pool[i].style.left = ( i<1 ? 0 : (parseFloat(this.pool[i-1].style.left)+this.elementW ) ) +"px";
				this.constructBloc(this.pool[i]);
			}

			this.oParent = this.gallery.parentNode;
			this.parentW = this.oParent.offsetWidth;
			this.galleryW = this.pool.length*this.elementW;

			this.fleches["previous"].style.cursor="pointer";
			this.fleches["previous"].onclick = function(){ minigallery.prev(); }
			this.fleches["next"].style.cursor="pointer";
			this.fleches["next"].onclick = function(){ minigallery.next(); }

			this.flechesDisplay("previous", "left_off.png");
			if (this.pool.length <= this.nbImage) {
				this.flechesDisplay("next", "right_off.png");
			}

/*			if (window.addEventListener) {
				this.gallery.addEventListener('DOMMouseScroll', weel, false);//moz
				this.gallery.addEventListener('mousewheel', weel, false);//safari
			} else {// IE
				this.gallery.onmousewheel = function () {
					if (event.wheelDelta>0) minigallery.prev();
					else minigallery.next();
				}
			}*/
		}
		function weel(e) {
			var raw = e.detail ? e.detail : e.wheelDelta* -1;
			if (raw>0) minigallery.next();
			else minigallery.prev();
		}
	},
	next : function() {
		if ( (Math.abs(this.num) + this.nbImage) <this.pool.length) {
			this.flechesDisplay("previous", "left.png");
//alert((Math.abs(this.num) + this.nbImage) +" < "+this.pool.length);
			this.num--;
			if ( (Math.abs(this.num) + this.nbImage) == this.pool.length) {
				this.flechesDisplay("next", "right_off.png");
			}

			this.actualPos = minigallery.getActualPos();
			this.pos = 1;
			this.goingTo = this.num*this.elementW;
			if (this.timer==null) this.timer = setInterval('minigallery.slow()', 30);
		}
	},
	prev : function() {
		if ( this.num<0) {
			this.flechesDisplay("next", "right.png");
			this.num++;
			if ( this.num == 0) {
				this.flechesDisplay("previous", "left_off.png");
			}

			this.actualPos = minigallery.getActualPos();
			this.pos = -1;
			this.goingTo = this.num*this.elementW;
			if (this.timer==null) this.timer = setInterval('minigallery.slow()', 30);
		}
	},
	slow : function(){
		this.actualPos = minigallery.getActualPos();
		var dx = ((this.pos*this.goingTo) - (this.pos*this.actualPos));
		var doMath = this.actualPos+((this.pos)*(0.2*dx));
		doMath = ((this.pos)>0 ? Math.floor(doMath) : Math.ceil(doMath));

		if ((this.pos*this.actualPos)>(this.pos*this.goingTo)) {
			this.gallery.style.left = doMath+"px";
		} else {
			clearInterval(this.timer);
			this.timer = null;
		}
	},
	flechesDisplay : function(o,set){
		if (this.fleches[o].src.indexOf(set) <0 ) {
			this.fleches[o].src = "_img/"+set;
		}
	},
	getActualPos : function (){
		return (parseFloat((window.ActiveXObject)? this.gallery.currentStyle.left : getComputedStyle(this.gallery, null).left));
	},
	constructBloc : function(o){
		var fog = document.createElement('span');
		fog.className  = 'fog';
		o.appendChild(fog);

		var txt = document.createElement('span');
		txt.className  = 'txt';
		txt.innerHTML = o.title;
		o.appendChild(txt);
		
	}
}