﻿$(document).ready(function() {
    //
    // selected menu item
    //
    $("#menu_contactezNous > span").css("opacity", "1").unbind();
    //
    // change top text
    //
    $(".bienvenue").attr("class", "contactezNous");
    //
    // envoyer button
    //
    $('#sendBtn').hover(function() {
        $(this).stop().css({ backgroundPosition: "0px 0px" }).animate(
            { backgroundPosition: "-132px 0px" },
		    500);
    }, function() {
        $(this).stop().animate(
            { backgroundPosition: "-264px 0px" },
		    500);
    }).click(function() { $form.submit(); return false; });
    var $form = $('form');
    $form.submit(function() {
        var name, tel, email, msg;
        var errors = {};
        errors.count = 0;
        errors.elmIds = new Array();
        name = $.trim($('#name').val());
        if (name == '') {
            errors.count++;
            errors.elmIds.push('name');
        }
        tel = $.trim($('#telephone').val());
        email = $.trim($('#email').val());
        msg = $.trim($('#message').val());
        if (msg == '') {
            errors.count++;
            errors.elmIds.push('message');
        }
        if (errors.count > 0) {
            for (i = 0; i < errors.count; i++) {
                $('#' + errors.elmIds[i]).effect("highlight", { color: '#f79122' }, 1000);
            }
        }
        else {
            $('#contact_form').hide();
            $('#contact_messageSent').fadeIn();
            $.post($form.attr("action"), $form.serialize(), function(json) {
                if (json.valid) {
                    $('#contact_messageSent').hide();
                    $('#contact_messageSent > img').attr("src", json.imgSrc);
                    $('#contact_messageSent > span').hide();
                    $('#contact_messageSent').fadeIn();
                }
                else {
                    alert(json.msg);
                    $('#contact_messageSent').hide();
                    $('#contact_form').show();
                }
            }, "json");
        }
        return false;
    });
});

