var enCoursClient=false, enCoursUne=false, timeAcc=null, timeAcc2=null, timeAcc3=null, timeAcc4=null, timeAcc5=null, timeAcc6=null;
var pauseEnCours=false;

// Animation cover (haut)
var animCover = function(){

	if(!pauseEnCours){

		try{
			enCoursUne=true;
			var el = $("#coverDefil .block:visible");
			var el2 = el.next(); 
			el2.fadeIn(1000,function(){
			  el.hide();
			  if( !el2.next().is('.block') ){
			    $("#coverDefil .block:first").show();
			    el2.hide();
			  }
			  enCoursUne=false;
				setNavUne();
			  animClient();
			});
		}catch(e){ 
		  enCoursUne=false;
			setNavUne();
			animClient(); 
		}

	}else{

	  enCoursUne=false;
		setNavUne();
		animClient(); 

	}
  
};

// Animation client (nouveaux sites)
var animClient = function(){

	try{
	  var el = $("#acc_client .block:visible");
	  var el2 = el.next(); if(el2.length==0) el2 = $("#acc_client .block:first");
	  enCoursClient=true; var img = el2.find('.preload'); if(img.length>0) img.attr('src',img.attr('rel')).removeClass('preload');
		setClientHeight();
	  el.fadeOut(1000,function(){ // Cache client
	    el2.fadeIn(750,function(){ // Affiche client
	      enCoursClient=false;
	      animPart();
	    });
	  });
	}catch(e){
    enCoursClient=false;
    animPart();
	}
  
};

// Animation partenaire (nous font confiance)
var animPart = function(){

	try{
	
	  var el = $("#acc_confiance .block:visible");
	  var el2 = el.next(); if(el2.length==0) el2 = $("#acc_confiance .block:first");
		var img = el2.find('.preload'); if(img.length>0) img.each(function(){ var el = $(this); el.attr('src',el.attr('rel')).removeClass('preload'); });
	  var time=1;
	
    el.fadeOut(250,function(){

      // Affiche partenaire
      el2.css({position:'relative',left:'270px'}).show().animate({left:0},500,function(){ startAccueil(); });
      
    });

	}catch(e){
		startAccueil();
	}
};

var startAccueil = function(){ clearTimeout(timeAcc); timeAcc=setTimeout(animCover,5000); };
var PlayPauseAcc = function(){
	if(pauseEnCours){
		pauseEnCours=false;
	}else{
		pauseEnCours=true;
	}
	setNavUne();
};
var setNavUne = function(){
	try{
	  var els = $("#coverDefil .block");
	  var nb = els.length-1; var html = ''; var select=0;
		for(i=0;i<els.length;i++){ if( $(els[i]).is(':visible') ) select=i; }
		if(select==els.length) select=0; select++; if(select>nb) select=1;
	  for(i=1;i<=nb;i++){
	    var sel = false; if(select==i) sel=true;
	    sel=(sel)?' class="select"':' onclick="changeUne(\''+(i-1)+'\')"';
	    html+='<a href="javascript:;"'+sel+'>'+i+'</a> ';
	  }
		if(pauseEnCours){
			html+='<a id="coverBtnPause" href="javascript:;" onclick="PlayPauseAcc()" style="margin:0;padding:0"><img src="images/play.gif" alt="Play" title="Play" style="margin:0;padding:0;" /></a>';
		}else{
			html+='<a id="coverBtnPause" href="javascript:;" onclick="PlayPauseAcc()" style="margin:0;padding:0"><img src="images/pause.gif" alt="Pause" title="Pause" style="margin:0;padding:0;" /></a>';
		}
	  $("#nav_cover").show().html(html);
	}catch(e){}
};
var changeUne = function(nb){
  if(enCoursUne) return;
	try{
		enCoursUne=true;
		var el = $("#coverDefil .block:visible");
		var el2 = $( $("#coverDefil .block")[nb] );
		el.fadeOut(600);
	  el2.fadeIn(600,function(){
	    if( !el2.next().is('.block') ){
	        $("#coverDefil .block:first").show();
	        el2.hide();
	    }
			el.hide(); 
			enCoursUne=false; 
			setNavUne();
		});
	}catch(e){}
};

var setClientHeight = function(){
	try{
	  $("#acc_client .photo").each(function(){
	    var el = $(this);
	    var parent = el.parent();
	    var show=parent.is(':visible');
	    parent.show();
	    var h = el.find('img').height();
	    var h = Math.min(200,h);
	    el.height(h);
	    if(!show) parent.hide();
	  });
	}catch(e){}
};
$(function(){

  // Nouveaux clients
  $("#menu_client").show();
  setNavUne();
  setClientHeight();
  $("#menu_client .prev").click(function(){
    if(enCoursClient) return;
    var el = $("#acc_client .block:visible");
    var el2 = el.prev(); if(el2.length==0) el2 = $("#acc_client .block:last");
		setClientHeight();
    el.fadeOut(500,function(){
      el2.fadeIn(250);
    });
  });
  $("#menu_client .next").click(function(){
    if(enCoursClient) return;
    var el = $("#acc_client .block:visible");
    var el2 = el.next(); if(el2.length==0) el2 = $("#acc_client .block:first");
		setClientHeight();
    el.fadeOut(500,function(){
      el2.fadeIn(250);
    });
  });
  startAccueil();

});


function preloadImg(){
	$(".preload").each(function(){
		var el = $(this);
		var src = el.attr('rel')
		el.attr('src',src);
		el.attr('onload',setClientHeight);
		el.removeClass('preload');
	});
}
addOnLoad('preloadImg');

addOnLoad('setClientHeight');