

$(function(){	
	creerBarreGrise();
	menuDynamique();
	$("#votreDevis").hide();
	if (nomPage != "lequipe") devisGratuit();
	actu();	
	if ($(".planche").size() > 0) initPlanchable();
	if ($("#sousMenu").size() > 0) gestionSousMenu();
	
	positionnerElements();	
	window.onresize = positionnerElements;
});


function go() 
{	
	// Puces H5
	$("#contenuGlissant h5").prepend("<img src='"+IMGPATH+"puceFleche.png' class='puceFleche' />");
	
	if (nomPage == "accueil") pageAccueil();
	if (nomPage == "lequipe") pageEquipe();
	if (nomPage == "contact") pageContact();
	if (nomPage == "latelier-de-communication-appliquee") pageAtelier();
	if (nomPage == "exemples-de-sites") pageExemplesWeb();
	if (nomPage == "tarifs-web") pageTarifs();	
	if (nomPage == "les-metiers-de-la-communication") pageMetiersComm();	
	$(".blocAccordeon").hide();	
	$(".blocAccordeon:lt(3)").show();	
	
	$("body").css("visibility", "visible");	
	
	diapo();
	
	if ($("#contenuGlissant").size() > 0) {
		$("#contenuGlissant h3").prepend("<img src='"+IMGPATH+"pipe.gif' class='pipe' alt='' /> ");
		$("#contenuGlissant em").prepend("<img src='"+IMGPATH+"bob.jpg' class='bob' alt='' /> ");
		
		initialiserAscenseur("#contenu", "#contenuGlissant", 30, "#ascenseur", "#curseurAscenseur", 5);		
	}
	
	if ($(".planche").size() > 0) preloadPlanches();
	
	$(".blocAccordeon:lt(3)").hide();	
}


function positionnerElements()
{
	l = $(window).width();	h = $(window).height();
	lg = $("#global").width(); hg = $("#global").height();
	x = (l-lg)>>1; y = ((h-hg)>>3)*3; // 3 huitièmes
	if (x<0) x=0; if (y<0) y=0;
	
	if (!isMenuDynamiqueOuvert)
	{
		$("#global").css("left", x+"px").css("top", y+"px");
	}
	else $("#global").css("left", (x-3000)+"px").css("top", y+"px");
	
	$("#menuDynamique").css("left", x+"px").css("top", (132+y)+"px");
	$("body").css("backgroundPosition", (x-582)+"px "+(y-210)+"px");
	
	if (l > 1000) $("#barreGrise").css("width", l +"px").css("top", (y+546)+"px").show();
	else $("#barreGrise").hide();
	
	$("#barreGriseGlobal").css("left", x +"px").css("top", (y+546)+"px");
	
	$("img#btnMenuDynamique").css("left", (x+895)+"px").css("top", (y+546)+"px");
	
	if ($("#sousMenu a.selected").size() > 0)
	{
		pos = $("#sousMenu a.selected img").offset();
		$("#itemGrisSelected").css("width", (pos.left+5)+"px").css("top", (pos.top)+"px").show();
	}
	
	if (h < hg+20) ySec = hg+2; else ySec = h-20;
	$("#menuSecondaire").css("top", ySec+"px").css("left", (l-420)+"px");
	
	if (l < 1180) $("#btnDevis").hide(); else $("#btnDevis").show();
}


