ï»¿/*global $ , document, setInterval, clearInterval */

/**
	-------------------------------------------------------------------------------------------------------
	Carrousel
	
	p_list = liste (ul) des Ã©lÃ©ments
	p_btNext = bouton pour passer l'Ã©lÃ©ment suivant
	p_btPrev = bouton pour passer l'Ã©lÃ©ment prÃ©cÃ©dent
	p_topVisiblePart = top de la partie visible
	p_bottomVisiblePart = bottom  de la partie visible
	p_moveDuration = durée du mouvement (en ms)
	p_autoRollDelay = delay entre 2 roll automatique (si null ou 0, pas de roll auto) en seconde
 */
 var Carrousel = function (p_list, p_btNext, p_btPrev, p_topVisiblePart, p_bottomVisiblePart, p_moveDuration, p_autoRollDelay) {
 
	if($(p_list) && $(p_btNext) && $(p_btPrev) && $(p_list + ' li').size() > 0)
	{
		this.currentItem = 0;
		this.nextItem = 0;
		this.prevItem = 0;
		this.btPrev = $(p_btPrev);
		this.btNext = $(p_btNext);
		this.list = $(p_list);
		this.listItem = $(p_list + ' li');
		this.nbItem = this.listItem.size();
		this.topDisplay = p_topVisiblePart + "px";
		this.bottom = p_bottomVisiblePart + "px";
		this.topHidden = (p_topVisiblePart - (p_bottomVisiblePart - p_topVisiblePart)) + "px";
		this.moveDuration = p_moveDuration;
		this.isMoving = false;
		if (p_autoRollDelay) {
			this.autoRollDelay = parseInt(p_autoRollDelay, 0) * 1000;
			this.autoRoll = (this.autoRollDelay > 0);
		}
		else {
			this.autoRoll = false;
		}
		this.autoRollIntervalId = -1;
	
		// retourne l'index de suivant
		this.getNextItem = function () {
			return (this.currentItem + 1 < this.nbItem ? this.currentItem + 1 : 0);
		};
		
		// retourne l'index de précédent
		this.getPrevItem = function () {
			return (this.currentItem - 1 >= 0 ? this.currentItem - 1 : this.nbItem - 1);
		};
		
		// Affiche l'Ã©lÃ©ment suivant
		this.moveToNextItem = function (p_evt) {
			// on rÃ©cupÃ¨re l'objet Carrousel
			var parent = p_evt.data.parentObject;
			// Si une animation n'est pas dÃ©jÃ  lancÃ©e
			if (!parent.isMoving) {
				// On stoppe le roll auto pour pouvoir faire repartir le compte Ã  rebour au dÃ©but.
				if(parent.autoRollIntervalId !== -1) {
					clearInterval (parent.autoRollIntervalId);
				}
				parent.isMoving = true;
				parent.nextItem = parent.getNextItem ();
				$(parent.listItem.get(parent.currentItem)).css("top", parent.topDisplay).animate({top : parent.bottom}, parent.moveDuration, "linear", function (){
					parent.currentItem = parent.nextItem; 
					parent.isMoving = false;
					// On relance le le mode auto
					if (parent.autoRollDelay > 0) {
						parent.autoRollIntervalId = setInterval(parent.autoNextNews, parent.autoRollDelay);
					}
				});
				$(parent.listItem.get(parent.nextItem)).css("top", parent.topHidden).animate({top : parent.topDisplay}, parent.moveDuration, "linear");
			}
		};
		
		 // Affiche l'Ã©lÃ©ment prÃ©cÃ©dent
		 this.moveToPreviousItem = function (p_evt) {
			var parent = p_evt.data.parentObject;
			if (!parent.isMoving) {
				if(parent.autoRollIntervalId !== -1) {
					clearInterval (parent.autoRollIntervalId);
				}
				parent.isMoving = true;
				parent.prevItem = parent.getPrevItem ();
				$(parent.listItem.get(parent.currentItem)).css("top", parent.topDisplay).animate({top : parent.topHidden}, parent.moveDuration, "linear",  function (){ 
					parent.currentItem = parent.prevItem;
					parent.isMoving = false;
					if (parent.autoRollDelay > 0) {
						parent.autoRollIntervalId = setInterval(parent.autoNextNews, parent.autoRollDelay);
					}
				});
				$(parent.listItem.get(parent.prevItem)).css("top", parent.bottom).animate({top : parent.topDisplay}, parent.moveDuration, "linear");
			}
		 };
		 
		 // Affiche l'Ã©lÃ©ment suivant (utilisÃ© pour le mode auto uniquement
		 this.autoNextNews = function () {
			$(p_btNext).trigger('click');
		 };
		 
		 // On lie les boutons au fonctions
		this.btPrev.bind ('click', {parentObject : this}, this.moveToPreviousItem);
		this.btNext.bind ('click', {parentObject : this}, this.moveToNextItem);
		
		// on initialise la position des Ã©lÃ©ments (li)
		for (var iFor = 0; iFor < this.nbItem; iFor++) {
			if (iFor === 0) {
				$(this.listItem.get(iFor)).css("top", this.topDisplay);
				this.currentItem = 0;
			}
			else {
				$(this.listItem.get(iFor)).css("top", this.bottom);
			}
		}
		
		// On lance le dÃ©roulement automatique (si voulu)
		if (this.autoRoll) {
			this.autoRollIntervalId = setInterval(this.autoNextNews, this.autoRollDelay);
		}
	}

};