function Adjust(){
	var scrLargeur = document.documentElement.clientWidth;  
	var decalage = ((scrLargeur-1024)/2);
	//alert(scrLargeur);
	$('body').css('background-position', (decalage+7)+'px 2px');
	$('#header, #worksBottom').css('background-position', (decalage+5)+'px bottom');
	$('#homeTop').css('background-position', (decalage+11)+'px top');
	$('#homeBottom').css('background-position', (decalage+10)+'px');
	$('#works, #contact').css('background-position', (decalage-2)+'px top');
	$('#processTop').css('background-position', (decalage-1)+'px top');
	$('#processBottom').css('background-position', (decalage+7)+'px');
}

$(document).ready(function(){
  //boutons
	Adjust();
	Reset();
	$(window).resize(function(){Adjust();});
	
	$('.gallery a').each(function(){
		$(this).lightBox();
	});
	$('.multi a').lightBox();
	
	$('#linkContact, #linkProcess, #linkWork').each(function(){
		var id = "#"+$(this).attr("id");
		$(this).hover(
			function (){
				$(id+' span.hover').fadeIn(100);
			},
			function () {
				$(id+' span.hover').fadeOut(150)
			}
		);
	});
	$('#linkContact a, #contactShortcut a').click(function(){
		var scroll = $("#contactTitle").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scroll+20}, 1000 );
	});
	$('#linkProcess a, #processShortcut a').click(function(){
		var scroll = $("#processTitle").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scroll-110}, 1000 );
	});
	$('#linkWork a, #worksShortcut a').click(function(){
		var scroll = $("#worksTitle").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scroll-170}, 1000 );
	});
	
	$('#contactButtonUp a').click(function(){
		var scroll = $("#header").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scroll-10}, 1000 );
	});
	
	$(".more").click(
		function(){
			$("#homeTextPanel").fadeOut(500);
			setTimeout('$("#homeMorePanel").fadeIn(250)', 500);	
	});
	$(".less").click(
		function(){
			$("#homeMorePanel").fadeOut(500);
			setTimeout('$("#homeTextPanel").fadeIn(250)', 500);		
	});
	
	$('.homeLink, #works a').each(function(){
		$(this).hover(function(){
			$(this).children('span').slideToggle(250);
			$(this).children('span').children('span').slideToggle(350);
		
		}, function(){
			$(this).children('span').children('span').slideToggle(250);
			$(this).children('span').slideToggle(350);
		});
	});
	
	$('#contactSubmit a').click(function(){
		ValidFormulaire();});
		
	$('#name, #email, #message').each(function(){
		var id = "#"+$(this).attr("id");
		$(this).focus(
			function(){
				Test(id);
		});
		$(this).blur(
			function(){
				Restore(id);
		});
	});
});

var messageVide = "Ce champ doit être rempli"
var nombreOK = 0;

function Test(input){
	if ($(input).val() == "" || $(input).val() == messageVide){
		$(input).val("");
		$(input).addClass("saisie");
	}
	if ($(input).attr("id") == "email")
		$('.wrongMail').fadeOut(300);
}
function Restore(input){
	if ($(input).val() == "")
		$(input).removeClass("saisie");
}
function Reset(){
	$('#name').val("");
	$('#name').removeClass("saisie");
	$('#email').val("");
	$('#email').removeClass("saisie");
	$('#message').val("");
	$('#message').removeClass("saisie");
}
function ValidFormulaire(){
	TraiterInput('#name');
	TraiterInput('#email');
	TraiterInput('#message');
	if (nombreOK == 3){
		nombreOK = 0;
		var AJAX = "includes/contact.php";
		if(!$.post(AJAX, { req: "testNav" }))
			AfficherAlerte(false, "Votre navigateur n'est pas à jour...");
			
		else{
			$.post(AJAX, { req: "submit", nom: $('#name').val(), email: $('#email').val(), message: $('#message').val()}, 
			function(data){
				//alert(data);
				if (data == "ok")
					AfficherAlerte(true, "Merci, votre message a bien été envoyé !");
				else if (data == "error1")
					AfficherAlerte(false, "Merci mais vous m'avez déja contacté recemment.");
				else if (data == "error2")
					AfficherAlerte(false, "Ce message a déja été envoyé recemment.");
				else
					AfficherAlerte(false, "Des Erreurs sont survenues.");
			});
		}
	}
	else{
		nombreOK = 0;
		AfficherAlerte(false, "Des Erreurs sont survenues.");
	}	
}
function TraiterInput(input){
	if ($(input).hasClass("saisie") == false){
		$(input).val(messageVide);
	}
	else{
		if (input == "#email"){
			var regexVerif = new RegExp("^[a-z0-9._-]+@[a-z0-9._-]{2,}[\.]{1}[a-z]{2,4}$","g");//regex contrôle email
			if (regexVerif.test($(input).val()))
				nombreOK++;
			else
				$('.wrongMail').fadeIn(300);
		}
		else
			nombreOK++;
	}
}
function AfficherAlerte(cas, message){
	if (cas == false)
		$('#contactAlert').css({'background' : '#6e1313', 'border' : '5px solid #6e1313', '-moz-border-radius' : '5px', '-webkit-border-radius' : '5px'});
	else{
		$('#contactAlert').css({'background': '#498a14' , 'border' : '5px solid #498a14', '-moz-border-radius' : '5px', '-webkit-border-radius' : '5px'});
		Reset();
	}
	$('#contactAlert p').html(message);
	$('#contactAlert').fadeIn(300);
}