function pageAccueil()
{	
	// On passe tout de suite sur le menu dynamique
	$("#btnMenuDynamique").click().css("visibility", "hidden");
	
	
	/*for (i=0; i<6; i++)	$("body").append("<div class='hoverAccueil'></div>");
	
	$(".hoverAccueil").hide().mouseleave(function(){
		$(".hoverAccueil").hide();
	}).each(function(k){
		$(this).click(function(){
			dest = $("#menuPrincipalAccueil a").eq(k).attr('href');
			document.location.href = dest;
		});
	});
	
	$("#menuPrincipalAccueil a").each(function(k){$(this).mouseenter(function(){		
		
		l = $(window).width(); h = $(window).height();
		x = $("#global").offset().left; y = $("#global").offset().top;
		x1 = 7+392; x2 = 7+788; y1 = 131; y2 = 136+244;
		x1 += x; x2 += x; y1 += y; y2 += y;
		h1 = y2-y1; h2 = 163;
		l1 = x1; l2 = x2-x1; l3 = l-x2;
		
		$(".hoverAccueil").eq(0).css({"left":"0px", "top":y1+"px", "width":l1+"px", "height":h1+"px"});
		$(".hoverAccueil").eq(1).css({"left":x1+"px", "top":y1+"px", "width":l2+"px", "height":h1+"px"});
		$(".hoverAccueil").eq(2).css({"left":"0px", "top":y2+"px", "width":l1+"px", "height":h2+"px"});
		$(".hoverAccueil").eq(3).css({"left":x1+"px", "top":y2+"px", "width":l2+"px", "height":h2+"px"});
		$(".hoverAccueil").eq(4).css({"left":x2+"px", "top":y1+"px", "width":l3+"px", "height":h1+"px"});
		$(".hoverAccueil").eq(5).css({"left":x2+"px", "top":y2+"px", "width":l3+"px", "height":h2+"px"});		
		
		$(".hoverAccueil").eq(k).fadeTo(100, 0.2);
		
	});});*/
}


function creerBarreGrise()
{
	$("body").append("<div id='barreGrise'></div>");
	$("body").append("<div id='barreGriseGlobal'></div>");
}


var isMenuDynamiqueOuvert = false;
var dureeSlide = 300;
function menuDynamique()
{
	if (nomPage == "accueil") dureeSlide=0;
	
	$("#menuDynamique").hide();	
	
	$("#menuDynamique a.big:eq(1)").css("width", "278px");	
	$("#menuDynamique a.big:eq(2)").css("width", "180px");	
	$("#menuDynamique a.big:eq(3)").css("width", "308px");	
	
	$("body").append("<img src='"+IMGPATH+"btnMenuDynamique.gif' id='btnMenuDynamique'/>");
	$("#btnMenuDynamique").click(function(){
		if (!isMenuDynamiqueOuvert)
		{			
			if (nomPage == "accueil") $("body").css("backgroundImage", "url("+IMGPATH+"ciel.jpg)");			
			$("body").css("overflow", "hidden");
			$("#itemGrisSelected").hide();
			$("#btnMenuDynamique").hide().attr("src", IMGPATH+"btnRetour.gif");
			x = $("#global").offset().left;
			$("#global").animate({left:'-=3000'}, dureeSlide, function(){});
			$("#menuDynamique").show().css("left", (x + 3000)+"px").animate({left:'-=3000'}, dureeSlide, function(){				
				isMenuDynamiqueOuvert = true;								
				$("#btnMenuDynamique").show();
			});
		}
		else
		{			
			$("#btnMenuDynamique").hide().attr("src", IMGPATH+"btnMenuDynamique.gif");;
			$("#global").animate({left:'+=3000'}, dureeSlide, function(){});			
			$("#menuDynamique").animate({left:'+=3000'}, dureeSlide, function(){				
				$("#menuDynamique").css("left", 0).hide();
				$("body").css("overflow", "auto");
				isMenuDynamiqueOuvert = false;
				$("#btnMenuDynamique, #itemGrisSelected").show();
				if (nomPage == "accueil") $("body").css("backgroundImage", "url("+IMGPATH+"cielAccueil.jpg)");			
			});
			
		}
	});
	
	// Positionnement des items
	x1 = 350; x2 = 650;
	y1 = 40; y2 = 110; y3 = 200; y4 = 252; y5 = 299; y6 = 346; 
	
	$("#menuDynamique a:eq(1)").css("left", x1+"px").css("top", y1+"px");
	$("#menuDynamique a:eq(2)").css("left", x2+"px").css("top", y1+"px");
	$("#menuDynamique a:eq(3)").css("left", x1+"px").css("top", y2+"px");
	$("#menuDynamique a:eq(4)").css("left", x2+"px").css("top", y2+"px");
		
	$("#menuDynamique a:eq(5)").css("left", x1+"px").css("top", y3+"px");
	$("#menuDynamique a:eq(6)").css("left", x1+"px").css("top", y4+"px");
	$("#menuDynamique a:eq(7)").css("left", x1+"px").css("top", y5+"px");
	$("#menuDynamique a:eq(8)").css("left", x1+"px").css("top", y6+"px");
	
	$("#menuDynamique a:eq(9)").css("left", x2+"px").css("top", y3+"px");
	$("#menuDynamique a:eq(10)").css("left", x2+"px").css("top", y4+"px");
	$("#menuDynamique a:eq(11)").css("left", x2+"px").css("top", y5+"px");
	$("#menuDynamique a:eq(12)").css("left", x2+"px").css("top", y6+"px");
	
	$("#menuDynamique a:eq(1) b").css("color", "#D1453D");
	$("#menuDynamique a:eq(3) b").css("color", "#555EA2");	
	$("#menuDynamique a:eq(4) b").css("color", "#7FA448");	
	$("#menuDynamique a:eq(2) b").css("color", "#7E5A89");	
}


