/**
* author : Smart Agence
  -- [respecter les structures cote html en utilisant ce script ... : ) ]
*/

function fadeshow(){
	var index = 1, max_div = 6, speed = 2000;
	this.actif = null;
	
	this.init = function(sp) {  
		if( $("#fadeShow").length <= 0 ) return ;
		speed = sp || 2000;			
		$("#fadeShow  div.fadeBloc").slice(1).hide();
		
		/*____________ [ set id for each bloc] __________________*/
		for(var j = 0; j < max_div; j+=1) {
			var id = 'id' + j;
			$("#fadeShow  div.fadeBloc").eq(j).attr("id",id);
		}   
		
		reactif();												   
		timer(); 
		
		$("#menuFaitsActu a").bind('click',function(event){
			event.stopPropagation();
			event.preventDefault();
			var n =  $("#menuFaitsActu a").index(this);
			if(n != (index-1)) { 
				setStart(n);
				index = index + 1;
			}			
		});
		$("#menuFaitsActu a img").each(function(){ 
			mouse_over_out($(this));
		});
		
		$(window).unload( function () { 		
				setStop();
				desactif();
			} );			   
	};	
	
	function mouse_over_out(img){	
		var src = img.attr("src");
		var ftype = src.substring(src.lastIndexOf('.'), src.length);
		
		img.bind("mouseover",function(){
			if(/_on/.test(src)){ return; }
			src = src.replace(ftype, '_over'+ftype);
			img.attr("src",src);		
		});
		img.bind("mouseout",function(){
			if(/_over/.test(src)) {						
				src = src.replace('_over', '');
				$(this).attr("src",src);
			}
		});		
	}
	
	function reactif(){	
		$("#fadeShow").bind("mouseover",function () {
				setStop();           
			});
		$("#fadeShow").bind("mouseout",function () {
				timer();           
			});
	}	
	
	function desactif(){			
		$("#fadeShow").unbind("mouseout");
	}	
	
	function timer () {	
		setStop();
		this.actif = setTimeout(function(){setAuto();}, speed);
	}	
	
	function setAuto(){
		if(index >= max_div) {index = 0;}
		setStart(index);
		index+=1;
		timer() ;
	}
	
	function setStart(n) {
		var id = 'id' + n ;   
		$("#fadeShow div.fadeBloc:visible").css("display","none");
		$("#fadeShow  div#"+id).fadeIn(1000); 
		upDateMenu(n);	
		index = parseInt(n);
	}
	
	function upDateMenu(n){			
		$("#menuFaitsActu a img").each(function(){
			var src = $(this).attr("src");
			if(/_on/.test(src)) {
				src = src.replace('_on','');
				$(this).attr("src",src);
				mouse_over_out($(this));
			} 
			if(/_over/.test(src)) {
				src = src.replace('_over','');
				$(this).attr("src",src);				
			}
		});
		
		var img,src,newsrc,ftype;
			img = $("#menuFaitsActu a img").eq(parseInt(n));
			src = img.attr("src");				
			ftype = src.substring(src.lastIndexOf('.'), src.length);
			newsrc = src.replace(ftype, '_on'+ftype);
			img.attr("src",newsrc);			
			img.bind("mouseout",function(){
				img.attr("src",newsrc);		
			});	
			img.bind("mouseover",function(){
				img.attr("src",newsrc);		
			});	
			
	}
	
	function setStop(){
		clearTimeout(this.actif);
		this.actif = null;
	}	
}

jQuery(document).ready(function($) {   
    var myfadeshow = new fadeshow(); 
	myfadeshow.init(5000);
});

