// JavaScript Document


/*
					
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
	jQuery("<img>").attr("src", arguments[i]);
  }
}


$.preloadImages("img/bandeau/baptiste-macaire-graphiste-1.jpg", "img/bandeau/baptiste-macaire-graphiste-2.jpg", "img/bandeau/baptiste-macaire-graphiste-3.jpg");

*/


/*	
	$.ajax({ 
		type: "POST",
		url: "./script/ajout2.php",
		cache: false,
		data: {nom: nom, image: image, le: v_left, v_top: v_top },
		dataType: "script",
		error: function( XMLHttpRequest, textStatuts, errorThrown ){ alert(textStatus); },
		success: function(bad) {
			
		}
	});
*/
			
			
function resize_inter_h () {

/*
	//DIMENSION DU STAGE
	var stage_w = $(window).width();
	var stage_h = $(window).height();
	//alert(stage_w + " | " + stage_h);
	
	var min_stage_h = (parseInt(137) + parseInt(545) + parseInt(110));
	//alert(min_stage_h);
	
	var header_min_h = 100;
	if(stage_h > min_stage_h){
	
		var inter_h = ( stage_h - min_stage_h ) / 2;
		var header_h =( $("#header").height() + inter_h )
		
		//alert("header retrici de : inter_h : " + inter_h + "ce qui donne h_header = " + header_h);
		
		if(header_h > header_min_h){
			$("#header").css("height", header_h + "px");
		} else {
			$("#header").css("height", "137px");
		}
	} else if (stage_h < min_stage_h){
	
		var inter_h = ( min_stage_h - stage_h ) / 2;
		var header_h =( $("#header").height() - inter_h);
		
		//alert("header sagrandit de : inter_h : " + inter_h + "ce qui donne h_header = " + header_h);
		
		$("#header").css("height", header_h + "px");
		
	} else {
	
		$("#header").css("height", "137px");
		
	}
	
	*/
}

	
	
function f_adapte (){
	
	//alert("f_adapte");
	
	// DIMENSION DU STAGE
	var stage_w = $(window).width();
	var stage_h = $(window).height();
	
	//alert(stage_w + " " + stage_h);
	
	
	// CALCUL DE LA HAUTEUR
	var page_actuel = f_recup_page_actuel();
	
	/*
	if(page_actuel == "home" || page_actuel == ""){
		var page_h = (164+30+164);
	}else{
		var page_h = 600;
	}
	*/
	
	if( stage_h < 800 ){
		
		var nb = 8;
		//$(document).find("div#header").css("height", "90px");
		$(document).find("div#header").css("height", "80px");
		
		if(page_actuel == "home" || page_actuel == ""){
			
		}
		/*
		//$(document).find("div#header").css("margin-bottom", "90px");
		alert("là");
		
	} else if ( stage_h < 800  ){
		
		var header_h = 80 + ( (stage_h - (80 + (page_h) + 100)) / 2 );
		//$(document).find("div#header").css("height", header_h+"px");
		$(document).find("div#header").css("height", "80px");
		//$(document).find("div#header").css("margin-bottom", header_h"px");
		alert("là2");
		var nb = 8;
		*/
	
	}else{
		
		var nb = 12;
		var header_h = 137 + ( (stage_h - (140 + 600 + 100)) / 2 );
		$(document).find("div#header").css("height", header_h+"px");
	
	}
	
	//alert("f_adapt nb : " + nb + ". header_h : " + header_h);
	
	$(document).find("div#nbobjet").text(nb);
	return nb;
	

}
	

function f_adapte_8 (){
	
	//alert("f_adapte_8");
	
	// DIMENSION DU STAGE
	var stage_w = $(window).width();
	var stage_h = $(window).height();
	
	var header_h = ((stage_h - (80 + (164 + 30 + 164) + 100)) / 2);
	//alert("margin-top : " + header_h);
	$(document).find("div#slide").css("margin-top", header_h+"px");
}
	


