﻿/* Animation contact */
var lang         = 'fr';
var base_href    = '/';
var anim_encours = 0;
var temps_depart = 1500;
var lance_depart = 800;
var temps_arrive = 1000;

function verifEmail(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
   return (reg.exec(email)!=null);
}

function errorMail(msg){
	if($("#email").length>0 && anim_encours == 0){
		anim_encours = 1;
		$("#email").css('color','#ff0000');
		$("#email").val(msg);
		$("#bandeau-email").effect("bounce", { times:3,direction:'left',distance:20 }, 150, function(){
			anim_encours = 0;
		});
	}
}

function initMail(){
	$("#email").css('color','#ffffff');
}

function goodMail(msg){
	if(anim_encours == 0){
		anim_encours = 1;
		$("#email").css('color','#00ff00');
		$("#email").val(msg);
		setTimeout(function(){
			$("#bandeau-email").animate({"left": "-579px"}, {duration: temps_depart, easing: 'easeInBack', complete:function(){
				$("#bandeau-email").fadeOut({ duration: 100, easing: 'easeOutQuad', complete:function(){
					$("#email").val('');
					initMail();
					anim_encours = 0;
				}});
			}});
		},lance_depart);
	}
}

function avertissementMail(msg){
	if(anim_encours == 0){
		anim_encours = 1;
		$("#email").css('color','#EE4000');
		$("#email").val(msg);
		setTimeout(function(){
			$("#bandeau-email").animate({"left": "-579px"}, {duration: temps_depart, easing: 'easeInBack', complete:function(){
				$("#bandeau-email").fadeOut({ duration: 100, easing: 'easeOutQuad', complete:function(){
					$("#email").val('');
					initMail();
					anim_encours = 0;
				}});
			}});
		},lance_depart);
	}
}

function contact_greg(){
	if($("#email").length>0){
		if($("#email").val() != '' && $("#email").val() != 'Oops ... forgot your mail ?'){
			if(verifEmail($("#email").val())){
				$.get(base_href+"action/enregistrement_contact.php?nocache="+Math.random()+"&email="+$("#email").val(), function(data, textStatus){
				if(textStatus == 'success'){
						if(data == 0){
							goodMail('Thanks...');
						}else{
							avertissementMail('Oops... already registred!');
						}
					}
				});
			}else{
				errorMail('Oops ! A Mistake ? Try again ...');
			}
		}else{
			errorMail('Oops ... forgot your mail ?');
		}
	}
}

$(document).ready(function(){
	//initialisation base href
	if($("#base_href").val() != ''){
		base_href = $("#base_href").val();
	}
	//initialisation langue
	if($("#langue").val() != ''){
		lang = $("#langue").val();
	}

	$("#getintouch").click(function () {

		if($("#bandeau-email").length>0 && anim_encours == 0){
			anim_encours = 1;
			$("#bandeau-email").fadeIn({ duration: 100, easing: 'easeOutQuad', complete:function(){
				$("#bandeau-email").animate({"left": "368px"}, {duration: temps_arrive, easing: 'easeOutBack', complete:function(){
					anim_encours = 0;
				}});
			}});
		}
	});

});