function pageAtelier()
{
	$("#contenuGlissant").css("line-height", "18px");
}


function gestionSousMenu()
{
	// Ajout des barres de couleur et des titres en couleur
	coul = new Array("#754D80", "#384C98", "#8BAC4A", "#B36795", "#C8121F");
	$("#sousMenu a").not(".url").each(function(k){
		$(this).prepend("<img class='barre' src='"+IMGPATH+"barre"+k+".gif' />");
		$(this).children(".titreEnCouleur").css("color", coul[k]);
	});
	
	// Détection de l'item courant, s'il y en a un
	$("#sousMenu a").each(function(){
		if ($(this).attr('href') == "/"+nomPage) $(this).addClass('selected');
	});
	
	// Création des bandes hover et selected
	$("body").append("<div id='itemGrisHover'></div><div id='itemGrisSelected'></div>");
	$("#itemGrisHover, #itemGrisSelected").hide();
	
	// Gestion des hovers
	$("#sousMenu a").mouseenter(function(){
		pos = $(this).children("img").offset();		
		$("#itemGrisHover").css("width", (pos.left+5)+"px").css("top", (pos.top)+"px").show();
	}).mouseleave(function(){
		$("#itemGrisHover").hide();
	});	
}


var intervalSlide = 5000;
var dureeTransition = 400;
var dureeTransitionCourte = 100;
var iPhoto = 0;
var interv;
function diapo()
{
	nbPhotosAcharger = $("#blocDiapo a").size();
	if (nbPhotosAcharger > 0)
	{
		// On a plusieurs photos
		// La première uniquement s'est chargée avec la page et la page est affichée
		// Maintenant on va précharger les autres photos puis lancer le diaporama
		$("#blocDiapo a").each(function(k){
			src = $(this).attr('href');
			$(this).remove();
			$("#blocDiapo").append("<img src='"+src+"' />");
			if (k == nbPhotosAcharger-1) $("#blocDiapo img:last").one("load", function(){
				$("#blocDiapo img").not(":first").hide();
				interv = setInterval("photoSuivante();", intervalSlide);
			});
		});
		
		// Ajout des flèches de contrôle
		$("#global").append("<img src='"+IMGPATH+"miniFlecheGauche.gif' id='flecheGaucheDiapo' />");
		$("#global").append("<img src='"+IMGPATH+"miniFlecheDroite.gif' id='flecheDroiteDiapo' />");
			
		$("#flecheGaucheDiapo").click(function(){dureeTransition = dureeTransitionCourte; clearInterval(interv); photoPrecedente();});
		$("#flecheDroiteDiapo").click(function(){dureeTransition = dureeTransitionCourte; clearInterval(interv); photoSuivante();});
		
		// Ajout de la bichromie pour la page agence graphique
		if (nomPage == 'lagence-graphique') $("#global").append("<img src='' id='bichromie'/>");
		majMiniFleches();
	}
}


function photoPrecedente()
{
	$("#blocDiapo img:eq("+iPhoto+")").hide();
	iPhoto--;
	if (iPhoto == -1) iPhoto = $("#blocDiapo img").size()-1;	
	$("#blocDiapo img:eq("+iPhoto+")").fadeIn(dureeTransition);	
	majMiniFleches();	
}


function photoSuivante()
{
	$("#blocDiapo img:eq("+iPhoto+")").hide();	
	iPhoto++;
	if (iPhoto == $("#blocDiapo img").size()) iPhoto = 0;	
	$("#blocDiapo img:eq("+iPhoto+")").fadeIn(dureeTransition);	
	majMiniFleches();	
}


