// JavaScript Document
(function($){
	$.fn.diaporama = function(options) {

		var defaults = {
			delay: 9,
			animationSpeed: "normal",
			controls:true,
			nbimg: 5,
			currentPos: 1
		};
				
		var options = $.extend(defaults, options);
			options.nbimg=0;
			$(".diaporama li").each(function(){options.nbimg+=1;});//alert(options.nbimg);

		
		this.each(function(){
		
			var obj = $(this);
			
			//alert(options.nbimg);
			
			if($(obj).find("li").length > 1){
				options.currentPos = options.currentPos + 1;
				var inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
				var sens = "right";
				var pause = false;
				
				$(obj).find("li").hide();
				$(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
				// Controls
				
				if(options.controls)
				{
					$(obj).after('<div id="thumbs"><ul></ul></div>');
					for (y=0; y<options.nbimg; y++)
		                $('#thumbs ul').append('<li id="btn'+(y+1)+'" name="img'+(y+1)+'"><img src="images/btn.png" width="96" height="3" /></li>');
		            $('#thumbs li').addClass("hors");
					
					$('#thumbs li:first-child').addClass("survol");	
					 
					$("#thumbs li").click(function(){
												   
						clearInterval(inter);						   
						$(obj).find("li.active").fadeOut(options.animationSpeed);						   
						elt = ($(this).attr('id')).substr(3,5);
						courent = ($(obj).find("li.active").attr("id")).substr(3,7);

                        $('#img'+courent).removeClass("active");
						$('#img'+elt).addClass("active");;
						$(obj).find("li.active").fadeIn(options.animationSpeed);
						
						v1 = '#btn'+elt;
						e1 = '#btn'+courent;//alert(v1);
						$(e1).removeClass("survol");
						$(v1).addClass("survol");
						
						inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
						
					});
				}
				
				// Affiche l'élément suivant
				
				
				function nextElt(options)
				{
					$(obj).find("li.active").fadeOut(options.animationSpeed);
					
					if(!$(obj).find("li.active").is(":last-child"))
					{
						$(obj).find("li.active").next().addClass("active").prev().removeClass("active");
						$(obj).find("li.active").fadeIn(options.animationSpeed);
						
					}
					else
					{
						$(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
						$(obj).find("li:last-child").removeClass("active");
					}
					
					elt = '#btn'+($(obj).find("li.active").attr("id")).substr(3,7);
					$("#thumbs li").removeClass("survol");
					$(elt).addClass("survol");

               }
				
				
				// Affiche l'élément précédent
				
				function prevElt(options)
				{
					$(obj).find("li.active").fadeOut(options.animationSpeed);
					
					if(!$(obj).find("li.active").is(":first-child"))
					{
						$(obj).find("li.active").prev().addClass("active").next().removeClass("active");
						$(obj).find("li.active").fadeIn(options.animationSpeed);
						
					}
					else
					{
						$(obj).find("li:last-child").addClass("active").fadeIn(options.animationSpeed);
						$(obj).find("li:first-child").removeClass("active");
					}
				}
				
			}
		});
		
		return this;
	};
})(jQuery);
