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

function letsgo(){
	/*Zoom*/
	$("a.zoom").fancybox({
		'frameWidth':	800,
		'frameHeight':	400,
		'zoomSpeedIn':	450,
		'overlayShow':	true,
		'overlayOpacity':	0.65
	});

	/*SlideShow*/
	function Carrousel(id){
		var ident = id;
		var reference = $("#carrousel"+ident+" .content:first-child");
		var nbElement = $("#carrousel"+ident+" div").length;
		var Cpt = 0;
		
		this.runIt = function(){
			$("#carrousel"+ident)
			.wrap('<div class="carrousel-conteneur'+ident+'"></div>') 
			.css("height",(reference.height()*nbElement)); 

			$(".carrousel-conteneur"+ident)
			.width(reference.width()) 
			.height(reference.height()) 
			.css("overflow", "hidden") 
			
			$("#next"+ident).click(function(){
				if(Cpt<(nbElement-1)){
					Cpt++;
					$("#carrousel"+ident).animate({
						marginTop: -(reference.height()*Cpt)
					});
				}
				else{
					Cpt=0;
					$("#carrousel"+ident).animate({
						marginTop: "0"
					});
				}
				return false; 
			});
		}
	}
	var c1 = new Carrousel(1);
	var c2 = new Carrousel(2);
	c1.runIt(); c2.runIt();
	
	/*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>');
	});
}
