/**
 * RSPlug Slider
 * @author Roland Dufour <roland.dufour@multiprog.net> pour Rentashop
 **/

var RSPlug_Slider = function (clipObj, subClipObj, options){
	this.options = { 
		scrollStep: 3, 
		widthFrame: 100, 
		spacingFrame: 0, 
		nbFrames: 3, 
		nbFramesShowed: null, 
		moveDelay: 1000, 
		enableLoop: false, 
		prevButton: null, 
		nextButton: null, 
		prevNoButton: null, 
		nextNoButton: null
	}; 
	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.spacingFrame && !isNaN(options.spacingFrame) && options.spacingFrame > 0){
		this.options.spacingFrame = options.spacingFrame;
	}
	if (options.nbFrames && !isNaN(options.nbFrames) && options.nbFrames > 0){
		this.options.nbFrames = options.nbFrames;
	}
	if (options.nbFramesShowed && !isNaN(options.nbFramesShowed) && options.nbFramesShowed > 0){
		this.options.nbFramesShowed = options.nbFramesShowed;
	} else {
		this.options.nbFramesShowed = this.options.scrollStep;
	}
	if (options.moveDelay && !isNaN(options.moveDelay) && options.moveDelay > 0){
		this.options.moveDelay = options.moveDelay;
	}
	if (null !== options.enableLoop){
		this.options.enableLoop = (options.enableLoop ? true : false);
	}
	this.noButton = false;
	if (options.prevNoButton && options.nextNoButton){
		this.noButton = true;
		this.options.prevNoButton = options.prevNoButton;
		this.options.nextNoButton = options.nextNoButton;
	}
	if (options.prevButton && options.nextButton){
		this.options.prevButton = options.prevButton;
		this.options.nextButton = options.nextButton;
		this.registerButtons();
	}
	
	this.index = 0;
	this.scrolling = false;
	this.clip = clipObj;
	this.clip.style.overflow = 'hidden';
	this.clip.style.width = parseInt(this.options.widthFrame * this.options.nbFramesShowed + this.options.spacingFrame * (this.options.nbFramesShowed -1)) + 'px';
	this.subClipObj = subClipObj;
	this.subClipObj.style.left = 0;
	
	this.updateButtons();
};


/***************
 *   BOUTONS   *
 ***************/

RSPlug_Slider.prototype.registerButtons = function(){
	var inst = this;
	this.options.prevButton.onclick = function(){
		inst.previous();
		inst.updateButtons();
		return false;
	};
	this.options.nextButton.onclick = function(){
		inst.next();
		inst.updateButtons();
		return false;
	};
};

RSPlug_Slider.prototype.updateButtons = function(){
	if (this.noButton && !this.options.enableLoop){
		var cp = this.canPrevious();
		this.options.prevButton.style.display = (cp ? '' : 'none');
		this.options.prevNoButton.style.display = (cp ? 'none' : '');
		var cn = this.canNext();
		this.options.nextButton.style.display = (cn ? '' : 'none');
		this.options.nextNoButton.style.display = (cn ? 'none' : '');
	}
};


/************
 *   NEXT   *
 ************/

RSPlug_Slider.prototype.getNext = function(){
	if (this.options.nbFrames <= this.options.scrollStep){
		return 0;
	}
	var newIndex = this.index + this.options.scrollStep;
	if (newIndex > (this.options.nbFrames - this.options.nbFramesShowed)){
		newIndex = this.options.nbFrames - this.options.nbFramesShowed;
	}
	if (this.index == newIndex && this.options.enableLoop){
		return 0;
	}
	return newIndex;
};
RSPlug_Slider.prototype.canNext = function(){ return (this.getNext() != this.index); };
RSPlug_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   *
 ****************/

RSPlug_Slider.prototype.getPrevious = function(){
	var newIndex = this.index - this.options.scrollStep;
	if (newIndex < 0){
		newIndex = 0;
	}
	if (this.index == 0 && this.options.enableLoop){
		if (this.options.nbFrames > this.options.nbFramesShowed){
			return (this.options.nbFrames - this.options.nbFramesShowed);
		}
	}
	return newIndex;
};
RSPlug_Slider.prototype.canPrevious = function(){ return (this.getPrevious() != this.index); };
RSPlug_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   *
 *******************/

RSPlug_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 + new_index * this.options.spacingFrame * -1);

	var nb_frame = (this.index < new_index) ? (new_index - this.index) : (this.index - new_index);
	var delay = this.options.moveDelay * nb_frame;		// NB de ms pour faire le décalage
	
	this.index = new_index;
	var inst = this;

	if (delay == 0){
		this.subClipObj.style.left = end + 'px';
		this.scrolling = false;
	}
	else {
		if (start < end){	// De gauche à droite
			var ecart = (end - start) / 100;
			
			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;
			
			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) + ');');
			}
		}
	}
};
