/*
 *	Class horizontalSlider
 *  constructeur
 *	@param string id de l'élément qui scroll
 *	@param number largeur du conteneur (masque)
 *	@param number hauteur du conteneur (masque) (optionnel)
 */
function horizontalSlider( mover_id, width, height, speed){
	this.moverElement = document.getElementById(mover_id);

	this.width = width;
	this.height = height;
	
	
	this.moverwidth = this.moverElement.offsetWidth;
	this.minx = 0;
	this.maxx = this.moverwidth-this.width;
	
	this.interval = 10;
	this.timer = null;

	this.stepBase = 1;
	// step de départ (utile en cas de déclenchement automatique)
	this.step = -1*this.stepBase;	
	
	this.to = null;
	this.from = null;
	this.startTime = null;	
	if (speed == undefined) speed = 15;
	this.speed = speed;
}
/*
 * ini(mover_id, width, height, speed); 
 * @param number direction (-1 = left, 1 = right) 
 */
horizontalSlider.prototype.ini = function(mover_id, width, height, speed){
	
	this.moverElement = document.getElementById(mover_id);


	this.width = width;
	this.height = height;
	
	
	this.moverwidth = this.moverElement.offsetWidth;
	this.minx = 0;
	this.maxx = this.moverwidth-this.width;
	
	this.interval = 10;
	this.timer = null;

	this.stepBase = 1;
	// step de départ (utile en cas de déclenchement automatique)
	this.step = -1*this.stepBase;	
	
	this.to = null;
	this.from = null;
	this.startTime = null;	
	if (speed == undefined) speed = 15;
	this.speed = speed;
	
}

/*
 * move(dir); 
 * @param number direction (-1 = left, 1 = right) 
 */
horizontalSlider.prototype.move = function(direction){
	this.step = direction*this.stepBase;
	this.start();
}
/*
 * start(); 
 * déclenche le slider
 * @param number direction (-1 = left, 1 = right) 
 */
horizontalSlider.prototype.start = function(){
	var pslider = this;
	this.timer = setInterval(function(){pslider.run();},this.interval);
}
/*
 * stop(); 
 * arrête le slider
 */
horizontalSlider.prototype.stop = function(){
	clearInterval (this.timer);
}
/*
 * moveto(); 
 * effectue un scroll d'une position de départ vers une position d'arrivée;
 * @param string position début ou fin (value = 'end', 'start')  
 * @param number duration en millisecondes 
 */
horizontalSlider.prototype.moveto = function(direction){
	this.stop();
	
	this.moverwidth = this.moverElement.offsetWidth;
	this.minx = 0;
	this.maxx = this.moverwidth-this.width;
		
	var dest;
	if (direction == 'start'){
		dest = this.minx;
	} else if (direction == 'end'){
		dest = -this.maxx;
	}	else {
		dest = this.getLimitPosition(-direction+this.width/2);
	}

	if (dest == null){
		setTimeout( function(){ this.moveto(direction);}.bind(this), 10 );
	} else {

		this.to = dest;

		this.from = parseInt(this.moverElement.style.left);
		this.startTime = (new Date).getTime();	
	
		this.duration = (Math.abs(this.to-this.from)*this.speed);
	
		pslider = this;
		this.timer = setInterval(function(){pslider.scrollto();},this.interval);		
	}

}
horizontalSlider.prototype.scrollto = function(){	
	
	var time  = (new Date).getTime();
	
	if (time >= this.duration+this.startTime){
		this.stop();
	}

	var Timepos = (time - this.startTime) / (this.duration);
	pos = this.sinoidal( Timepos) * (this.to-this.from) + this.from;
	this.setPosition( pos );
}


/*
 * run(); 
 * incrémente la position, fonction appelée par start();
 */
horizontalSlider.prototype.run = function(){
	var pos = parseInt(this.moverElement.style.left) + this.step;
	this.setPosition( pos );
}





/*
 * setPosition(pos); 
 * positionne le slider à une position donnée
 * @param number position en pixel du slider 
 */
horizontalSlider.prototype.setPosition = function(pos){
	if (this.isUnderLimit(pos)){	
		this.moverElement.style.left = pos  + 'px';
	} else {
		this.moverElement.style.left = this.getLimitPosition(pos)  + 'px';
		this.stop();
	}
}
/*
 * getLimitPosition(pos); 
 * @param number position en pixel du slider 
 * @return number retourne la position comprise dans la limite de son conteneur
 */
horizontalSlider.prototype.getLimitPosition=function(pos){
	
	if( pos*-1 > this.maxx){
		return -this.maxx;
	}
	if( pos*-1 < this.minx){
		return this.minx;
	}	
	return pos;
}
/*
 * isUnderLimit(pos); 
 * @param number position en pixel du slider 
 * @return boolean retourne vrai ou faut selon que le slider est ou non dans les limites du conteneur
 */
horizontalSlider.prototype.isUnderLimit=function(pos){
	
	if( pos*-1 > this.maxx){
		return false;
	}
	if( pos*-1 < this.minx){
		return false;
	}	
	return true;
}

//transitions
horizontalSlider.prototype.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}
horizontalSlider.prototype.linear = function(pos){
	return pos;
}
horizontalSlider.prototype.cubic = function(pos){
	return Math.pow(pos, 3);
}
horizontalSlider.prototype.circ = function(pos){
	return Math.sqrt(pos);
}