
var minigallery = {
	pool : [], nbImage : 0, imgDirPath: null,
	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, imgDir){
		this.num = 0;
		this.nbImage = nombreDimages;
		if (document.getElementById(divId)==undefined) return;
		this.imgDirPath = imgDir;
		this.gallery = document.getElementById(divId);
//this.gallery.parentNode.style.width = "319px";

		this.fleches = { 'previous' : document.getElementById("previous"), 'next' : document.getElementById("next") };
		this.pool = this.gallery.getElementsByTagName('div');

		if (this.pool.length>0) {

			var elementMargin = parseFloat((window.ActiveXObject)? this.pool[0].currentStyle.marginLeft : getComputedStyle(this.pool[0], null).marginLeft);
// NAD 25/08/2010
// calcul auto du nombre d'elements (remplace nombreDimages)
this.nbImage = Math.floor(parseFloat(this.gallery.parentNode.offsetWidth/(this.pool[0].offsetWidth+elementMargin)));
var newPad = ((this.gallery.parentNode.offsetWidth/this.nbImage)-this.pool[0].offsetWidth);
//\
			if (window.ActiveXObject){//IE
				// NAD 25/08/2010
				this.nbImage = Math.floor(parseFloat(parseFloat(this.gallery.parentNode.currentStyle.width)/(parseFloat(this.pool[0].currentStyle.width)+elementMargin)));//2 = border
				var newPad = ((parseFloat(this.gallery.parentNode.currentStyle.width)/this.nbImage)-parseFloat(this.pool[0].currentStyle.width));

				this.elementW = parseFloat(this.pool[0].currentStyle.width.split("px")[0])+newPad;

			}else{
				this.elementW = this.pool[0].offsetWidth+newPad;
			}

			for(var i=0;i<this.pool.length; i++){//positionnement <element>

				this.pool[i].style.marginLeft = Math.ceil(elementMargin/2)+'px';// NAD 25/08/2010

				this.pool[i].style.left = ( i<1 ? 0 : (parseFloat(this.pool[i-1].style.left)+this.elementW ) ) +"px";

// NAD 25/08/2010 mis en commentaire
/*				if (window.ActiveXObject){
					this.elementW = parseFloat(this.pool[i].currentStyle.width.split("px")[0])+newPad;	
				}else{
					this.elementW = this.pool[i].offsetWidth+newPad;
				}*/
					
			}

			this.oParent = this.gallery.parentNode;
			this.parentW = this.oParent.offsetWidth;
			this.galleryW = this.pool.length*this.elementW;

			this.fleches["previous"].onclick = function(){ minigallery.prev(); }
			this.fleches["next"].onclick = function(){ minigallery.next(); }

			this.flechesDisplay("previous", "gauche");//gauche_off
			if (this.pool.length <= this.nbImage) {
				this.flechesDisplay("next", "droite");//droite_off
			}

			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", "gauche");
			this.num--;
			if ( (Math.abs(this.num) + this.nbImage) == this.pool.length) {
				this.flechesDisplay("next", "droite");//droite_off
			}

			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", "droite");
			this.num++;
			if ( this.num == 0) {
				this.flechesDisplay("previous", "gauche");//gauche_off
			}

			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){
		var img = '<img src="img/'+this.imgDirPath+'/fleche_'+set+'.gif">';
		if (this.fleches[o].innerHTML!=img) {
			this.fleches[o].innerHTML = img;
		}
	},
	getActualPos : function (){
		return (parseFloat((window.ActiveXObject)? this.gallery.currentStyle.left : getComputedStyle(this.gallery, null).left));
	}
}

