// ---------------------------
$(document).ready(function() {
// ---------------------------
	$(".tem-content").css("visibility","hidden") ;
	
	if (document.readyState != "complete") {
    setTimeout(arguments.callee,100);
    return;
  }
 
	if ($(".tem").length > 0)
	{
		Reference = $(".tem li:first-child") ; 
		
		myWidthMax = 0 ;
		
		myWidths = new Array() ;
		$('.tem li').each(function(index) {
	    myWidthMax += $(this).width();
	    myWidths[index] = $(this).width();
	  });
	 	
	 	
	  if (myWidthMax <= $(".tem-content").width()) {
	  	$("#tem-next").hide();
	  	$("#tem-prev").hide();
	  }

		NbElement = $(".tem li").length ;
		w = $(".tem-content").width() ;
		$(".tem") 
		.wrap('<div class="tem-conteneur"></div>') 
		.css("width",(myWidthMax)); // CODE : .css("width",(Reference.width() * NbElement)); 

		$(".tem-conteneur") 
		.width(w)
		.height($(".tem-content").height())
		.css("overflow","hidden") 
		.after('');
		
		$(".tem-content").css("visibility","visible") ;
	
		// Actions de navigation 
     
		// Initialisation du compteur 
		Cpt = 0;
		myMargin = 0;
		
		var meW = 0 ;
		var meNb = 0 ;
		for (var i = 0 ; i < myWidths.length ; i++) {
			if (meW + myWidths[i] < $(".tem-content").width()) {
				meW += myWidths[i] ;
				meNb++ ;
			}
		}

		// Clic sur le bouton "Suivant" 
	  $("#tem-next").click(function() { 
	    // Si le compteur est inférieur au nombre de diaposives moins 1  
	    if(Cpt < NbElement - meNb) { 
	    	// Ajout +1 au compteur (nous allons sur la diapositive suivante) 
	    	Cpt++; 
	   		myMargin += myWidths[Cpt-1] ;
	    	// Mouvement du tem en arrière-plan 
	      $(".tem").animate({ 
	          marginLeft : - (myMargin) 
	      }); 
	    }
	    /*
	  	$("#tem-prev").css("background","url(mods/carroussel/img/btn1.gif) top left no-repeat"); 
	    if (Cpt == NbElement - meNb) $("#tem-next").css("background","url(mods/carroussel/img/btn2off.gif) top left no-repeat"); 
	    */
	  }); 
         
		// Action du bouton "Précédent" 
	  $("#tem-prev").click(function() { 
	  // Si le compteur est supérieur à zéro 
	  if(Cpt > 0) { 
	    // Soustraction -1 au compteur (nous allons sur la diapositive précédente) 
	    Cpt--;
	    myMargin -= myWidths[Cpt] ;
	    // Mouvement du tem en arrière-plan 
	    $(".tem").animate({ 
	        marginLeft : - (myMargin) 
	    }); 
	  }
	  /*
	  if (Cpt == 0) $("#tem-prev").css("background","url(mods/carroussel/img/btn1off.gif) top left no-repeat"); 
	  $("#tem-next").css("background","url(mods/carroussel/img/btn2.gif) top left no-repeat"); 
	  */
	  }); 
	}
});