function majMiniFleches()
{
	if (iPhoto == 0) $("#flecheGaucheDiapo").hide(); else $("#flecheGaucheDiapo").show();
	if (iPhoto == $("#blocDiapo img").size()-1) $("#flecheDroiteDiapo").hide(); else $("#flecheDroiteDiapo").show();	
	afficherBichromie();
}


function afficherBichromie()
{
	src = $("#blocDiapo img:eq("+iPhoto+")").attr('src');
	p = src.lastIndexOf('.');
	srcBichromie = src.substr(0, p) + ".gif";	
	$("#bichromie").attr('src', srcBichromie);
}


function pageExemplesWeb()
{	
	nb = $(".site").size();
	$(".site").each(function(k){
		$(this).css("zIndex", nb-k);
	});
	
	$(".content").each(function(k){
		w = $(".screenshot").eq(k).width();		
		$(this).css("width", w+"px").css("marginLeft", ((500-w)>>1)+"px");
	});
	
	$("#siteSuiv").click(function(){				
		incrementerZindex();
		c=0; while ((!isZindexMaxVisible()) && c < 20)
		{
			incrementerZindex();
			c++;
		}
	});
	
	$("#sitePrec").click(function(){
		decrementerZindex();
		c=0; while ((!isZindexMaxVisible()) && c < 20)
		{
			decrementerZindex();
			c++;
		}
	});
	
	$("#filtresExemplesWeb input").attr('checked', 'true');
	
	$("#filtresExemplesWeb input").change(function(){
		if ($("#b2c").attr('checked') == true) $(".site.b2c").show(); else $(".site.b2c").hide();
		if ($("#b2b").attr('checked') == true) $(".site.b2b").show(); else $(".site.b2b").hide();
		if ($("#particulier").attr('checked') == true) $(".site.particulier").show(); else $(".site.particulier").hide();
	});	
}


function isZindexMaxVisible()
{
	ret = false;
	
	zMax = 0;
	$(".site").each(function(){
		z = $(this).css("zIndex");		
		if ((1.0*z) > (1.0*zMax)) zMax = 1.0*z;		
	});	
	
	$(".site").each(function(){
		z = $(this).css("zIndex");
		if ((1.0*z) == zMax) ret = $(this).is(':visible');
	});
	
	return ret;
}

function incrementerZindex()
{
	zMax = 0;
	$(".site").each(function(){
		z = $(this).css("zIndex");
		znew = (1.0*z)+1;
		$(this).css("zIndex", znew);
		if (znew > zMax) zMax = znew;
	});
	
	$(".site").each(function(){
		z = $(this).css("zIndex");
		if (z == zMax) $(this).css("zIndex", 1);
	});
}


function decrementerZindex()
{
	zMax = 0;
	$(".site").each(function(){
		z = $(this).css("zIndex");		
		if ((1.0*z) > zMax) zMax = z;
		znew = (1.0*z - 1);
		$(this).css("zIndex", znew);		
	});	
	
	$(".site").each(function(){
		z = $(this).css("zIndex");
		if (z == "0") $(this).css("zIndex", zMax);
	});
}


