/* auteur: Nadim Saikali */
/* Date de création: 25/07/2008 */

var sildebps = {
	pool : [], etapes : [],
	gallery : null, dot : null, timer: null, timerImg: null,
	elementW : 0, actualPos : 0, goingTo : 0, pos: 0, num : 0, maxEtape : 6,
	fleches : {}, timebeat : null,
	set : function(){
		this.num = 0;
		this.dot = document.getElementById('dot');
		this.gallery = document.getElementById('nav_bps');
		this.fleches = { 'left' : document.getElementById("goleft"), 'right' : document.getElementById("goright") };

		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++){
				this.pool[i].saveIvar = i;
				this.pool[i].onclick = function (){
					sildebps.setPosition(this.saveIvar);
				}
			}

			this.fleches["left"].onclick = function(){ sildebps.left();	}
			this.fleches["right"].onclick = function(){ sildebps.right(); }

			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) sildebps.left();
					else sildebps.right();
				}
			}
		}
		function weel(e) {
			var raw = e.detail ? e.detail : e.wheelDelta* -1;
			if (raw>0) sildebps.right();
			else sildebps.left();
		}
		this.etapes = document.getElementById('etapes').getElementsByTagName('div');
		
		this.play();
	},
	stop: function(){
		clearInterval(this.timebeat);
	},
	play : function(){
		this.timebeat = setInterval('sildebps.right(1)', 10000);
	},
	right : function(n) {
		if(n!=1) sildebps.stop();
		if (this.num < this.maxEtape-1) {
			this.num++;
			this.pos = -1;
			this.setPosition(this.num);
		} else {
			sildebps.stop();
		}
	},
	left : function(n) {
		if(n!=1) sildebps.stop();
		if (this.num>0) {
			this.num--;
			this.pos = 1;
			this.setPosition(this.num);
		}
	},
	setPosition : function (n){
		// si this.num!=n == clic sur <a>
		if (this.num>n) this.pos = 1;
		if (this.num<n) this.pos = -1;

		this.num = n;
		this.actualPos = sildebps.getActualPos();
		this.goingTo = this.num*this.elementW;
		if (this.timer==null) this.timer = setInterval('sildebps.slow()', 30);
	},
	slow : function(){
		this.actualPos = sildebps.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.dot.style.left = doMath+"px";
		} else {
			clearInterval(this.timer);
			this.etapes[this.num].style.display = 'block';
			this.setClass(this.num);
			this.timer = null;
		}
	},
	slowImg : function(){
		
	},
	setClass : function(n){
		for(var i=0;i<this.pool.length; i++){
			this.pool[i].className = '';
			this.etapes[i].style.display = 'none';
		}
		this.pool[n].className = 'on';
		this.etapes[n].style.display = '';
//		if (this.timerImg==null) this.timerImg = setInterval('slowImg.slow()', 30);
	},
	getActualPos : function (){
		return (parseFloat((window.ActiveXObject)? this.dot.currentStyle.left : getComputedStyle(this.dot, null).left));
	}
}
