/********************************************************************/
/*********************** Nicolas SIONNIERE **************************/
/******************** (c) 2010 SIONNIERE NICOLAS ********************/
/****************** Email: portfolio@sionniere.com ******************/
/****************** http://nicolas.sionniere.com/ *******************/
/********************************************************************/

function letsgo(){
	/*SlideShow*/
	$("#metiers").cycle({
		fx: 'fade',
		timeout: 6000,
		pause: 1,
		prev: '#prev',
		next: '#next'
	});

	/*Form*/
	$('.error').hide();

	$('.boxgrid.captionfull').hover(function(){
	$('.cover').fadeTo(0,0.9)
	$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:250});
	}, function() {
	$(".cover", this).stop().animate({top:'200px'},{queue:false,duration:500});
	});

	$('a[name=contact]').click(function(e) {
		e.preventDefault();
		$('#reponse').html('');
		$('html').animate({scrollTop:0}, "fast")
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeTo("fast",0.6);	
		var winH = $(window).height();
		var winW = $(window).width();
		$("#form").css('top',  winH/2-$("#form").height()/2);
		$("#form").css('left', winW/2-$("#form").width()/2);
		$("#form").fadeTo("slow",1); 
	});
	$('a[name=mention]').click(function(e) {
		e.preventDefault();
		$('html').animate({scrollTop:0}, "fast")
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeTo("fast",0.6);	
		var winH = $(window).height();
		var winW = $(window).width();
		$("#mention").css('top',  winH/2-$("#mention").height()/2);
		$("#mention").css('left', winW/2-$("#mention").width()/2);
		$("#mention").fadeTo("slow",1); 
	});
	
	$('.window .close').click(function (e) {
		e.preventDefault();
		$('#mask').fadeOut('slow');
		$('.window').fadeOut('fast');
	});		
	$('#mask').click(function () {
		$(this).fadeOut('slow');
		$('.window').fadeOut('fast');
	});

	$('#btnsend').click(function() {
		$('.error').hide();
		var nom = $("input#nom").val();
		if (nom == "") {
			$("label#nom_error").show();
			$("input#nom").css('border','2px solid #ff2222');
			$("input#nom").focus();
			return false;
		}
		else {$("input#nom").css('border','2px solid #85b916');}
		var email = $("input#email").val();
		var regexp = /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
		if (email.match(regexp)==null) {
			$("label#email_error").show();
			$("input#email").css('border','2px solid #ff2222');
			$("input#email").focus();
			return false;
		}
		else {$("input#email").css('border','2px solid #85b916');}
		var tel = $("input#tel").val();
		if (tel == "") {
			$("label#tel_error").show();
			$("input#tel").css('border','2px solid #ff2222');
			$("input#tel").focus();
			return false;
		}
		else {$("input#tel").css('border','2px solid #85b916');}
		var message = $("textarea").val();
		if (message == "") {
			$("label#message_error").show();
			$("textarea").css('border','2px solid #ff2222');
			$("textarea").focus();
			return false;
		}
		else {$("textarea").css('border','2px solid #85b916');}
		var dataString = 'nom='+ nom + '&email=' + email + '&tel=' + tel + '&message=' + message;
		$.ajax({
			type: "POST",
			url: "envoi-formulaire.html",
			data: dataString,
			success: function(data){
				if(data=="DONE"){
					$('#reponse').html('<h2 id="sended">Message envoyé avec succès !</h2>'); 
				}
				else if(data=="FAIL1")
				{
					$('#reponse').html('<h2 id="notsended">Merci de compléter tous les champs du formulaire</h2>');
				}
				else $('#reponse').html('<h2 id="notsended">Ce message n\'a pas pu être envoyé. Réessayez.</h2>');
			}
		});
		
		return false;
	});
	$('#reponse').ajaxStart(function() {
		$(this).html('<div id="loader">Envoi en cours, merci de patienter ...</div>');
	});
	$('#reponse').ajaxError(function() {
		$(this).html('<h2 id="notsended">Ce message n\'a pas pu être envoyé. Réessayez.</h2>');
	});
}