function f_ascenseur (page){
	
	var stage_h = $(window).height();
	//alert(stage_h);
	
	var nbobjet = $(document).find("div#nbobjet").text();
	
	if(stage_h < 700){
		var stage_h = $(window).height();
		var header_h = (stage_h - (90 + 100));
		
		$(document).find("."+page).find("div.ascenseur").css("height", header_h+"px");
		$(document).find("."+page).find("div.ascenseur").css("overflow-y", "scroll");
		$(document).find("."+page).find("div.ascenseur").css("overflow-x", "hidden");
	
	
	} else {
		$(document).find("."+page).find("div.ascenseur").css("height", "530px");
		$(document).find("."+page).find("div.ascenseur").css("overflow-y", "hidden");
		$(document).find("."+page).find("div.ascenseur").css("overflow-x", "hidden");
	}
	
	//alert("f_ascenseur " + page + "  " + nbobjet + "hauteur du scroll : " + header_h);
	
}
	
	
	
	
/* CLICK SUR GALERIE SUIVANTE */

function f_galerie_suiv (num) {

		/*
		$(document).find("#slide").animate({"left": "-1000px"}, "slow", function(){
				$(document).find("#page").load("script/page/slide.php", {num: num});
		});
		*/
		
		var nbobjet = $(document).find("div#nbobjet").text();
		
		$(document).find("#slide").slideUp("500", function(){
				
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				$(document).find("#page").load("script/page/slide.php", {num: num, nbobjet: nbobjet}, function(){
							
							if( $("div#page").find("div.charge") ){
								$(document).find("div.charge").removeClass("charge");
							}	
							
							$(document).find("#slide").slideDown("1000");										 
				});
		});
}
	

/* CLICK SUR GALERIE PRECEDENTE */

function f_galerie_prec (num) {
		
		/*
		$(document).find("#slide").animate({"left": "1000px"}, "slow", function(){
				$(document).find("#page").load("script/page/slide.php", {num: num});
		});
		*/
		
		var nbobjet = $(document).find("div#nbobjet").text();
		
		$(document).find("#slide").slideUp("slow", function(){
				
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				$(document).find("#page").load("script/page/slide.php", {num: num, nbobjet: nbobjet}, function(){
							
							if( $("div#page").find("div.charge") ){
								$(document).find("div.charge").removeClass("charge");
							}	
							
							$(document).find("#slide").slideDown("1000");										 
				});
		});
		
		
}



/* LANCEMENT DES PAGES */
		
function f_home(num, nbobjet_test){
	
	var page_actuel = f_recup_page_actuel();
	
	if(page_actuel == "works"){
		enlev_bras();
	}
	

	if(nbobjet_test == "non"){	// SI ON NE CONNAIT PAS nbobjet
	
		var nbobjet = $(document).find("div#nbobjet").text();
	
	}else{					// SINON CA SIGNIFI QU'IL FAUT RECHARGER LA PAGE
	
		var nbobjet = nbobjet_test;
	
	}
	
	// ON CHARGE HOME QUE SI ON Y EST PAS DEJA 
	// OU QUE L'UTILISATEUR A RESIZER LA PAGE (AUQUEL CAS nbobjet_test EST DIFFERENT DE NON)
	if(page_actuel != "home" || nbobjet_test != "non"){
		
		$(document).find("div#page").fadeOut("fast", function(){
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				$(document).find("div#page").removeClass(page_actuel);
				$(document).find("div#page").addClass("home");
		
				$(document).find("div#page").load("script/page/slide.php", {num: num, nbobjet: nbobjet}, function(){
				
						$(document).queue(function(){
												   
								$(document).find("div#page_actuel").text("home");
								
								if( $("div#page").find("div.charge") ){
										$(document).find("div.charge").removeClass("charge");
								}	
								
								$(document).find("div#page").fadeIn("slow", function(){
								
									$(document).find("#footer_fleche").stop();
									$(document).find("#footer_fleche").animate({"left": "70px", "bottom": "0px"}, "slow");
								
								});
								$(this).dequeue();
						});
				
				});
		});
		//$(document).find("div#page").html("HOME " + num + "<div id='slide'><div class='galerie_suivante'></div></div>");
		
	} else {
		
		$("div#wrapper").find("div#onglet").html("Vous y &ecirc;tes d&eacute;j&agrave; !");
		
		var offset = $("div#menu").find("a[title="+page_actuel+"]").offset();
		//alert( "left: " + offset.left + ", top: " + offset.top + " " + page_actuel);
		var o_left = (offset.left - (125/2) + (85/2));
		var o_top = (offset.top - 39);
		
		$("div#wrapper").find("div#onglet").css("left", o_left+"px");
		$("div#wrapper").find("div#onglet").css("top", o_top+"px");
		
		if( $("div#wrapper").find("div#onglet").css("display") == "none" ){
			
			$("div#wrapper").find("div#onglet").slideDown("fast");
			$(this).a = setTimeout(onglet_out,500);
			
		}
		
	}
}


