/*
 * Utilisation de l'objet Carrousel:
 * 
 * Pour utiliser l'objet Carrousel il sufit de créer un nouvel objet de ce type,
 * de le déclarer grace à la fonction 'ajouterCarrousel'
 * et de démarrer le ou les carrousels de la page:
 * 
 * A placer en fin de page:
 * 	<script type="text/javascript">
 *		carrousel = new Carrousel('carrousel-conteneur', 3000, 10000, 60, new Array(9, 16, 21, 24, 25, 24, 21, 16, 9), 400);
 *		ajouterCarrousel(carrousel);
 *		demarrerCarrousel();
 *	</script>
 *
 */

/* tableau des carrousels de la page */
tabCarrousel = new Array();

/**
 * Ajoute un carrousel au tableau
 * 
 * @param nouveau carrousel
 */
function ajouterCarrousel(nouveau) {
	tabCarrousel[tabCarrousel.length] = nouveau;
}

/**
 * demarre les carrousels de la page
 * 
 * @return
 */
function demarrerCarrousel() {
	for ( var i = 0; i < tabCarrousel.length; i++) {
		carrousel = tabCarrousel[i];
		carrousel.start();
	}
}

/**
 * utilisable pour passer à la baniere précédente par un bouton ou un lien
 * 
 * @param id identifiant du conteneur
 * @return
 */
function afficherPrecedent(id) {
	carrousel = findCarrousel(id);
	if (!carrousel.mouvementEnCours) {
		carrousel.goToRightByHand();
	}
}

/**
 * utilisable pour passer à la baniere suivante par un bouton ou un lien
 * 
 * @param id identifiant du conteneur
 * @return
 */
function afficherSuivant(id) {
	carrousel = findCarrousel(id);
	if (!carrousel.mouvementEnCours) {
		carrousel.goToLeftByHand();
	}
}

/**
 * Trouve un carrousel par son id dans le tableau des carrousels
 * 
 * @param id
 * @return
 */
function findCarrousel(id) {
	for ( var i = 0; i < tabCarrousel.length; i++) {
		if (tabCarrousel[i].id == id) {
			return tabCarrousel[i];
		}
	}
	return null;
}

/**
 * Objet Carrousel
 * 
 */
