/* -----------------------------------------------------------------------------
	aaz.site.js - Fonctions javascript par défaut du site
------------------------------------------------------------------------------*/


/* -----------------------------------------------------------------------------
	Formulaires / Controles
------------------------------------------------------------------------------*/
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email) || email.substr(-3)=='.co') {
		return false;
	}
	return true;
}

// Change le status d'un élément
function switchStatus(selecteur, status){
	if (status == 'n') {
		$(selecteur).parent('div').addClass('error');
	}
	else
	{
		$(selecteur).parent('div').removeClass('error');
	}
	$(selecteur).parent('div').children('span.n,span.o,span.dn').removeClass().addClass(status);
}


function countErrors(selector)
{
	var nbErrors = $(selector).find('div.error:visible').length;
	return nbErrors;
}

// A défaut de blink on focus sur le premier champ
function blinkError(selector){
	$(selector).find('div.error:first').children('input').focus();
//	$(selector).find('span.n:first').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
}

/* -----------------------------------------------------------------------------
	Modal Standard
------------------------------------------------------------------------------*/

function openModal(href){
	closeModal();
	$.get(href, function(data) {
		$('html, body').animate({scrollTop: 0}, 800);
		$('body').prepend('<span></span>');	// Patch IE6.
		$('body>span:first').html(data);
	});
}


function closeModal(f){
	$('div.modal').slideUp('normal', function() {
		// Animation complete.
		$('div.overlay-modal, body>span:first').remove();
		if (typeof f == "function") {
			f();
		}
	});
}

function createModal(title,content){
	closeModal();
	var data = '<div class="overlay-modal"></div><div class="modal"> \
		<div class="top-stroke"></div> \
		<div class="content-stroke"> \
			<div class="content-txt"> \
				<h1>'+title+'</h1> \
				<span class="close"><a href="#">[x]</a></span> \
				<div class="content-modal txt"> \
				<span class="blue">'+content+'</div> \
			</div> \
		</div> \
		<div class="bottom-stroke"></div> \
	</div>';
	$('body').prepend(data);
}