function onglet_out(){
	//alert(this);
	$("div#wrapper").find("div#onglet").slideUp("fast");
	
}

	

function f_works(num){
	
	var page_actuel = f_recup_page_actuel();
	
	if(page_actuel != "works"){
		
		
		$(document).find("div#page").fadeOut("fast", function(){
															  
		
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				
		
			
						$(document).find("div#page").removeClass(page_actuel);
						$(document).find("div#page").addClass("works");
						
						$(document).find("div#page").load("script/page/works.php", {num: num}, function () {
									//$(document).find("div#page").text("WORKS " + num);
									
									$(document).find("#bras_suiv").addClass("bras_suivant");
									$(document).find("#bras_prec").addClass("bras_precedent");
									
									// DIMENSION DU STAGE
									var stage_w = $(window).width();
									var stage_h = $(window).height();
									
									// DIMENSIONS DES BRAS (ICI #BRAS_PREC EST PRIS EN EXEMPLE)
									var bras_w = $(document).find("#bras_prec").width();
									var bras_h = $(document).find("#bras_prec").height();
									
									// CALCUL DE LA POSITION EN X DE BRAS SUIVANT => PAS BESOIN CAR = -222
									//var bras_suiv_left = (stage_w-78);
									
									// CALCUL DE LA POSITION EN X DE BRAS PRECEDENT
									var bras_prec_left = -(bras_w-78);
									
									//CALCUL DE LA POSITION EN Y DES DEUX BRAS
									var bras_top = (stage_h/2)-(bras_h/2);
									
									$(document).queue(function(){
											$(this).find("#bras").show();
											$(this).find("#bras_suiv").animate({marginTop: bras_top+"px", marginRight: "-222px"});
											$(this).find("#bras_prec").animate({"top": bras_top+"px", "left": "-222px"}, "slow");
											$(this).dequeue();
									});
									
									$(document).queue(function(){
											
											$(document).find("div#page_actuel").text("works");
											
											if( $("div#page").find("div.charge") ){
													$(document).find("div.charge").removeClass("charge");
											}
											
											$(document).find("div#page").fadeIn("slow", function(){
											
													$(document).find("#footer_fleche").stop();
													$(document).find("#footer_fleche").animate({"left": "190px", "bottom": "0px"}, "slow");
											
											});
											$(this).dequeue();
									});
						});
		
		
		});
		
		
	} else {
		
		$("div#wrapper").find("div#onglet").html("Vous y &ecirc;tes d&eacute;j&agrave; !");
		
		var offset = $("div#menu").find("a[title="+page_actuel+"]").offset();
		//alert( "left: " + offset.left + ", top: " + offset.top );
		var o_left = (offset.left - (125/2) + (85/2));
		var o_top = (offset.top - 39);
		
		$("div#wrapper").find("div#onglet").css("left", o_left+"px");
		$("div#wrapper").find("div#onglet").css("top", o_top+"px");
		
		if( $("div#wrapper").find("div#onglet").css("display") == "none" ){
			
			$("div#wrapper").find("div#onglet").slideDown("fast");
			$(this).a = setTimeout(onglet_out,500);
			
		}
		
	}
}





