var isIntro = false;

$(function(){
	$("body").append("<div id='motoSuperieure'></div>");
	$("body").append("<div id='motoInferieure'></div>");
	$("#global").hide();
	$("#fondCarbone").hide();
	$("a#btnLogo").text("");
});

function goIntro()
{
	isIntro = true;
	$("body").css("visibility", "visible").append("<a id='passerIntro' href='index.php?page=accueil&pasIntro'>Accéder directement au site</a>");
	$("img#logoAnimation").css("cursor", "pointer").one("click", function(){location.href="index.php?page=accueil&pasIntro";});
	
	var depart=300;
	var dureeApparitionLogo=1000;
	var pause1 = 1300;
	var dureeApparitionMoto=4000;	
	var pause2 = 2600;
	var departMoto = depart + dureeApparitionLogo + pause1;

	l = $(window).width(); h = $(window).height();
	centreH = (h>>1) + 100;
	centreL = l>>1;
	
	$("a#passerIntro").css("top", (h-30)+"px").css("left", (l-170)+"px");
	$("img#logoAnimation").fadeTo(0,0).css("top", (centreH - 202)+"px").css("left", (centreL-318)+"px").css("width", "637px");
	$("#motoSuperieure, #motoInferieure").css("left", (centreL-702)+"px").hide();
	$("#motoInferieure").css("top", centreH+"px");

	setTimeout("$('img#logoAnimation').fadeTo("+ dureeApparitionLogo +",0.7);", depart);	
	
	setTimeout("$('#motoSuperieure, #motoInferieure').fadeIn("+500+");", departMoto);
	_animationFluide("#motoSuperieure", "top", "px", centreH, centreH-500, dureeApparitionMoto, departMoto);
	_animationFluide("#motoSuperieure", "height", "px", 0, 500, dureeApparitionMoto, departMoto);
	_animationFluide("#motoInferieure", "height", "px", 0, 500, dureeApparitionMoto, departMoto);
	_animationFluideGeneralisee("positionnerBackgroundMotoInferieure", -1000, -500, dureeApparitionMoto, departMoto);
	
	setTimeout("preparationDeLaPage();", departMoto + dureeApparitionMoto + pause2);
}

function go()
{
	$("body").css("visibility", "visible");
	preparationDeLaPage();	
	if (typeof(go_page) == 'function') go_page();
}

function positionnerBackgroundMotoInferieure(y) {$("#motoInferieure").css("background-position", "0px "+y+"px");}

function preparationDeLaPage()
{
	var dureeApparitionMasque = 800;
	var dureeDisparitionMasque = 800;
	
	if (! isIntro) {dureeApparitionMasque = 10; dureeDisparitionMasque = 10;}
	
	$("body").append("<div id='masqueComplet'></div>");
	l = $(window).width(); h = $(window).height();
	$("#masqueComplet").hide().css("width", l+"px").css("height", h+"px").fadeIn(dureeApparitionMasque, function(){

		// Préparation des éléments
		$("#motoSuperieure, #motoInferieure, img#logoAnimation, a#passerIntro").hide();
		$("body").css("background-image", "url(images/fond.jpg)").css("overflow", "auto")
		$("#global").show();
		$("#global").append("<div id='hoverBtnPrincipaux'></div>");
		$("#fondCarbone").show();
		$("#hoverBtnPrincipaux").fadeTo(0,0.3);
		$("a#btnCataloguePieces, a#btnSavoirFaire, a#btnUneHistoire").bind("mouseenter", function(){
			btnL = $(this).width() + 25; btnH = $(this).height() + 320;
			btnX = $(this).css("left"); btnY = $(this).css("top");
			$("#hoverBtnPrincipaux").css("height", btnH+"px").css("width", btnL+"px").css("left", btnX).css("top", btnY);
			$(this).fadeTo(0,0.7);
		}).bind("mouseleave", function(){
			$("#hoverBtnPrincipaux").css("height", "0px");
			$(this).fadeTo(0,1);
		});
				
		// Positionnement des éléments puis disparition du masque
		positionnerElements();
		window.onresize = positionnerElements;
		$(this).fadeOut(dureeDisparitionMasque);
	});
}

function positionnerElements()
{
	l = $(window).width(); h = $(window).height();
	fondX = (l>>1) - 702; fondY = (h>>1) - 400;
	
	if (fondY<-120) fondY=-120;
	
	$("body").css("background-position", fondX+"px "+fondY+"px");
	
	globalX = fondX + 256; globalY = fondY + 105;
	$("#global").css("left", globalX+"px ").css("top", globalY+"px");
	
	fondCarboneX = (l - 679) >> 1; fondCarboneY = fondY + 1000 - 769; 
	fondCarboneH = h-fondCarboneY-1;
	if (fondCarboneH < 472) fondCarboneH = 472;
	$("#fondCarbone").css("left", fondCarboneX+"px").css("top", fondCarboneY+"px").css("height", fondCarboneH+"px");
	
	var positionBtn = 780;
	var iBtn = -1;
	$("#menuDuHaut a").each(function(){
		iBtn++;
		positionBtn -= $(this).width();
		$(this).css("left", positionBtn+"px").fadeTo(0,0.6);
	});	
	$("#menuDuHaut a:eq("+iBtn+")").css({"color":"black", "background":"url(images/bordArrondi.gif)"}).fadeTo(0,1);
	
	$("a#lienBackoffice").css("left", (l-22)+"px");
}

