var quitter = false;

$(document).ready(function(){
						   
	// -- Creation du fade ---------------> */
	
	/*$("body").append("<div id=\"fade\"></div>");
			
	$("#fade").css({
		'width' : '100%',
		'height' : '100%',
		'background' : '#2a2a2a', //url(../img/lapin-qui-danse.gif) no-repeat center center
		'position' : 'fixed',
		'z-index' : '9999',
		'top' : 0,
		'left' : 0,
		'opacity' : 1,
		'display' : 'block'
	});
	
	unfade();*/
    
	// -- Gestion du menu ----------------> */
	
	//if(window.innerWidth && window.innerWidth > 480){}
	//$('header nav li:not(.actif) a').css("opacity",0);
	
	$('header nav li a').hover(function(){
		$('header nav li a').stop().animate({opacity:0},250);
		$(this).stop().animate({opacity:1},250);;							
	},function(){
		$('header nav li a').stop().animate({opacity:1},250);
	});
	
	
	$('#nav-con a').click(function(e){
		e.preventDefault();
		if($(this).hasClass("active")){
			$('header').stop().animate({marginTop:"-455px"},{duration: 300, easing: 'easeOutQuad'});
			$('#logo').stop().animate({paddingTop:90},{duration: 300, easing: 'easeOutQuad'});
		}else{
			$('header').stop().animate({marginTop:0},{duration: 300, easing: 'easeOutQuad'});
			$('#logo').stop().animate({paddingTop:540},{duration: 300, easing: 'easeOutQuad'});
		}
		$(this).toggleClass("active");
	});	
	
	$('#btn_ecrire').click(function(e){
		e.preventDefault();	
		$("header .perso .nom").css("display","none");
		$('#header-holder').stop().animate({marginLeft:"-714px"},{duration: 500, easing: 'easeOutQuad'});
		$('#contact').css({"text-align":"left", "background-position":"15px 140px"});
		$(this).stop().animate({opacity:0}, 500, 'easeOutQuad', function(){
			$(this).css("display","none");
			$("#ecrire").stop().animate({top:0}, 500, 'easeOutBack');
		});
	});
	
	$('#ecrire-quit').click(function(e){
		e.preventDefault();	
		$("#ecrire").stop().animate({top:"-555px"}, 500, 'easeInBack', function(){					
			$("header .perso .nom").css("display","block");
			$("#btn_ecrire").css("display","block");
			$("#btn_ecrire").stop().animate({opacity:1}, 500, 'easeOutQuad');
			$('#header-holder').stop().animate({marginLeft:0},{duration: 500, easing: 'easeOutQuad'});
			$('#contact').css({"text-align":"right", "background-position":"140px 140px"});
		});
	});
	
	// -- Gestion Quitter page ------------> */
	
	if(!quitter){
		$("header nav li:not(#nav-con) a").click(function(e){
								  
			/*e.preventDefault();
			
			quitter = true;
			lien = $(this).attr("href");
			
			$("#fade").css("display","block");
			$("#fade").animate({opacity:1},500);
			
			setTimeout("goto()",500);*/
			
		});
	}
	
	// -- Gestion des personnages -------> */
	
	$("section.perso").hover(function(){
		persosFade($(this));
	},function(){
		persosUnfade();
	});
	
	// -- Gestion contact form ----------> */
	
	$("#ecrire form").submit(function(){
		return envoyer_form();						 
	})
	
	// -- Codes GA -----------------------> */
	
	$("#c_jas_lin").click(function(){_gaq.push(['_trackEvent', 'Social', 'LinkedIn', 'JasonLemay']);});
	$("#c_jay_lin").click(function(){_gaq.push(['_trackEvent', 'Social', 'LinkedIn', 'JaysonGuerard']);});
	$("#c_dav_lin").click(function(){_gaq.push(['_trackEvent', 'Social', 'LinkedIn', 'DavidLaplante']);});
	
	$("#c_jas_twi").click(function(){_gaq.push(['_trackEvent', 'Social', 'Twitter', 'JasonLemay']);});
	$("#c_jay_twi").click(function(){_gaq.push(['_trackEvent', 'Social', 'Twitter', 'JaysonGuerard']);});
	$("#c_dav_twi").click(function(){_gaq.push(['_trackEvent', 'Social', 'Twitter', 'DavidLaplante']);});
	
	$("#c_jas_con").click(function(){_gaq.push(['_trackEvent', 'Email', 'Contact', 'JasonLemay']);});
	$("#c_jay_con").click(function(){_gaq.push(['_trackEvent', 'Email', 'Contact', 'JaysonGuerard']);});
	$("#c_dav_con").click(function(){_gaq.push(['_trackEvent', 'Email', 'Contact', 'DavidLaplante']);});
	
	
});

function persosFade(t){
	$("section.perso").not(t).children('.bonhomme').stop().animate({
		width: "100px",
		height: "182px",
		left: "95px",
		top: "150px",
		opacity: 0.6
	},200);
	$("section.perso").not(t).children('.bonhomme').children().stop().animate({
		width: "100px",
		height: "364px"
	},200);
}

function persosUnfade(){
	$("section.perso div.bonhomme").stop().animate({
		width: "200px",
		height: "364px",
		left: "40px",
		top: "0",
		opacity: 1
	},200);	
	$("section.perso div.bonhomme img").stop().animate({
		width: "200px",
		height: "728px"
	},200);
}

function unfade(){
	$("#fade").animate({opacity:0},300,function(){$("#fade").css("display","none");});
}

function goto(){
	window.location.href = lien;
}

// Courriel

function envoyer_form(){	
	// Requête AJAX 
	if($('p#anti_flood').attr("class") != "anti_flood"){
		if($("#section.ecrire").attr("lang") == "fr"){
			_gaq.push(['_trackEvent', 'Formulaires', 'Contact', 'Francais']);
		}else{
			_gaq.push(['_trackEvent', 'Formulaires', 'Contact', 'Anglais']);
		}
		$.ajax({
			type: "POST",
			url: "../scripts/php/mail.php",
			data: "mail_nom="+$('#nom').val()+"&mail_courriel="+$('#email').attr('value')+"&mail_message="+$('#message').attr('value'),
			success: function(etat){
				traiter_form(etat);		
			}
		});
	}
	// Ne pas envoyer la page
	return false;
}

function traiter_form(etat){
	etat = parseInt(etat);
	// Afficher message selon code reçu
	switch(etat){
		case 1:
			$(".erreur").html('Tous les champs sont obligatoires');
			break;
		case 2:
			$(".erreur").html('Votre courriel doit &ecirc;tre sous la forme <em>adresse@domaine.aa</em>');
			break;
		case 3:
			$("#ecrire").stop().animate({top:"-555px"}, 500, 'easeInBack', function(){
				$("#msg").css({opacity:0,display:"block"});		
				$("#msg").animate({opacity:1}, 500);
				$("#msg").html('<p id="anti_flood" class="anti_flood">Merci!<br />Votre message a bien &eacute;t&eacute; envoy&eacute;.');	
			});
			break;
		case 4:
			$(".erreur").html('Un probl&egrave;me avec le serveur est survenu. Votre courriel n\'a pu être envoy&eacute;.');
			break;
		default:
			$(".erreur").html('Erreur inconnue .. oups!');
			break;
	}
}