function Carrousel(id, tempsCourt, tempsLong, lapseAuto, lapseManu, pas, tailleImage, modeRotation) {
	/**
	 * id: Identifiant du conteneur
	 */
	this.id = id;

	/**
	 * temps d'attente entre chaque changement
	 */
	this.tempsCourt = tempsCourt;

	/**
	 * temps d'attente après avoir déclanché une rotation manuelle
	 */
	this.tempsLong = tempsLong;

	/**
	 * temps d'attente entre chaque petit décalage vers la droite ou vers la
	 * gauche
	 */
	this.lapseAuto = lapseAuto;

	this.lapseManu = lapseManu;

	/**
	 * décalage en px entre chaque 'lapse'
	 */
	this.pas = pas;

	/**
	 * la taille des banieres
	 */
	this.tailleImage = tailleImage;

	/**
	 * utilisé pour retenir la position des deux banieres en mouvements
	 */
	this.margin = 0;

	/**
	 * utiliser pour stocker le timer
	 */
	this.timer = null;

	this.mouvementEnCours = false;

	// nettoyage du carrousel
	var carrouselConteneur = document.getElementById(id);
	var childs = carrouselConteneur.childNodes;
	var tabToSupr = new Array();
	var obj, i;

	for (i = 0; i < childs.length; i++) {
		obj = childs[i];
		if (obj.nodeType == 3) {
			tabToSupr[tabToSupr.length] = obj;
		}
	}
	for (i = 0; i < tabToSupr.length; i++) {
		obj = tabToSupr[i];
		carrouselConteneur.removeChild(obj);
	}



	/**
	 * valeurs: automatique; manuel
	 */
	if (typeof modeRotation != 'undefined') {
		this.modeRotation = modeRotation;
	} else {
		this.modeRotation = 'automatique';
	}

	/**
	 * Démarre le carrousel
	 */
	this.start = function() {
		this.mouvementEnCours = false;

		if (this.modeRotation == 'automatique') {
			this.initGoToLeft(this.lapseAuto);
		}
	};

	/**
	 * initialise le carrousel entre chaque rotation
	 */
	this.initCarrousel = function() {
		var carrouselConteneur = document.getElementById(id);
		var childs = carrouselConteneur.childNodes;
		for ( var i = 0; i < childs.length; i++) {
			childs[i].style.left = '0px';
		}
	};

	/**
	 * déclanchement manuel de la rotation pour la gauche
	 */
	this.goToLeftByHand = function() {
		clearTimeout(this.timer);
		this.initCarrousel();
		this.initGoToLeft(this.lapseManu);
	};

	/**
	 * déclanchement manuel de la rotation pour la droite
	 */
	this.goToRightByHand = function() {
		clearTimeout(this.timer);
		this.initCarrousel();
		this.initGoToRight(this.lapseManu);
	};

	/**
	 * initialisation de la rotation vers la gauche
	 */
	this.initGoToLeft = function(lapseParam) {

		this.mouvementEnCours = true;

		if (typeof this.doBeforeLeft == 'function') {
			this.doBeforeLeft();
		}

		this.initCarrousel();
		if (lapseParam == undefined) {
			lapseParam = this.lapseAuto;
		}

		clearTimeout(this.timer);

		var carrouselConteneur = document.getElementById(this.id);
		var childs = carrouselConteneur.childNodes;
		this.imgActuelle = childs[0];
		this.imgSuivante = childs[1];

		this.slideLeft(lapseParam);
	};

	/**
	 * initialisation de la rotation vers la droite
	 */
	this.initGoToRight = function(lapseParam) {
		this.mouvementEnCours = true;

		if (typeof this.doBeforeRight == 'function') {
			this.doBeforeRight();
		}

		this.initCarrousel();
		if (lapseParam == undefined) {
			lapseParam = this.lapseAuto;
		}

		clearTimeout(this.timer);

		carrouselConteneur = document.getElementById(this.id);
		this.imgActuelle = carrouselConteneur.firstChild;
		this.imgSuivante = carrouselConteneur.lastChild;

		this.imgSuivante.style.left = '-' + this.tailleImage + 'px';
		carrouselConteneur.removeChild(this.imgSuivante);
		this.imgActuelle.style.left = '-' + this.tailleImage + 'px';
		carrouselConteneur.insertBefore(this.imgSuivante, this.imgActuelle);
		this.margin = -(this.tailleImage);
		this.slideRight(lapseParam);
	};

	/**
	 * effectue un petit déplacement vers la gauche
	 */
	this.slideLeft = function(lapseParam) {

		if (parseInt(this.imgSuivante.style.left) <= (- (this.tailleImage))) {
			clearTimeout(this.timer);
			this.initCarrousel();
			this.margin = 0;
			var carrouselConteneur = document.getElementById(id);

			carrouselConteneur.removeChild(this.imgActuelle);
			this.imgSuivante.style.left = '0px';
			carrouselConteneur.appendChild(this.imgActuelle);
			this.imgActuelle.style.left = '0px';
			carrousel = findCarrousel(this.id);
			this.mouvementEnCours = false;
			if (this.modeRotation == 'automatique') {
				if (lapseParam != this.lapseAuto) {
					this.timer = setTimeout( function(ms) {
						carrousel.initGoToLeft();
					}, this.tempsLong);
				} else {
					this.timer = setTimeout( function(ms) {
						carrousel.initGoToLeft();
					}, this.tempsCourt);
				}
			}
		} else {
			this.margin -= this.getPas();
			if (this.margin <= (- this.tailleImage)) {
				this.imgSuivante.style.left = -this.tailleImage + 'px';
				this.imgActuelle.style.left = -this.tailleImage + 'px';
			} else {
				this.imgSuivante.style.left = this.margin + 'px';
				this.imgActuelle.style.left = this.margin + 'px';
			}
			carrousel = findCarrousel(this.id);
			this.timer = setTimeout( function(ms) {
				carrousel.slideLeft(lapseParam);
			}, lapseParam);
		}
	};

	/**
	 * effectue un petit déplacement vers la droite
	 */
	this.slideRight = function(lapseParam) {
		if (parseInt(this.imgActuelle.style.left) >= 0) {
			clearTimeout(this.timer);
			this.initCarrousel();
			this.margin = 0;
			this.mouvementEnCours = false;
			if (this.modeRotation == 'automatique') {
				if (lapseParam != this.lapseAuto) {
					this.timer = setTimeout( function(ms) {
						carrousel.initGoToLeft();
					}, this.tempsLong);
				} else {
					this.timer = setTimeout( function(ms) {
						carrousel.initGoToLeft();
					}, this.tempsCourt);
				}
			}
		} else {
			this.margin += this.getPas();
			if (this.margin >= 0) {
				this.imgSuivante.style.left = '0px';
				this.imgActuelle.style.left = '0px';
			} else {
				this.imgSuivante.style.left = this.margin + 'px';
				this.imgActuelle.style.left = this.margin + 'px';
			}
			carrousel = findCarrousel(this.id);
			this.timer = setTimeout( function(ms) {
				carrousel.slideRight(lapseParam);
			}, lapseParam);
		}
	};

	this.getPas = function() {
		if (typeof this.pas == 'object' && this.pas.length) {
			nbPas = this.pas.length;
			tailleSegment = this.tailleImage / nbPas;
			if (this.margin < 0) {
				index = parseInt(-this.margin/tailleSegment);
			} else {
				index = parseInt(-this.margin/tailleSegment);
			}
			if (index < 0) {
				return this.pas[0];
			} else if (index >= this.pas.length) {
				return this.pas[this.pas.length - 1];
			} else {
				return this.pas[index];
			}
		} else {
			return this.pas;
		}
	};

}

