var IMG_PATH = "/wp-content/themes/apparence/img/";
var bgSite;
var dureeSlide = 150;

function initPlanchable()
{
	$(".planche").hide();
	
	// Mélange des éléments
	
	nbPlanches = $(".planche").size();

	for (k=0; k<5; k++)
	{
		for (j=0; j<nbPlanches; j++)
		{
			i = Math.floor(Math.random()*nbPlanches);		
			if (i != j && !$(".planche").eq(j).hasClass("pasInsert") && !$(".planche").eq(i).hasClass("pasBouger"))					
				$(".planche").eq(i).insertBefore(".planche:eq("+j+")");		
		}
	}
	
	$(".planchable").click(function(){		
		masquerSite();				
		obj = $(this).next(".planche");		
		chargerEtAfficherSlide(obj, true);
	});	
}


function masquerSite()
{
	$("#global, #menuSecondaire, #barreGrise, #barreGriseGlobal, #btnMenuDynamique").hide();
	$("#itemGrisSelected").css("visibility", "hidden");
	bgSite = $("body").css("backgroundImage");
	$("body").css("background", "none");
}


function afficherSite()
{
	$("#global, #menuSecondaire, #barreGrise, #barreGriseGlobal, #btnMenuDynamique").show();
	$("#itemGrisSelected").css("visibility", "visible");
	$("body").css("backgroundImage", bgSite);
	$("body").css("backgroundRepeat", "no-repeat");
	$("#barreGrise").css("visibility", "visible");
	positionnerElements();
}


function afficherLoading()
{
	$("body").append("<img src='"+IMG_PATH+"loading.gif' id='loading' />");
	x = ($(window).width() - 36) >> 1; y = ($(window).height() - 36) >> 1;
	$("#loading").css("left", x+"px").css("top", y+"px").hide();
	setTimeout("$('#loading').show();", 150);
}


function masquerLoading()
{
	$("#loading").remove();
}


function chargerEtAfficherSlide(obj, sens)
{	
	$("#barreGrise").css("visibility", "hidden");
	afficherLoading();
	nom = obj.children(".photo").text();
	$("body").append("<img src='"+IMG_PATH+"planches/"+nom+"' id='thePlanche'/><div id='ombreHaut'></div><div id='ombreBas'></div>");
	
	$("#thePlanche").hide().load(function(){			
		masquerLoading();
		lp = $(this).width(); hp = $(this).height();
		l = $(window).width(); h = $(window).height() - 48;
		ratiop = lp/hp; ratio = l/h;		
		if (ratiop > ratio) {zoom=l/lp; lp=l; hp=hp*zoom;} else {zoom=h/hp; hp=h; lp=lp*zoom;}		
		$(this).css("width", lp+"px").css("height", hp+"px");
				
		// SLIDE ET AFFICHAGE
		xp = (l-lp)>>1; yp = (h-hp)>>1;
		$("body").css("overflow", "hidden");
		
		deb=xp+l; fin=xp;
		if (!sens) {deb=xp-l; fin=xp;}
		
		// OMBRE
		$("#ombreHaut").css("top", (yp-34)+"px").css("width", lp+"px").css("left", deb+"px").animate({left:fin+"px"}, dureeSlide);
		$("#ombreBas").css("top", (yp+hp)+"px").css("width", lp+"px").css("left", deb+"px").animate({left:fin+"px"}, dureeSlide);
		
		$(this).css("left", deb+"px").css("top", yp+"px").show().animate({left:fin+"px"}, dureeSlide, function(){
			$("body").css("overflow", "auto");									
			
			// CREATION DU FOOTER
			if (obj.children(".url").size() > 0) {intitule="Exemple"; feminin=""; lienSite="<a target='_blank' href='"+obj.children(".url").attr('href')+"'><img src='"+IMG_PATH+"visiterSite.png' /></a>";} else {intitule = "Référence"; feminin="e"; lienSite="";}
			$("body").append("<div id='footerPlanches'></div>");
			$("#footerPlanches").append("<table><tr><td></td><td></td><td>"+lienSite+"</td><td class='btn'><span id='slidePrev'><img src='"+IMG_PATH+"flecheItemGauche.gif' style='float:left;' />"+intitule+"<br/><b>précédent"+feminin+"</b></span></td><td class='btn' style='width:130px;'><span id='slideNext'><img src='"+IMG_PATH+"flecheItem.gif' />"+intitule+"<br/><b>Suivant"+feminin+"</b></span></td></tr></table>");
			desc = "<b>" + obj.children(".client").html() + "</b><br/>" + obj.children(".description1").html() + "<br/>" + obj.children(".description2").html();			
			$("#footerPlanches td:eq(1)").html(desc).css("borderLeft", "4px solid #808080").css("paddingLeft", "6px");
			$("#footerPlanches td:eq(0), #footerPlanches td:eq(3), #footerPlanches td:eq(4)").css("width", "140px");
			$("#footerPlanches td:last").css("textAlign", "right");
			yf = ((h-hp)>>2) + ((h-hp)>>3) +yp+hp;
			$("#footerPlanches").css("top", yf+"px");
			
			// FLECHES
			if (obj.next().hasClass("planche")) {$("#slideNext").show();$("#thePlanche").unbind("click").bind("click", function(){slide_next(obj);});} else $("#slideNext").hide();
			if (obj.prev().hasClass("planche")) $("#slidePrev").show(); else $("#slidePrev").hide();
			
			// RETOUR AU SITE			
			$("body").prepend("<img src='"+IMG_PATH+"retSite.png' id='retSite'/>");
			$("#retSite").unbind("click").bind("click", function(){
				$("#footerPlanches, #thePlanche, #ombreHaut, #ombreBas, #retSite").hide();
				afficherSite();
				$("#thePlanche, #ombreHaut, #ombreBas, #footerPlanches, #retSite").remove();								
			});
			
			// SLIDE SUIVANT
			$("#slideNext").unbind("click").bind("click", function(){slide_next(obj);});			
			
			// SLIDE PRECEDENT
			$("#slidePrev").unbind("click").bind("click", function(){
				$("#footerPlanches").hide();
				$("body").css("overflow", "hidden");				
				$("#ombreHaut, #ombreBas").animate({left:l+"px"}, dureeSlide);				
				$("#thePlanche").animate({left:l+"px"}, dureeSlide, function(){
					$("#thePlanche, #ombreHaut, #ombreBas, #footerPlanches, #retSite").remove();
					$("body").css("overflow", "auto");					
					obj = obj.prev();
					chargerEtAfficherSlide(obj, false);
				});
			});							
		});		
	});
}


function slide_next(obj)
{
	$("#footerPlanches,#barreGrise").hide();
	$("body").css("overflow", "hidden");					
	$("#ombreHaut, #ombreBas").animate({left:-l+"px"}, dureeSlide);
	$("#thePlanche").animate({left:-l+"px"}, dureeSlide, function(){					
		$("#thePlanche, #ombreHaut, #ombreBas, #footerPlanches, #retSite").remove();
		$("body").css("overflow", "auto");		
		obj = obj.next();
		chargerEtAfficherSlide(obj, true);
	});
}


function preloadPlanches()
{
	$(".planche").each(function(){
		jpeg = $(this).children(".photo").text();
		tof = "/wp-content/themes/apparence/img/planches/"+jpeg;
		$("body").append("<img src='"+tof+"' class='preloadJpeg' />");
	});
}


