$(document).ready(function(){
    // Initialisation de l'accordéon
    $("#content").accordion();
    // Initialisation de l'effet lightbox
    $("a[rel^='prettyPhoto']").prettyPhoto({
        showTitle: false,
        theme: 'light_rounded'
    });
    // Cache la zone "Contact"
    $("#contact").hide();
    // Affiche la zone "Contact"
    $(".contact").click(function() {
        var css = $("#contact").css("display");
        if (css == "none") {
            $("#contact").show(1000);
            $("body").scrollTo( '+=208px', 1000 );
            return false;
            }else {$("#contact").slideUp(1000);}
    });
    // Ensemble de tests sur les champs du formulaire
    $("input#nom").click(function() {
        var name = $("input#nom").val();
        if (name == "Veuillez indiquer votre nom") {
            $("input#nom").val("");
            $("input#nom").css("font-style","normal");
            }
    });
    $("input#nom").focus(function() {
        var name = $("input#nom").val();
        if (name == "Veuillez indiquer votre nom") {
            $("input#nom").val("");
            $("input#nom").css("font-style","normal");
            }
    });
    $("input#mail").click(function() {
        var email = $("input#mail").val();
        if (email == "Veuillez donner un e-mail valide") {
            $("input#mail").val("");
            $("input#mail").css("font-style","normal");
            }
    });
    $("input#mail").focus(function() {
        var email = $("input#mail").val();
        if (email == "Veuillez donner un e-mail valide") {
            $("input#mail").val("");
            $("input#mail").css("font-style","normal");
            }
    });
    $("textarea#message").click(function() {
        var msg = $("textarea#message").val();
        if (msg == "Merci de laisser un message") {
            $("textarea#message").val("");
            $("textarea#message").css("font-style","normal");
            }
    });
    $("textarea#message").focus(function() {
        var msg = $("textarea#message").val();
        if (msg == "Merci de laisser un message") {
            $("textarea#message").val("");
            $("textarea#message").css("font-style","normal");
            }
    });
    // Validations et envoi du formulaire au clic sur le bouton "Envoyer"
    $(".submit").click(function() {
        // Validations
        var name = $("input#nom").val();
        if (name == "" || name == "Veuillez indiquer votre nom") {
            $("input#nom").val("Veuillez indiquer votre nom");
            $("input#nom").css("font-style","italic");
            return false;
            }
        var email = $("input#mail").val();
        if (!email.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$') || name == "" || name == "Veuillez donner un e-mail valide") {
            $("input#mail").val("Veuillez donner un e-mail valide");
            $("input#mail").css("font-style","italic");
            return false;
        }
        var msg = $("textarea#message").val();
        if (msg == "" || msg == "Merci de laisser un message") {
            $("textarea#message").val("Merci de laisser un message");
            $("textarea#message").css("font-style","italic");
            return false;
            }
        // Envoi du message
        var data = 'name='+ encodeURIComponent(name) + '&email=' + encodeURIComponent(email) + '&msg=' + encodeURIComponent(msg);
        $.ajax({
            type: "POST",
            url: "lib/contact.php",
            data: data,
            success: function() {
                $('#contactform').html("<p>Merci</p>")
                .hide()
                .fadeIn(800, function() {
                    $('#contactform').append("<p>Et à bientôt !</p>");
                });
            }
        });
        return false;
    });
});