function pageEquipe()
{
	$("h2").css("left", "160px");
	
	$("#julien2, #julientexte2, #marion2, #mariontexte2, #wilfrid2, #wilfridtexte2, #pierre2, #pierretexte2").hide();
	
	$("#pierrelien, #marionlien").appendTo("#global");
	
	$("#marion1, #marion2, #mariontexte1, #mariontexte2").click(function(){window.open($("#marionlien").attr('href'));});
	$("#pierre1, #pierre2, #pierretexte1, #pierretexte2").click(function(){window.open($("#pierrelien").attr('href'));});
	
	$("#pierrelien").mouseenter(function(){
		$("#pierre1, #pierretexte1").hide();
		$("#pierre2, #pierretexte2").show();
		$("#pierrelien").css("textDecoration", "underline");
	}).mouseleave(function(){
		$("#pierre1, #pierretexte1").show();
		$("#pierre2, #pierretexte2").hide();
		$("#pierrelien").css("textDecoration", "none");
	});
	
	$("#marionlien").mouseenter(function(){
		$("#marion1, #mariontexte1").hide();
		$("#marion2, #mariontexte2").show();
		$("#marionlien").css("textDecoration", "underline");
	}).mouseleave(function(){
		$("#marion1, #mariontexte1").show();
		$("#marion2, #mariontexte2").hide();
		$("#marionlien").css("textDecoration", "none");
	});
	
	$("#blocEquipe").mousemove(function(e){
	
		var limiteY = 150;
		var plateau = 20;
			
		x = e.pageX; y = e.pageY;
		x -= $(this).offset().left;
		y -= $(this).offset().top;		
		if (y > limiteY)
		{			
			dj = Math.abs(240-x)-plateau; if (dj > 100) dj = 100; if (dj < 0) dj = 0;
			dm = Math.abs(395-x)-plateau; if (dm > 100) dm = 100; if (dm < 0) dm = 0;
			dw = Math.abs(560-x)-plateau; if (dw > 100) dw = 100; if (dw < 0) dw = 0;
			dp = Math.abs(740-x)-plateau; if (dp > 100) dp = 100; if (dp < 0) dp = 0;
			
			if (dj > 50) {
				$("#julien1, #julientexte1").show();
				$("#julien2, #julientexte2").hide();
			}
			else
			{
				$("#julien1, #julientexte1").hide();
				$("#julien2, #julientexte2").show();
			}
			
			if (dm > 50) {
				$("#marion1, #mariontexte1").show();
				$("#marion2, #mariontexte2").hide();
				$("#marionlien").css("textDecoration", "none");
			}
			else
			{
				$("#marion1, #mariontexte1").hide();
				$("#marion2, #mariontexte2").show();
				$("#marionlien").css("textDecoration", "underline");
			}
			
			if (dw > 50) {
				$("#wilfrid1, #wilfridtexte1").show();
				$("#wilfrid2, #wilfridtexte2").hide();
			}
			else
			{
				$("#wilfrid1, #wilfridtexte1").hide();
				$("#wilfrid2, #wilfridtexte2").show();
			}
			
			if (dp > 50) {
				$("#pierre1, #pierretexte1").show();
				$("#pierre2, #pierretexte2").hide();
				$("#pierrelien").css("textDecoration", "none");
			}
			else
			{
				$("#pierre1, #pierretexte1").hide();
				$("#pierre2, #pierretexte2").show();
				$("#pierrelien").css("textDecoration", "underline");
			}
		}
		else 
		{			
			$("#julien1, #julientexte1, #marion1, #mariontexte1, #wilfrid1, #wilfridtexte1, #pierre1, #pierretexte1").show();
			$("#julien2, #julientexte2, #marion2, #mariontexte2, #wilfrid2, #wilfridtexte2, #pierre2, #pierretexte2").hide();
		}
	}).mouseleave(function(){
		$("#julien1, #julientexte1, #marion1, #mariontexte1, #wilfrid1, #wilfridtexte1, #pierre1, #pierretexte1").show();
		$("#julien2, #julientexte2, #marion2, #mariontexte2, #wilfrid2, #wilfridtexte2, #pierre2, #pierretexte2").hide();
	});
}


function pageTarifs()
{
	$("#grilleTarifaire tr").each(function(k){		
		o = $(this).children("td");
		if (k > 0) o.first().css("textAlign", "right").css("fontWeight", "bold").css("backgroundColor", "#DCE4EA").css("paddingRight", "5px");
		o.eq(5).css("backgroundColor", "#A6B7C9");
		o.eq(4).css("backgroundColor", "#B8C6D5");
		o.eq(3).css("backgroundColor", "#C6D2DD");
		o.eq(2).css("backgroundColor", "#D2DCE5");
		o.eq(1).css("backgroundColor", "#DCE4EA");
	});
	
	$("#grilleTarifaire td").each(function(){
		if ($(this).text() == "oui") $(this).html("<img src='"+IMGPATH+"oui.gif' />");
		if ($(this).text() == "non") $(this).html("<img src='"+IMGPATH+"non.gif' />");
	});
}


function actu()
{
	if ($(".uneActu").size() > 0)
	{
		$("h2").hide();
		src = $("#contenuGlissant img:last").attr('src');
		$("#contenuGlissant img:last").remove();
		$("#blocDiapo img:first").attr('src', src);
	}
}


