(function($){
	$.fn.diaporama = function(options) {

		var defaults = {
			delay: 8,
			animationSpeed: 1000,
			controls:true
		};
				
		var options = $.extend(defaults, options);
		
		this.each(function(){
			var obj = $(this);
			
			
			if($(obj).find("li").length > 1){
				var inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
				var sens = "right";
				var pause = false;
				
				$(obj).find("li img").css({'opacity':'0', 'filter':'alpha(opacity=0)'});
				//$(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
				$(obj).find("li:first-child img").addClass("active").animate({'opacity':'1.0', 'filter':'alpha(opacity=100)'}, options.animationSpeed);
				
				// Controls
				
				if(options.controls)
				{
					$('.next').mouseover(function(){
						$('.next').animate({opacity:1.0}, 300);
					}).mouseout(function(){
						$('.next').animate({opacity:0.3}, 300);
					});
					
					
					$('.prev').mouseover(function(){
						$('.prev').animate({opacity:1.0}, 300);
					}).mouseout(function(){
						$('.prev').animate({opacity:0.3}, 300);
					});
					
					$(obj).siblings().find(".prev").click(function(){
						clearInterval(inter);
						prevElt(options);
						if(!pause)
							inter = setInterval(function(){prevElt(options)}, (options.delay*1000));
						sens = "left";
					});
					
					$(obj).siblings().find(".next").click(function(){
						clearInterval(inter);
						nextElt(options);
						if(!pause)
							inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
						sens = "right";
					});
													
					$(obj).siblings().find(".pause").toggle(
						function(){
							$(this).removeClass("pause").addClass("play");
							clearInterval(inter);
							pause = true;
						},
						function(){
							$(this).removeClass("play").addClass("pause");
							inter = setInterval(function(){ (sens == "right")?nextElt(options):prevElt(options)}, (options.delay*1000));
							pause = false;
						}
					);
				}
				
				// Affiche l'Ã©lÃ©ment suivant
				
				/*
				function nextElt(options)
				{
					$(obj).find("li.active").fadeOut(options.animationSpeed, function(){
						if(!$(obj).find("li.active").is(":last-child"))
							$(obj).find("li.active").removeClass("active").next().addClass("active").fadeIn(options.animationSpeed);
						else
						{
							$(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
							$(obj).find("li:last-child").removeClass("active");
						}		
					});
					
					
					
				}
				*/
				
				function nextElt(options)
				{
					
					$(obj).find("li.active img").animate({'opacity':'0', 'filter':'alpha(opacity=0)'}, options.animationSpeed);
						
					if(!$(obj).find("li.active").is(":last-child")){
						$(obj).find("li.active").removeClass("active").next().addClass("active");
						$(obj).find("li.active img").animate({'opacity':'1.0', 'filter':'alpha(opacity=100)'}, options.animationSpeed)
					}
					
					else
					{
						$(obj).find("li:first-child").addClass("active");
						$(obj).find("li:last-child").removeClass("active");
						$(obj).find("li.active img").animate({'opacity':'1.0', 'filter':'alpha(opacity=100)'}, options.animationSpeed)
					}	
					
					
					
				}
				
				// Affiche l'Ã©lÃ©ment prÃ©cÃ©dent
				
				function prevElt(options)
				{
					$(obj).find("li.active img").animate({'opacity':'0', 'filter':'alpha(opacity=0)'}, options.animationSpeed);
						
					if(!$(obj).find("li.active").is(":first-child")){
						$(obj).find("li.active").removeClass("active").prev().addClass("active");
						$(obj).find("li.active img").animate({'opacity':'1.0', 'filter':'alpha(opacity=100)'}, options.animationSpeed);
					}
					else
					{
						$(obj).find("li:last-child").addClass("active");
						$(obj).find("li:first-child").removeClass("active");
						$(obj).find("li.active img").animate({'opacity':'1.0', 'filter':'alpha(opacity=100)'}, options.animationSpeed)
					}	
					
					
				}
			}
		});
		
		return this;
	};
})(jQuery);
