
function Carrousel (idImgsContainer, speedCarrousel, speedAnimating, imgMargin, toNext){
	this.stepPerSecond = 30;
	this.nbSteps = speedAnimating / 1000 * this.stepPerSecond;
	this.imgsContainer = document.getElementById(idImgsContainer);
	this.speedCarrousel = speedCarrousel;
	this.speedAnimating = parseFloat(speedAnimating / this.nbSteps);
	this.imgMargin = (null !== imgMargin && undefined !== imgMargin && !isNaN(imgMargin) && imgMargin > 0) ? imgMargin : 0;
	this.toNext = (null !== toNext && undefined !== toNext && toNext === false) ? false : true;
	this.init();
}

Carrousel.prototype.init = function (){
	this.animating = false;
	this.prepareNextTimeouts = [];
	this.currentImgIndex = 0;
	this.imgs = this.imgsContainer.getElementsByTagName('li');
	this.nbImgs = this.imgs.length;
	this.imgs[0].style.display = 'block';
	for (var i=1; i<this.nbImgs; i++){
		this.imgs[i].style.display = 'none';
	}
	this.prepareNext();
};

Carrousel.prototype.prepareNext = function (){
	if (!this.animating && this.prepareNextTimeouts.length == 0){
		var inst = this;
		this.prepareNextTimeouts.push(setTimeout(function(){ inst.slideToNext(); }, this.speedCarrousel));
	}
};

Carrousel.prototype.clearPrepareNext = function (){
	if (this.prepareNextTimeouts.length > 0){
		while (this.prepareNextTimeouts.length > 0){
			clearTimeout(this.prepareNextTimeouts.pop());
		}
	}
};

Carrousel.prototype.slideToPrevious = function (){
	this.slideTo(false);
};

Carrousel.prototype.slideToNext = function (){
	this.slideTo(true);
};

Carrousel.prototype.slideTo = function (toNext){
	if (this.animating){
		return false;
	}
	this.animating = true;
	
	this.clearPrepareNext();
	
	if (toNext){
		var imgIndex = (this.currentImgIndex +1 < this.nbImgs) ? this.currentImgIndex +1 : 0;
	} else {
		var imgIndex = (this.currentImgIndex -1 >= 0) ? this.currentImgIndex -1 : this.nbImgs -1;
	}

	var longueurDeplacement = (this.imgsContainer.offsetWidth || this.imgsContainer.parentNode.offsetWidth || this.imgs[this.currentImgIndex].offsetWidth);
	longueurDeplacement += this.imgMargin; // On ajoute une marge entre chaque image
	var step = longueurDeplacement / this.nbSteps;
	
	var inst = this;
	for (var i=0; i<=this.nbSteps; i++){
		var delai = this.speedAnimating *i;

		if ((toNext && this.toNext) || (!toNext && !this.toNext)){
			this.setSlide(this.imgs[imgIndex], (i *-1 * step +longueurDeplacement), delai);
			this.setSlide(this.imgs[this.currentImgIndex], (i *-1 * step), delai);
		} else {		
			this.setSlide(this.imgs[imgIndex], (i * step -longueurDeplacement), delai);		
			this.setSlide(this.imgs[this.currentImgIndex], (i * step), delai);
		}
		if (i == 0){
			setTimeout(function(){ inst.imgs[imgIndex].style.display = 'block'; }, delai);
		}
	}
	setTimeout(function(){ inst.currentImgIndex = imgIndex; inst.animating = false; inst.prepareNext(); }, delai);
};

Carrousel.prototype.setSlide = function (obj, position, delai){
	setTimeout(function(){ obj.style.left = position +'px'; }, delai);
};