function pageContact()
{
	var myLatlng = new google.maps.LatLng(46.070774,6.316859);
	var myOptions = {
		zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.HYBRID};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);		
    var myLatLng = new google.maps.LatLng(46.070774,6.316859);
    var beachMarker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        title:"APPARENCE"
    });
}


// Pour la partir telechargement
function msgClic()
{
	alert("Veuillez utiliser la fonction 'clic droit / enregistrer sous' pour télécharger les fichiers.");
	return false;
}


// Post-it : Demande de devis
function devisGratuit()
{
	$("#blocPrecisez, #blocStructure").hide();
	
	if (nomPage == "accueil") 
	{
		$("#menuDynamique").append("<img src='"+IMGPATH+"devis_gratuit.png' id='btnDevis' />");
		$("#btnDevis").css("top", "-20px").css("left", "950px");
	}
	else $("#global").append("<img src='"+IMGPATH+"devis_gratuit.png' id='btnDevis' />");
	
	$("#btnDevis").click(function(){
		
		if (nomPage == "accueil")  $("#menuDynamique").animate({left:'-=3000'}, 350, function(){});
		else $("#global").animate({left:'-=3000'}, 350, function(){});
		
		$("#btnMenuDynamique, #itemGrisSelected, #itemGrisHover").hide();
				
		l = $(window).width();
		xDevis = (l - $("#votreDevis").width()) >> 1;
		xDevis += 3000;
		yDevis = $("h2#titrePage").offset().top;
		$("#votreDevis").css("left", xDevis+"px").css("top", yDevis+"px").show().animate({left:'-=3000'}, 350, function(){
			$("#btnMenuDynamique, #itemGrisSelected, #itemGrisHover").hide();
		});		
		
		$("#votreDevis").prepend("<img src='"+IMG_PATH+"retSite.png' id='fermerDevis'/>");
		$("#fermerDevis").click(function(){
			window.history.go(0);
		});
	});
	
	$("select[name=projet]").change(function(){
		proj = $(this).attr('value');
		if (proj == "autre") $("#blocPrecisez").show(); else $("#blocPrecisez").hide();
	});
	
	$("select[name=typeClient]").change(function(){
		typ = $(this).attr('value');
		if (typ == "entreprise" || typ == "association") $("#blocStructure").show(); else $("#blocStructure").hide();
	});
	
	$("#validationDevis").click(function(){
		
		pro = $("select[name=projet]").attr("value");
		typ = $("select[name=typeClient]").attr("value");
		nomm = $("input[name=nom]").attr("value");
		pren = $("input[name=prenom]").attr("value");
		cod = $("input[name=codePostal]").attr("value");
		tell = $("input[name=tel]").attr("value");
		
		if (pro=="" || typ=="" || nomm=="" || pren=="" || cod=="" || tell=="") alert("Veuillez remplir tous les champs !");
		else $("form").submit();
	});
}

// Gestion de l'accordéon
function pageMetiersComm()
{
	$("#contenuGlissant h3").css("cursor", "pointer");
	$("#contenuGlissant h3").mouseenter(function(){
		$(this).css("backgroundColor", "#E9E9E9");
	}).mouseleave(function(){
		$(this).css("backgroundColor", "white");
	});	
	
	$("#contenuGlissant h3").addClass("titreAccordeon").each(function(k){
		$(this).after("<div class='blocAccordeon'></div>");
		group = $(".blocAccordeon:eq("+k+")");
		obj = group.next();
			
		pasFini = true;
		while (obj.parent().attr('id') == "contenuGlissant" && pasFini)
		{
			objSuivant = obj.next();
			if (obj.hasClass("titreAccordeon")) pasFini = false;
			else obj.appendTo(group);			
			obj = objSuivant;
		}
	});
	
	$(".titreAccordeon").click(function(){
		
		if ($(this).hasClass("develop"))
		{
			$(this).removeClass("develop");
			$(this).next().hide(400);
		}
		else
		{
			$(".titreAccordeon").removeClass("develop");
			obj = $(this);
			obj.addClass("develop");
			$(".blocAccordeon").hide(400);
			obj.next().show(400);
		}		
	});
}