function swfModal(swf){
	closeModal();
// 			<div class="content-txt" style="width:683px"> \
	var data = '<div class="overlay-modal"></div><div class="modal"> \
		<div class="top-stroke"></div> \
		<div class="content-stroke"> \
			<div class="content-txt"> \
				<h1>Pour tourner les pages, cliquez en bas à droite du guide</h1> \
				<span class="close"><a href="#">[x]</a></span> \
				<div class="content-modal"><span id="flash_holder" style="bgcolor:#B8B8B8"></span></div> \
			</div> \
		</div> \
		<div class="bottom-stroke"></div> \
	</div>';
	var flashvars = {};
	var params = {
	  menu: "false"
	};
	var attributes = {
	  quality	: "high",
	  bgcolor	: "#B8B8B8",
	  width		: "619",
	  height	: "619",
	  play		: "true",
	  loop		: "false",
	  wmode		: "transparent",
	  allowScriptAccess		: "always"
	};
	$('body').prepend(data);
	$.getScript('js/swfobject.2.2.js', function() {
		swfobject.embedSWF(swf, "flash_holder", "619", "619", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	});
}


/* -----------------------------------------------------------------------------
	Bloc Login
------------------------------------------------------------------------------*/
function showLogin(){
	$('div.ep form').fadeIn('normal');
	$('input[name=login]').focus();
}

function showLoginPreHome(){
	$('#ph li:eq(1)').fadeOut(function(){
		$('div.ep form').show();
		$('#ph li:eq(2)').fadeIn(function(){
			$('input[name=login]').focus();
		});
	});
}

/* -----------------------------------------------------------------------------
	Bloc Newsletter
------------------------------------------------------------------------------*/
function showNewsletter(){
	$('span.footerNewsletter>form').fadeIn('normal');
	$('input[name=newsletter_mail]').focus();
}



/* -----------------------------------------------------------------------------
	Lancements onLoad
------------------------------------------------------------------------------*/
$(function() {

	/* -----------------------------------------------------------------------------
	Montrer le formulaire Login
	------------------------------------------------------------------------------*/
	$('A.showLogin').click(function()
	{
		showLogin();
		return false;
	});

	$('A.showLoginPreHome').click(function()
	{
		showLoginPreHome();
		return false;
	});


	/* -----------------------------------------------------------------------------
	Montrer le formulaire newsletter
	------------------------------------------------------------------------------*/
	$('A.showNewsletter').click(function()
	{
		showNewsletter();
		return false;
	});

	/* -----------------------------------------------------------------------------
	Modal SWF
	------------------------------------------------------------------------------*/
	$('A[href$=swf]').live('click', function() {
		swfModal($(this).attr('href'));
		return false;
	});


	/* -----------------------------------------------------------------------------
	Liens modal
	------------------------------------------------------------------------------*/
	$('A[target=modal], A.openModal').live('click', function() {
		closeModal();
		var thisModal = $(this).attr('href');
		openModal(thisModal);
		return false;
	});

	$('A.closeModal, span.close A').live('click', function() {
		closeModal();
		return false;
	});



	/*--------------------------------------------
		formulaires standard
	----------------------------------------------*/

	$('form.standardForm A.submit, input.submit').live('click', function() {
		var thisForm = $(this).parents('form');
		$(thisForm).find('input:visible, textarea:visible, select:visible').each(function(index) {
				$(this).focus().blur();
		});
		// reste-t-il des erreurs ?
		if (countErrors($(thisForm)) == 0) {
			// ajax submit !
			if ($(thisForm).find('input[name=ajaxAction]').length >= 1) {
				$.post("actions/ajaxActions.php", {
					'ajaxAction': $(thisForm).find('input[name=ajaxAction]').val(),
					'serialised': $(thisForm).serialize()
				},
				function(data){
					if (data.erreur == '0') {
						$(thisForm).slideUp();
						createModal(data.html.titre,data.html.content);
					}
					else
					{
						createModal('Erreur','Vous devez saisir une adresse e-mail valide.');
					}
				}, "json");
			}
			// Submit
			else
			{
				$(thisForm).submit();
			}
		}
		else
		{
			blinkError($(thisForm));
		}
		return false;
	});

	$('#submit').live('click', function() {
		var thisForm = $(this).parents('form');
		$(thisForm).submit();
		return false;
	});


	$('form input').live("keydown", function(event) {
		if (event.keyCode == 13) {
			return false;
		}
	});

	// Champ mail
	$('input.obligatoire_mail:visible').live('keyup blur', function() {
		if (!checkEmail($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Champ obligatoire
	$('input.obligatoire:visible, textarea.obligatoire:visible').live('keyup blur', function() {
		if ($(this).val() == '' || $(this).val() == null) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	$('select.obligatoire:visible').live('keyup blur change', function() {
		var thisVal = $(this).children('option:selected').val();
		if (thisVal == '' || thisVal == null || thisVal == 'Séléctionner') {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Champ numérique
	$('input.obligatoire_numerique, input.numerique, input.cp').keydown(function(e)
     {
		var key = e.charCode || e.keyCode || 0;
		var thisVal = $(this).attr('value');
		if (key == 38)
		{
			$(this).attr('value',parseInt(thisVal)+1);
		}
		else if (key == 40)
		{
			var newVal = parseInt(thisVal)-1;
			if (newVal < 1) {newVal = 1;}
			$(this).attr('value',newVal);
		}
		// autorise backspace, tab, delete, fleches, chiffres, pavé numérique, controle
		if (
			key == 8 ||
			key == 9 ||
//			key == 17 ||	// Controle
			key == 46 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105))
		{
			return true;
		}
		return false;
	});


	// Champ numérique
	$('input.obligatoire_siret').keydown(function(e)
     {
		var key = e.charCode || e.keyCode || 0;
		var thisVal = $(this).attr('value');
		if (key == 38)
		{
			$(this).attr('value',parseInt(thisVal)+1);
		}
		else if (key == 40)
		{
			var newVal = parseInt(thisVal)-1;
			if (newVal < 1) {newVal = 1;}
			$(this).attr('value',newVal);
		}
		// autorise backspace, tab, delete, fleches, chiffres, pavé numérique, controle
		if (
			key == 8 ||
			key == 9 ||
//			key == 17 ||	// Controle
			key == 46 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105))
		{
			return true;
		}
		return false;
	});

	$('input.obligatoire_siret:visible').live('keyup blur', function() {
		var thisVal = $(this).attr('value');
		if (isNaN(thisVal) || thisVal.length !== 9) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});


	// Checkbox obligatoire
	$('input.obligatoire_checkbox:visible').live('click', function() {
		var thisName = $(this).attr('name');
		var isChecked  = $('input[name='+thisName+']:checked').length;
		if (isChecked == 1) {
			switchStatus(this, 'o');
		}
		else
		{
			switchStatus(this, 'n');
		}
	});

	// Champ obligatoire numérique
	$('input.obligatoire_numerique:visible, select.obligatoire_numerique:visible').live('keyup blur', function() {
		if ($(this).val() == '' || $(this).val() == null || isNaN($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});


});

