(function($){
	
	$.fn.fader = function(options) {
		var opt;
		var objects;
		var controls;
		var objectscontainer;
		var controlcontainer;
		var container;
		var currentid=0;
		var current;
		var fading=false;
		var auto=false;
		var delay=3000;
		var fadeDelay=1000;
		var timer;
		opt=options;
		container=$(this);
		objectscontainer=$(opt.container);
		controlcontainer=$(opt.controls);
		auto=$(opt.auto);
		objects = $('li',objectscontainer);
		controls=$('li',controlcontainer);
		
		
		if (objects.length>1) {
		
	init();
	buildControls();
	
	if (auto) timer=setTimeout(next, delay); 

}
	
	function init() {
		
		
		
		
		
		var compte=0;
		objects.each(function(i){
			$(this).css('position','absolute');
			$(this).css('top','0px');
			$(this).css('left','0px');
			$(this).css('zIndex',compte+1);
			$(this).attr('ref',compte);
		compte++;
		
		});
		
		
		currentid=0;
		
$('li[ref='+currentid+']').css('zIndex',1000);

//$('#image0').css('zIndex',1000);
		
		 compte=0;
		
		controls.each(function(i){
		
			$(this).css('cursor','pointer');
			$(this).attr('rel',compte);
			$(this).click(function(event){
				if (timer) clearTimeout(timer); 
				if (fading==false) {
					
					show(parseInt($(this).attr("rel")));
				}
			})
			
			compte++;
			
			
		});
		
		//alert(compte);
	}
	function buildControls() {
		
		Reference = $(controls[0]);
		Referencesuivante=  $(controls[1]);
		
		var refwidth=parseFloat(Reference.width())+parseFloat(Reference.css('margin-left'))+parseFloat(Reference.css('margin-right'));
		
var numItems=controls.length;
		 
controlcontainer.css('width',(numItems*refwidth)+10);
		 
		 Cpt = 0;
		 
		 
		 $("#buttonright").css('cursor','pointer');
		 $("#buttonleft").css('cursor','pointer');
		 
		         // Clic sur le bouton "Suivant"
		         $("#buttonright").click(function() {
		 	            // Ajout +1 au compteur (nous allons sur la diapositive suivante)
		             
		             // Mouvement du carrousel en arrière-plan
		             if (Cpt<numItems-9)    {
		            	 Cpt++;
		             controlcontainer.animate({
		            	 marginLeft : - (refwidth * Cpt)
		 	            });
		             }
		             
		 	        });
		 	        // Clic sur le bouton "Précédent"
		 	        $("#buttonleft").click(function() {
		 	        	
		            // Soustraction -1 au compteur (nous allons sur la diapositive précédente)
			            
			            // Mouvement du carrousel en arrière-plan
			            if (Cpt>0) { 
			            	Cpt--;
			            	controlcontainer.animate({
		                 marginLeft : - (refwidth * Cpt)
			            });
			            }
		         });
	}
	function next() {
		currentid=parseInt(currentid);
		
		if (currentid>=objects.length-1) {
			show(0);
		}
		else {
			
			
			
			
			show(currentid+1);
			
		}
			//if (currentid>=objects.length) currentid=0;
			
			
			
			

		
		
	}
	function show(newid) {
		newid=newid.toString();
	//alert(newid);
	//alert(typeof(newid));
		fading=true;
		
	//	console.log("tofade",currentid);
		$('#image'+newid).css('zIndex',990);
		$('#image'+currentid).fadeOut(1000, function() {
		 
			
			$('#image'+newid).css('zIndex',1000);
			
			$(this).css('zIndex',900);
			
			$(this).fadeIn(0);
			
			$('#image'+newid).css('zIndex',fadeDelay);
			fading=false;
			
			if (auto) timer=setTimeout(next, delay-fadeDelay); 
		  });

		
		

		currentid=newid;
	
	//	console.log("new",currentid);
		
	
	}
	
	}
})(jQuery);
