﻿/**
* author : Smart Agence
  -- [respecter les structures cote html en utilisant ce script ... : ) ]
*/

(function($){	
	$.fn.fadeshow = function(options){           //définir une fonction
		var defaults = {
				timeout : 5000
			};
		
		var options = $.extend(defaults, options);   //fusionner les options
		
		return this.each(function() {
			var actif = null,fadeshow = $(this);
			var innfadeblocs = [],fadeBloc = null;
			var total = 0, fin=0,listeNumbers = [];
								
			/* auto slider */			
			var stopTime = function(){
				window.clearInterval(actif);
				actif = null;
			};
			
			var next = 0;
			var run = function(){
				if( next == fin ) {
					next = 0;	
				}else {
					next += 1;				
				}			
				changeContent(next);
			};

			// create liste number
			var setMenuNumbers = function(){
				if( total > 0 ) {
					i = total;
					var tmp = new Array('<div id="menuNumber">');
					for(var i = 1; i <= total; i++) {
						var c ='';
						if( i == 1 ) {c ='class="on"';}
						tmp.push('<a id="num_'+i+'" ' + c + '><span>' + i + '</span></a>');
					}
					tmp.push('</div>');
					var html = tmp.join('\n');					
					fadeBloc.after(html);
					
					listeNumbers = $("#menuNumber a");
					
					$.each(listeNumbers,function(key,value){
						value.onclick = function(){
							changeContent( key );
							next = key;
							return false;
						};
					});
				} 
			};

			var start = function(){				
				actif = setInterval(function(){ run(); },options.timeout);				
			};
			
			var changeContent = function(i){
				fadeBloc.stop(true,true).fadeOut(500,function(){															  
					innfadeblocs.css("display","none");
					innfadeblocs.eq(i).css("display","block");
					
					listeNumbers.removeClass('on');
					listeNumbers.eq(i).addClass('on');
					
					fadeBloc.fadeIn(200);					
				});
			};

			//init	
			var init = function(){				
				fadeBloc = fadeshow.find("#fadeBloc");
				innfadeblocs = fadeshow.find(".inn-fadeBloc");				
				total = innfadeblocs.length;
				fin = total - 1;
				
				innfadeblocs.slice(1).css("display","none");
				setMenuNumbers();
				
				fadeshow.hover(function(){ stopTime(); },function(){ start(); });
				
				start();				
			};		
			
			init();
			
		}); // fin each	
	
	}; // fin fadeshow		
})(jQuery);


jQuery(document).ready(function($) { 
	if( $("#fadeShow").length > 0 ) {							
		$("#fadeShow").fadeshow({timeout : 5000 });
	}
});

