/**
 * RS Slider
 * @author Roland Dufour <roland.dufour@multiprog.net> pour Rentashop
 **/

var RS_Slider = function (clipObj, subClipObj, options){
	this.options = { scrollStep: 3, widthFrame: 100, nbFrames: 3, moveDelay: 1000 }; 
	if (options.scrollStep && !isNaN(options.scrollStep) && options.scrollStep > 0){
		this.options.scrollStep = options.scrollStep;
	}
	if (options.widthFrame && !isNaN(options.widthFrame) && options.widthFrame > 0){
		this.options.widthFrame = options.widthFrame;
	}
	if (options.nbFrames && !isNaN(options.nbFrames) && options.nbFrames > 0){
		this.options.nbFrames = options.nbFrames;
	}
	if (options.moveDelay && !isNaN(options.moveDelay) && options.moveDelay > 0){
		this.options.moveDelay = options.moveDelay;
	}
	
	this.index = 0;
	this.scrolling = false;
	this.clip = clipObj;
	this.clip.style.overflow = 'hidden';
	this.clip.style.width = parseInt(this.options.widthFrame * this.options.scrollStep);
	this.subClipObj = subClipObj;
	this.subClipObj.style.left = 0;
};


/************
 *   NEXT   *
 ************/

RS_Slider.prototype.getNext = function(){
	var newIndex = this.index + this.options.scrollStep;

	if (this.options.nbFrames <= this.options.scrollStep){
		return 0;
	}
	if (newIndex > (this.options.nbFrames - this.options.scrollStep)){
		newIndex = this.options.nbFrames - this.options.scrollStep;
	}
	return newIndex;
};
RS_Slider.prototype.canNext = function(){ return (this.getNext() != this.index); };
RS_Slider.prototype.next = function (){
	if (this.scrolling){ return; }	// On ne fait rien si on est en train de scroller
	if (this.canNext()){ this.moveTo (this.getNext()); }
};


/****************
 *   PREVIOUS   *
 ****************/

RS_Slider.prototype.getPrevious = function(){
	var newIndex = this.index - this.options.scrollStep;
	if (newIndex < 0){
		newIndex = 0;
	}
	return newIndex;
};
RS_Slider.prototype.canPrevious = function(){ return (this.getPrevious() != this.index); };
RS_Slider.prototype.previous = function (){
	if (this.scrolling){ return; }	// On ne fait rien si on est en train de scroller
	if (this.canPrevious()){ this.moveTo (this.getPrevious()); }
};


/*******************
 *   DEPLACEMENT   *
 *******************/

RS_Slider.prototype.moveTo = function (new_index){
	if (this.scrolling){ return; }
	
	this.scrolling = true;
	
	var start = parseInt(this.subClipObj.style.left);
	var end = parseInt(new_index * this.options.widthFrame * -1);

	
	if (this.index < new_index){
		var nb_frame = new_index - this.index;
	} else {
		var nb_frame = this.index - new_index;
	}
	
	var delay = this.options.moveDelay * nb_frame;
	
	
	if (start < end){	// De gauche à droite
		var ecart = (end - start) / 100;
		var inst = this;
		
		this.index = new_index;
		if (delay == 0){
			this.subClipObj.style.left = end + 'px';
			this.scrolling = false;
		} else {
			for (var i=0; i<100; i++){
				eval('setTimeout(function(){ inst.subClipObj.style.left = ' + parseInt(start + (ecart * (i +1))) + ' + \'px\';' + (i == 99 ? ' inst.scrolling = false; ' : '') + '}, ' + Math.ceil(delay / 100 * i) + ');');
			}
		}
	}
	else {	// De droite à gauche
		var ecart = (start - end) / 100;
		var inst = this;
		
		this.index = new_index;
		if (delay == 0){
			this.subClipObj.style.left = end + 'px';
			this.scrolling = false;
		} else {
			for (var i=0; i<100; i++){
				eval('setTimeout(function(){ inst.subClipObj.style.left = ' + parseInt(start - (ecart * (i +1))) + ' + \'px\';' + (i == 99 ? ' inst.scrolling = false; ' : '') + '}, ' + Math.ceil(delay / 100 * i) + ');');
			}
		}
	}
};
