var fctIndiceActif = function(arrayLi) {
	for(i = 0; i < arrayLi.length; i++) {
		if(arrayLi[i].attr("class") == 'active') {
			return i;
		}
	}
	return 0;
};

var fctArrayLi = function(cible) {
	var arrayLi = new Array();
	var i = 0;
	jQuery("ul[class!=list] > li", cible).each(function(){
		arrayLi[i] = jQuery(this);
		i++;
	});
	return arrayLi;
};

var evenementCarousel = function(cible, auto, delai) {
	var arrayLi = fctArrayLi(cible);
	var timerID = '';
	
	if(auto) {
		timerID = setInterval(function() {
			var indiceActif = fctIndiceActif(arrayLi);
			
			var indiceNext = (indiceActif + 1);
			if( indiceNext >= arrayLi.length) indiceNext = 0;
			
			arrayLi[indiceActif].removeClass("active");
			arrayLi[indiceNext].addClass("active");
		}, delai);
		
		cible.hover(
			function() {
				clearInterval(timerID);
			},
			function() {
				timerID = setInterval(function() {
					var indiceActif = fctIndiceActif(arrayLi);
					
					var indiceNext = (indiceActif + 1);
					if( indiceNext >= arrayLi.length) indiceNext = 0;
					
					arrayLi[indiceActif].removeClass("active");
					arrayLi[indiceNext].addClass("active");
				}, delai);
			}
		);
	}
	
	jQuery(".prew", cible).click(function() {
		var indiceActif = fctIndiceActif(arrayLi);
		
		var indicePrev = (indiceActif - 1);
		if( indicePrev < 0) indicePrev = (arrayLi.length - 1);
		
		arrayLi[indiceActif].removeClass("active");
		arrayLi[indicePrev].addClass("active");
		
		return false;
	});
	
	jQuery(".next", cible).click(function() {
		var indiceActif = fctIndiceActif(arrayLi);
		
		var indiceNext = (indiceActif + 1);
		if( indiceNext >= arrayLi.length) indiceNext = 0;
		
		arrayLi[indiceActif].removeClass("active");
		arrayLi[indiceNext].addClass("active");
		
		return false;
	});
}