function f_projet(traj){
	
	//charge
	var cible = $(document).find("div#content");
	charge(cible);
	
	var num = $(document).find("div#num_id").text();
	
	var num_total = $(document).find("div#num_id").attr("title");
	
	if(traj == "suiv" && num == num_total){
		
		var suiv = 1;
		var pos_fin = "-1500";
		var pos_debut = "1500";
		
	}else if(traj == "suiv"){
		
		var suiv = parseInt(num) +1;
		var pos_fin = "-1500";
		var pos_debut = "1500";
		
	}else if (traj == "prec" && num == 1){
		
		var suiv = num_total;
		var pos_fin = "1500";
		var pos_debut = "-1500";
		
	}else if (traj == "prec"){
		
		var suiv = parseInt(num) -1;
		var pos_fin = "1500";
		var pos_debut = "-1500";
		
	}
	
	
	$(document).find("#slide").animate({"left": pos_fin+"px"}, "fast", function(){
			$(document).find("#slide").hide();
			$(document).find("#page").load("script/page/works.php", {num: suiv}, function(){
					$(document).find("#slide").css("left", pos_debut+"px");
					$(document).find("#slide").show();
					$(document).find("#slide").animate({"left": "0px"}, "fast", function(){
																						 
							if( $("div#page").find("div.charge") ){
								$(document).find("div.charge").removeClass("charge");
							}
					});															  
																						  
			});
			
	});
	
	
}


function charge(cible){
	
	if(!cible.find("div").hasClass("charge")){
		
		cible.queue(function(){
				$(this).append("<div class='charge'> </div>");
				$(this).dequeue();
		});
		var cible_h = parseInt( cible.css("height") );
		var cible_top = (cible_h/2) - 14; // 14px etant la moitié de la hauteur du gif
		
		var cible_w = parseInt( cible.css("width") );
		var cible_left = (cible_w/2) - 14; // 14px etant la moitié de la largeur du gif
		
		//alert(cible_top + " " + cible_left);
		cible.find("div.charge").css({"top": cible_top+"px", "left": cible_left+"px"});
	}
}



function f_about(){
	
	var page_actuel = f_recup_page_actuel();
	
	if(page_actuel == "works"){
		enlev_bras();
	}
	
	if(page_actuel != "about"){
		
		$(document).find("div#page").fadeOut("fast", function(){
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				
				
				
				$(document).find("div#page").removeClass(page_actuel);
				$(document).find("div#page").addClass("about");
				
				$(document).find("div#page").load("script/page/about.php", function () { 
						
						f_ascenseur("about");
						
						$(document).queue(function(){
								
								$(document).find("div#page_actuel").text("about");
								
								if( $("div#page").find("div.charge") ){
										$(document).find("div.charge").removeClass("charge");
								}	
								
								$(document).find("div#page").fadeIn("slow", function(){
																					 
										$(document).find("#footer_fleche").stop();
										$(document).find("#footer_fleche").animate({"left": "310px", "bottom": "0px"}, "slow");
								
								});
								$(this).dequeue();
						});
					
				});
		
		});
		
		
		
	} else {
	
		$("div#wrapper").find("div#onglet").html("Vous y &ecirc;tes d&eacute;j&agrave; !");
		
		var offset = $("div#menu").find("a[title="+page_actuel+"]").offset();
		//alert( "left: " + offset.left + ", top: " + offset.top );
		var o_left = (offset.left - (125/2) + (85/2));
		var o_top = (offset.top - 39);
		
		$("div#wrapper").find("div#onglet").css("left", o_left+"px");
		$("div#wrapper").find("div#onglet").css("top", o_top+"px");
		
		if( $("div#wrapper").find("div#onglet").css("display") == "none" ){
			
			$("div#wrapper").find("div#onglet").slideDown("fast");
			$(this).a = setTimeout(onglet_out,500);
			
		}
	
	}
}

