// JavaScript Document

$(document).ready(function(){
    $("#navbis li").mouseenter(function(){
      $(this).stop().animate({ 
		height: "25px",
		paddingTop: "10px"
      }, 200 );
    });
	 $("#navbis li").mouseleave(function(){
      $(this).stop().animate({ 
		height: "20px",
		paddingTop: "7px"
      }, 100 );
    });
	$('ul#portfolio').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '159px'
	});
});


$(function(){

// Mentions legales			
$('#dialog').dialog({
	autoOpen: false,
	width: 600,
	modal: true,
	buttons: {
		"Fermer": function() { 
		$(this).dialog("close"); 
		}
	}
});
// Lien mentions legales
$('#dialog_link').click(function(){
	$('#dialog').dialog('open');
	return false;
	});

});

// formulaire contact
$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$("#dialog-formcontact").dialog("destroy");
		
		var nom = $("#nom"),
			email = $("#email"),
			tel = $("#tel"),
			message = $("#message"),
			allFields = $([]).add(nom).add(email).add(tel).add(message),
			tips = $(".validateTips");

		function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("La longueur du champ ' " + n + " ' doit être comprise entre "+min+" et "+max+" caractères. Merci.");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog-formcontact").dialog({
			autoOpen: false,
			height: 500,
			width: 500,
			modal: true,
			buttons: {
				'Envoyer': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(nom,"nom",3,30);
					bValid = bValid && checkLength(email,"email",6,80);
					//bValid = bValid && checkLength(tel,"tel",0,10);
					bValid = bValid && checkLength(message,"message",5,350);

					bValid = bValid && checkRegexp(nom,/^[a-z]([a-z-éèçïë'\s])+$/i,"Merci de saisir votre nom !");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Votre adresse email est incorrecte !");
					//bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
					if(tel.val() != ""){
					bValid = bValid && checkRegexp(tel,/^([0-9])+$/,"Votre numéro de téléphone ne doit contenir que des chiffres !");}
					//bValid = bValid && checkRegexp(message,/^([0-9a-zA-Z'"\s,;:.éèçà&])+$/,"Merci de saisir votre message !");
					
					if (bValid) {
						$('#users tbody').append('<tr>' +
							'<td>' + nom.val() + '</td>' + 
							'<td>' + email.val() + '</td>' + 
							'<td>' + tel.val() + '</td>' +
							'<td>' + message.val() + '</td>' +
							'</tr>');
	
	var dataString = 'nom='+ nom.val() + '&email=' + email.val() + '&tel=' + tel.val() + '&msg=' + message.val();
	//alert (dataString);return false;
	
	$.ajax({
      type: "POST",
      url: "contact-valid.asp",
      data: dataString,
      success: function() {
        $("#dialog-formcontact").html('<div id="message-reponse"></div>');
        $("#message-reponse").html('<h2>Votre message a bien été envoyé !</h2>')
        .append('<p>Nous vous contacterons dans quelques jours. Merci.</p>')
        .hide()
        .fadeIn(300, function() {
          $("#message-reponse").append('<img src="images/picto-form-ok.png" />');
        });
      }
     });

	setTimeout(function() { 
        $("#dialog-formcontact").dialog('close');
    }, 5000); 

					
					}
				}//,
				//"Fermer": function() {
				//	$(this).dialog('close');
				//}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('.aff-contact')
			//.button()
			.click(function() {
				$('#dialog-formcontact').dialog('open');
			});

});