$(function(){ 
	// Lancement de l'accordeon
	$("#accordion").accordion({ opencontent: false, close: true });

	// Lancement du slideshow
	$('.projet:not(:first)').hide();
	$('#btnslider')
		.children('.prev').click(function()
		{
			// Initialisation
			var show = $('.projet:visible');
			var first = $('#contentslider').children('.projet:first');
			// Verification du premier projet
			if(show.attr('id') == first.attr('id'))
			{
				// Affiche le dernier projet
				$('.projet:visible').fadeOut('', function callback(){ $('#contentslider').children('.projet:last').fadeIn(''); });
			}
			else
			{
				// Affiche le projet precedent
				$('.projet:visible').fadeOut('', function callback(){ $(this).prev('.projet').fadeIn('slow'); });
			}
		}).end()
		.children('.next').click(function()
		{
			// Initialisation
			var show = $('.projet:visible');
			var last = $('#contentslider').children('.projet:last');
			// Verification du premier projet
			if(show.attr('id') == last.attr('id'))
			{
				// Affiche le dernier projet
				$('.projet:visible').fadeOut('', function callback(){ $('#contentslider').children('.projet:first').fadeIn(''); });
			}
			else
			{
				// Affiche le projet precedent
				$('.projet:visible').fadeOut('', function callback(){ $(this).next('.projet').fadeIn('slow'); });
			}	
		}).end();
		
	// Formulaire de contact
	$('.clearForm').click(function()
	{ 
		$('#formContent').children('input').removeClass('valid').removeClass('error').val('').next('.validBtns').html('');
		$('#message').val('');
	});
	$('#formContent').children('input').keyup(function(){ checkField($(this)); });
	$('#formContent').children('input').blur(function(){ checkField($(this)); });
	
	// Fonction de verification d'un champ
	function checkField(field)
	{
		// Initialisation
		var verifText = field.val();
		
		// Verification du champ
		if(verifText != '')
		{
			// Expressions Regulieres
			switch(field.attr('name')){
				case "nom" : 	var regExp = /^[A-Za-zéèêëàâäîïôöç\- ]{2,}$/; break;
				case "prenom" : var regExp = /^[A-Za-zéèêëàâäîïôöç\- ]{3,}$/; break;
				case "email" : 	var regExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; break;
				case "sujet" : 	var regExp = /^[A-Za-zéèêëàâäîïôöç@\-\(\) ]{5,}$/; break;
			}
			// Verification du champ
			if(regExp.test(verifText)){ field.removeClass('error').addClass('valid').next('.validBtns').html('<img src="img/validOK.png" alt="Champ valide" />'); }
			else { field.removeClass('valid').addClass('error').next('.validBtns').html('<img src="img/validNO.png" alt="Champ non valide" />'); }
		} else { field.removeClass('valid').removeClass('error').next('.validBtns').html(''); }
	}
	
	// Envoi du formulaire
	$('#formContact').submit(function()
	{
		// Initialisation
		var formCheck = true;
		$('#formContent').children('input').each(function(){ if($(this).hasClass('error') || $(this).val() == ''){ formCheck = false; } });
		
		// Verification du formulaire
		if(formCheck === false)
		{
			// Message d'erreur
			if(!$('p.messageForm').attr('id'))
			{
				$(this).prepend('<p class="messageForm" style="display:none;">Merci de saisir tous les champs et de verifier qu\'ils sont valides.</p>');
				$('p.messageForm').fadeIn('slow');
			}
			// Arret du formulaire
			return false;
		}
		else
		{
			// Initialisation
			var formNom = $('#nom').val();
			var formPrenom = $('#prenom').val();
			var formEmail = $('#email').val();
			var formSujet = $('#sujet').val();
			var formMessage = $('#message').val();
			
			// Affichage du loader
			$('#loader').show().css('padding', '130px 0');
			$(this).height('350px');
			
			// Envoi du formulaire en Ajax
			$.post(
				'contact.php',
				{ formSend: 'send', nom: formNom, prenom: formPrenom, email: formEmail, sujet: formSujet, message: formMessage, key: 'jQ' },
				function callback(data, status)
				{
					// Masque le loader
					$('#loader').hide();
					
					// Verification du retour
					if(data == 'message_sent')
					{
						// Affiche le message de reussite
						$('#formContact').html('<div id="success"><p>Le petit oiseau voyageur a atteint sa destination !</p></div>');
					}
					else
					{
						// Affiche le message d'echec
						$('#formContact').html(
							'<div id="error"><p>Le petit oiseau voyageur n\'est pas parti... :(</p></div>' +
							'<p class="messageForm">' + data + '</p>'
						);
					}
				}
			);
			
			// Corrige le bug de l'envoi
			return false;
		}
		
		// Empeche l'envoi normal
		return false;
	});	
});