function f_contact(){

	var page_actuel = f_recup_page_actuel();
	
	if(page_actuel == "works"){
		enlev_bras();
	}
	
	if(page_actuel != "contact"){
		
		$(document).find("div#page").fadeOut("fast", function(){
		
				// charge
				var cible = $(document).find("div#content");
				charge(cible);
				
				
				$(document).find("div#page").removeClass(page_actuel);
				$(document).find("div#page").addClass("contact");
				
				$(document).find("div#page").load("script/page/contact.php", function () {
					
					f_ascenseur("contact");
					
					$(document).find("div#conteneur").css("top", "0px");
					
					$(document).queue(function(){
											   
							$(document).find("div#page_actuel").text("contact");
							
							if( $("div#page").find("div.charge") ){
									$(document).find("div.charge").removeClass("charge");
							}	
							
							$(document).find("div#page").fadeIn("slow", function(){
							
									$(document).find("#footer_fleche").stop();
									$(document).find("#footer_fleche").animate({"bottom": "-20px"}, "slow");
							
							});
							$(this).dequeue();
					});
					
					intval = setInterval ( "defile_img()", 6000 );
				
				 });
		
		});
		
		
		
	} else {
	
	
	$("div#wrapper").find("div#onglet").html("Vous y &ecirc;tes d&eacute;j&agrave; !");
		
		var offset = $("div#header").find("a[title="+page_actuel+"]").offset();
		//alert( "left: " + offset.left + ", top: " + offset.top );
		var o_left = (offset.left - (125/2) + (192/2));
		var o_top = (offset.top + 60);
		
		$("div#wrapper").find("div#onglet").css("left", o_left+"px");
		$("div#wrapper").find("div#onglet").css("top", o_top+"px");
		
		if( $("div#wrapper").find("div#onglet").css("display") == "none" ){
			
			$("div#wrapper").find("div#onglet").slideDown("fast");
			$(this).a = setTimeout(onglet_out,500);
			
		}
	}
}






function f_recup_page_actuel() {
	var val = $(document).find("div#page_actuel").text();
	return val;
}
	
	
	
	
function enlev_bras() {

	$(document).find("#bras_suiv").animate({"marginRight": "-300px"}, 500, function(){
			$(document).find("#bras_prec").animate({"left": "-300px"}, 700, function(){
					$(document).find("div#bras").hide();
			});
	});
	
}






function defile_img(){
	
	if( $(document).find("div#page").hasClass("contact") ){
		var $curr = $(document).find("div#conteneur");
		var top_curr = parseInt( $curr.css("top") );
		var height_img = parseInt( $curr.find("img#vente_img_1").css("height") );
		var nb_image = 3;
		//alert("top_curr : " + top_curr + "     ((nb_image-1)*height_img) : " + ((nb_image-1)*height_img));
		
		if( top_curr >= parseInt(((nb_image-1)*height_img)) ){
			//ALORS ON EST A LA DERNIERE IMAGE
			clearInterval(intval);
			
			$(document).find("div#conteneur").css("top", "170px");
		} else {
			//ON FAIT DEFILER LES IMAGES
			$(document).find("div#conteneur").animate({"top": "+=85px"}, "fast");
		}
	}else{
		//alert("Ce n'est plus la page contact");
		clearInterval(intval);
		
	}
}



// FORMULAIRE DE CONTACT

