$(document).ready(function() {
 
 $.timer = function (intervals, callback)
 {
	var interval = intervals ;
	if (!callback)
		return false;
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (vals) {
			if (self.id)
				clearInterval(self.id);
			
			var val = vals ;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };



$(".diapo").each(function(index, el) {
    
	var NbElement = "";
	el = $(this);
	// Element de rˇfˇrence pour la zone de visualisation (ici le premier item) 
	Reference = el.children();
	
	// Nombre d'ˇlˇments de la liste 
	NbElement = (el.children().length);
        
	el.wrap('<div class="carrousel-conteneur"></div>').width( Reference.width()).height(el.children().height());
        el.children().css("position","absolute");
	//el.css("width", Reference.width() );
	el.parent().css("overflow", "hidden");
        
        $(".target").each(function(index,ele){
           $(this).addClass("target"+(index+1));
        });
        
        var Cpt = 1;
        //affichage de la premi¸re diapo
        $(".target"+Cpt).fadeIn("slow");
        
        // Actions du timer
        $.timer(3500, function (timer) {
            //si on atteint la derni¸re diapo
            if(Cpt == (NbElement) ) {
                // fondu OUT du carrousel
                $(".target"+(Cpt)).fadeOut("slow");
                $(".target1").fadeIn("slow",function(){
                    // Rˇ-Initialisation du compteur 
                    Cpt = 1;
                });  
            }
            if(Cpt < (NbElement) ) {
                // fondu OUT du carrousel
                $(".target"+Cpt).fadeOut("slow");
                // Ajout +1 au compteur (nous allons sur la diapositive suivante) 
                Cpt++;
                // fondu IN du carrousel
                $(".target"+(Cpt)).fadeIn("slow");
                
            }

	
        }); 
        
    });
});
