$(document).ready(function() {
	var isPopin1 = false;
	var isPopin2 = false;
	
	// --- MODE D'EMPLOI ---
	// affichage
	$('.modeemploi').click(function(e) {
		$('.listEvents').css('overflow', 'hidden');
		$('.listAsso').css('overflow', 'hidden');
		$('.fiche').css('overflow', 'hidden');
		if(!isPopin1 && !isPopin2) {
			$('p.msg').remove();
			$('.overlay').animate({ opacity : 0.3 }, 300, function() {
				isPopin1 = true;
				$('.Popin').slideDown('normal');
			});
		}
		e.preventDefault();
	});
	// fermeture
	$(document).keydown(function(e) {		
		if(isPopin1 && e.keyCode==27) {
			$('.Popin').slideUp('normal', function() {
				$('.overlay').animate({ opacity : 1.0 }, 300, function() {
					$('.listEvents').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					$('.listAsso').css('overflow', 'hidden');
					$('.fiche').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					isPopin1 = false;
				});
			});
		}
	});
	$('a.close').click(function(e) {
		if(isPopin1) {
			$('.Popin').slideUp('normal', function() {
				$('.overlay').animate({ opacity : 1.0 }, 300, function() {
					$('.listEvents').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					$('.listAsso').css('overflow', 'hidden');
					$('.fiche').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					isPopin1 = false;
				});
			});
		}
		e.preventDefault();
	});
	
	// --- CONTACT ---
	// affichage
	$('.contactbox').click(function(e) {
		$('.listEvents').css('overflow', 'hidden');
		$('.listAsso').css('overflow', 'hidden');
		$('.fiche').css('overflow', 'hidden');
		if(!isPopin1 && !isPopin2) {
			$('p.msg').remove();
			$('#nom').val('');
			$('#email').val('');
			$('#message').val('');
			$('.overlay').animate({ opacity : 0.3 }, 300, function() {
				isPopin2 = true;
				$('.Contact').slideDown('normal');
			});
		}
		e.preventDefault();
	});
	// fermeture
	$(document).keydown(function(e) {		
		if(isPopin2 && e.keyCode==27) {
			$('.Contact').slideUp('normal', function() {
				$('.overlay').animate({ opacity : 1.0 }, 300, function() {
					$('.listEvents').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					$('.listAsso').css('overflow', 'hidden');
					$('.fiche').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					isPopin2 = false;
				});
			});
		}
	});
	$('a.close').click(function(e) {
		if(isPopin2) {
			$('.Contact').slideUp('normal', function() {
				$('.overlay').animate({ opacity : 1.0 }, 300, function() {
					$('.listEvents').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					$('.listAsso').css('overflow', 'hidden');
					$('.fiche').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					isPopin2 = false;
				});
			});
		}
		e.preventDefault();
	});
	$('#cancelMsg').click(function(e) {
		if(isPopin2) {
			$('.Contact').slideUp('normal', function() {
				$('.overlay').animate({ opacity : 1.0 }, 300, function() {
					$('.listEvents').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					$('.listAsso').css('overflow', 'hidden');
					$('.fiche').css({'overflow-x' : 'hidden', 'overflow-y' : 'auto'});
					isPopin2 = false;
				});
			});
		}
		e.preventDefault();
	});
	// limitation caractères textarea
	$("#message").keyup(function() {
		var text = $(this).val();
		if(text.length > 500) {
			$(this).val(text.substr(0,500));
		}
	});
	
	// envoi message
	$('#submitMsg').click(function(e) {
		var msg = '';
		$('p.msg').remove();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if($('#nom').val()=='') { msg = msg + "Le nom est obligatoire. "; }
		if(reg.test($('#email').val()) == false) { msg = msg + "L'email est invalide. "; }
		if($('#message').val()=='') { msg = msg + "Le message est obligatoire."; }
		if(msg != '') { $('p.top').after('<p class="msg">'+msg+'</p>'); }
		else {
			$.post('inclusions/sendmail.php', { nom : $('#nom').val(), email : $('#email').val(), message : $('#message').val() }, function() {
				$('a.close').trigger('click');
			});
		}
		e.preventDefault();
	});
});