function f_envoi_form(this_form) {
	
	$(this_form).fadeOut("fast", function (){
			// charge
			var cible = $(document).find("div#content").find("#contact_droite");
			charge(cible);
							  
	
			var nom = $(this_form).find("input#form_nom").val();
			var societe = $(this_form).find("#form_societe").val();
			var email = $(this_form).find("#form_email").val();
			var site = $(this_form).find("#form_site").val();
			var objet = $(this_form).find("#form_obj").val();
			var message = $(this_form).find("#message").val();
			//alert(nom + " " + societe + " " + email + " " + site + " " + objet + " " + message);
			
			
			$.ajax({ 
					type: "POST",
					url: "script/mail/envoi.php",
					cache: false,
					data: {form_nom: nom, form_societe: societe, form_email: email, form_site: site, form_obj: objet, message: message },
					dataType: "script",
					error: function(){
						var erreur_txt = envoi_erreur();
						$(this_form).find("div#reponse").html(erreur_txt);
						$(this_form).find("input#form_nom").val(nom);
						$(this_form).find("#form_societe").val(societe);
						$(this_form).find("#form_email").val(email);
						$(this_form).find("#form_site").val(site);
						$(this_form).find("#form_obj").val(objet);
						$(this_form).find("#message").val(message);
						$(this_form).fadeIn("fast");
						},
					success: function(reponse) {
						//alert(reponse);
						if(reponse == "erreur"){
							
							var erreur_txt = envoi_erreur();
							$(this_form).find("div#reponse").html(erreur_txt);
							
						}else{
							$(this_form).html("<p style='text-align: center;'><br /><br />Merci.<br />Votre email a bien &eacute;t&eacute; envoy&eacute;.</p>");
							
							if( $("div#page").find("div.charge") ){
								$(document).find("div.charge").removeClass("charge");
							}
							
							$(this_form).fadeIn("fast");
						}
					}
			});
	
	});
		
}

function envoi_erreur(){
	var erreur_txt = "<p style='text-align: center; padding-top:0px;'>Erreur lors de l'envoi du message.<br /> S'il vous plait, veuillez recommencer.</p>";
	return erreur_txt;
}








function f_img(id_proj, sens, id_content, nb_total, id_img){

	
	// charge
	var cible = $(document).find("div#vue"+id_content);
	charge(cible);		
							
	
	var img_id = $("div#content").find("div#"+id_content).attr("title");
	//alert(img_id + " " + sens);
	
	if(sens == "prec"){
		
		var nb = parseInt(img_id) - 1;
	
	} else if(sens == "suiv"){
		
		var nb = parseInt(img_id) + 1;
	
	} else if ( sens == "exact"){
	
		var nb = parseInt(id_img);
	
	}
	
	
	if( nb < 1){
	
		nb = nb_total;
	
	} else if ( nb > nb_total ){
	
		nb = 1;
	
	}
	
	$("div#content").find("div#"+id_content).attr("title", nb);
	
	//alert($("div#content").find("div#vue").attr("title"));
	
	$.ajax({ 
		type: "POST",
		url: "script/img_change.php",
		cache: false,
		data: {id_proj: id_proj, nb: nb },
		dataType: "script",
		error: function( XMLHttpRequest, textStatuts, errorThrown ){ },
		success: function(retour) {
			
			var tab = retour.split("|");
			
			var img_nom = tab[0];
			var img_type = tab[1];
			var img_width = tab[2];
			var img_height = tab[3];
			
			//alert(retour);
			
			if( img_type == "13" ){
				
				swfobject.embedSWF("image/works/projet/"+img_nom, id_content+"_img", img_width, img_height, "9.0.0");
				
				if( img_height < 200 ){

					$("div#"+id_content).find("#"+id_content+"_img").css("margin-top", "100px");
					
				}
				
			} else {
				
				//$("div#vue").html("OUAICH");
				$("div#vue").html('<span id="'+id_content+'_img"><img src="image/works/projet/'+img_nom+'" alt="'+img_nom+'" class="cursorCrosshair"></span>');
			
			}
			
			$("div#page").find("ul.nav-img").find("div.selectionne").each(function(){ 
																				   
				$(this).removeClass("selectionne"); 
				
			});
			
			$("div#page").find("ul.nav-img").find("div#img"+nb).addClass("selectionne");
			
			if( $("div#page").find("div.charge") ){
				$(document).find("div.charge").removeClass("charge");
			}	
			
			
			
		}
		
	});
	
}




/*
function f_img(texte, id, cible){
	
	if(!$("img#"+cible).hasClass("selectionne")){
	
		alert(cible + " " + texte);
		
		$(document).find("#bas img").each(function(){
			$(this).removeClass();
		});
		
		$("#"+cible).addClass("selectionne");
		
		$(document).find("#"+id).find("img").attr("src", "image/works/projet/"+texte);
		$(document).find("#"+id).find("img").attr("alt", texte);
		$(document).find("#"+id).find("img").attr("title", cible);
		
	}
}
*/






