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

		var defaults = {
			delay: 3,
			animationSpeed: "normal",
			controls:true
		};
		var options = $.extend(defaults, options);
		
		this.each(function(){
		
			var obj = $(this);
			
			
			if($(obj).find("div").length > 1){
				var inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
				var sens = "right";
				var pause = false;
				
				$(obj).find("div").hide();
				/**  Uniquement pour nicepore cite  **/
				$(obj).find("span.home-actu-date").show();
				/**  FIN Uniquement pour nicepore cite  **/
				
				$(obj).find("div:first-child").addClass("active").fadeIn(options.animationSpeed);
				
				// Controls
				
				if(options.controls){
						
					$(obj).after("<div class='home-actu-next'><a href='#' class='next'>Suivant</a></div><div class='home-actu-prev'><a href='#' class='prev'>Précédent</a></div><div class='home-actu-pause'><a href='#' class='pause'>Lecture/Pause</a></div>");
				
					$(obj).siblings().find(".prev").click(function(){
						clearInterval(inter);
						prevElt(options);
						if(!pause)
							inter = setInterval(function(){prevElt(options)}, (options.delay*1000));
						sens = "left";
						return false;
					});
					
					$(obj).siblings().find(".next").click(function(){
						clearInterval(inter);
						nextElt(options);
						if(!pause)
							inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
						sens = "right";
						return false;
					});
					
					$(obj).siblings().find(".pause").toggle(
						function(){
							$(this).removeClass("pause").addClass("play");
							clearInterval(inter);
							pause = true;
							return false;
						},
						function(){
							$(this).removeClass("play").addClass("pause");
							inter = setInterval(function(){ (sens == "right")?nextElt(options):prevElt(options)}, (options.delay*1000));
							pause = false;
							return false;
						}
					);
				}
				
				// Affiche l'élément suivant
				
				function nextElt(options){
					$(obj).find("div.active").fadeOut(options.animationSpeed,function(){
					
						if(!$(obj).find("div.active").is(":last-child")){
							$(obj).find("div.active").next().addClass("active").prev().removeClass("active");
							$(obj).find("div.active").fadeIn(options.animationSpeed);
							
						}
						else{
							$(obj).find("div:first-child").addClass("active").fadeIn(options.animationSpeed);
							$(obj).find("div:last-child").removeClass("active");
						}
					
					});
					
					
				}
				
				// Affiche l'élément précédent
				
				function prevElt(options){
					$(obj).find("div.active").fadeOut(options.animationSpeed,function(){
					
						if(!$(obj).find("div.active").is(":first-child")){
							$(obj).find("div.active").prev().addClass("active").next().removeClass("active");
							$(obj).find("div.active").fadeIn(options.animationSpeed);
							
						}
						else{
							$(obj).find("div:last-child").addClass("active").fadeIn(options.animationSpeed);
							$(obj).find("div:first-child").removeClass("active");
						}
					});
					
					
				}
			}
		});
		
		return this;
	};
})(jQuery);
