/* 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, loopTimer: null,
	links: null,
	bullLink : [],
	bulLinkSize : 13,
	fleches : {},
	balise : 'a',
	autoLoop : 3000, autoVar: 2,
	set : function(divId, linksId, nombreDimages, autoplay){
		this.num = 0;
		this.nbImage = nombreDimages;
		this.gallery = document.getElementById(divId);
		this.links = document.getElementById(linksId);
		if(document.getElementById("previous")!=undefined && document.getElementById("next")!=undefined)
			this.fleches = { 'previous' : document.getElementById("previous"), 'next' : document.getElementById("next") };
		else 
			this.fleches = null;

		this.pool = this.gallery.getElementsByTagName(this.balise);

		if (this.pool.length>0) {
			var elementMargin = parseFloat((window.ActiveXObject)? this.pool[0].currentStyle.marginLeft : getComputedStyle(this.pool[0], null).marginLeft);

			if (!this.pool[0].firstChild.complete) {//premiere image chargee
				setTimeout( function(){
					minigallery.set(divId, linksId, nombreDimages );
				}
				, 100);
				return;
			}

			this.elementW = this.pool[0].offsetWidth+elementMargin;

			var LinkStartPos = Math.round((this.elementW/2) - ((this.pool.length*this.bulLinkSize )/2));

			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.pool[i].style.visibility = 'visible';
				var bulLink = this.constructBloc(this.links);
				bulLink.style.left = LinkStartPos+( i*this.bulLinkSize ) +"px";
			}

			this.oParent = this.gallery.parentNode;
			this.parentW = this.oParent.offsetWidth;
			this.galleryW = this.pool.length*this.elementW;

			if(this.fleches) {
				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", "off");
			if (this.pool.length <= this.nbImage) {
				this.flechesDisplay("next", "off");
			}
			this.bullDisplay(0, 'on');

			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) { clearInterval(minigallery.loopTimer); minigallery.prev(); }
					else { clearInterval(minigallery.loopTimer);minigallery.next(); }
				}
			}
		}
		function weel(e) {
			clearInterval(minigallery.loopTimer);
			var raw = e.detail ? e.detail : e.wheelDelta* -1;
			if (raw>0) minigallery.next();
			else minigallery.prev();
		}
		if (autoplay=='autoplay') {
			this.loopTimer = window.setInterval('minigallery.auto()', this.autoLoop);
		}
	},
	next : function() {
		if ( (Math.abs(this.num) + this.nbImage) <this.pool.length) {
			this.flechesDisplay("previous", "");

			this.bullDisplay(Math.abs(this.num), '');
			this.num--;
			if ( (Math.abs(this.num) + this.nbImage) == this.pool.length) {
				this.flechesDisplay("next", "off");
			}
			this.bullDisplay(Math.abs(this.num), 'on');

			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", "");
			this.bullDisplay(Math.abs(this.num), '');
			this.num++;
			if ( this.num == 0) {
				this.flechesDisplay("previous", "off");
			}
			this.bullDisplay(Math.abs(this.num), 'on');

			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)
			this.fleches[o].className = set;
	},
	bullDisplay : function(n, set){
		this.bullLink[n].className = set;
	},
	getActualPos : function (){
		return (parseFloat((window.ActiveXObject)? this.gallery.currentStyle.left : getComputedStyle(this.gallery, null).left));
	},
	constructBloc : function(o){
		var bul = document.createElement('a');
		bul.className  = '';
		o.appendChild(bul);
		this.bullLink.push(bul);
		bul.posI = this.bullLink.length;
		bul.onclick = function(){
			minigallery.getToFast(this.posI);
		}
		return bul;
	},
	getToFast : function(i){
		clearInterval(this.loopTimer);
		var n = (i-1);
		this.bullDisplay(Math.abs(this.num), '');
		if (n < Math.abs(this.num)) {
			this.num = (-1)*(n+1);
			minigallery.prev();
		} else {
			this.num = (-1)*(n-1);
			minigallery.next();
		}
	},
	auto : function(){
		var n = (this.autoVar-1);
		this.bullDisplay(Math.abs(this.num), '');
		if (n < Math.abs(this.num)) {
			this.autoVar=2;
			this.num = (-1)*(n+1);
			minigallery.prev();
		} else {
			if(this.autoVar>=this.pool.length) this.autoVar=1;
			else this.autoVar++;
			this.num = (-1)*(n-1);
			minigallery.next();
		}
	}
}
