
var inSearchInput = false;
var onSearchArea = false;

$(document).ready(
function() {

    function loadBody(container, pageName, dataBody) {
        var urlBody = pageName + ".php";
        $.ajax({
            type: "POST",
            url: urlBody,
            data: dataBody,
            dataType: "html",
            success: function(resAjax) { container.html(resAjax); }
        });
    }

    // Img rollover
    $(".rollover_link").hover(
     function() {
         this.src = this.src.replace("-off", "-on");
     },
     function() {
         this.src = this.src.replace("-on", "-off");
     }
    );

    $(".rollover_link_selected").each(
    function() {
        this.src = this.src.replace("-off", "-on");
    });

    // Input fill/empty
    $(".empty_fill_text").focus(
    function() {
        if (this.value == this.title)
            this.value = "";
    });
    $(".empty_fill_text").blur(
    function() {
        if (this.value == "")
            this.value = this.title;
    });

    // Nav left
    $(".navigation ul.subMenu").hide();
    $(".navigation li.toggleSubMenu > a").click(function() {
        // If menu was opened then close it :
        if ($(this).next("ul.subMenu:visible").length != 0) {
            $(this).next("ul.subMenu").slideUp("normal");
            $(this).attr("class", $(this).attr("class").replace("-on", "-off"));
        }
        // If menu was hidden then show it and close other one :
        else {
            $(".navigation ul.subMenu").slideUp("fast");
            //$(".navigation .toggleSubMenu a").attr("class", $(".navigation .toggleSubMenu a").attr("class").replace("-on", "-off"));
            $(".navigation .toggleSubMenu a.linknav-on").attr("class", $(".navigation .toggleSubMenu a").attr("class").replace("-on", "-off"));
            $(this).next("ul.subMenu").slideDown("fast");
            $(this).attr("class", $(this).attr("class").replace("-off", "-on"));
        }
        return false;
    });

    // Search
    function search() {
        var kwd = $("#search_keyword").attr("value").replace(/\s/g, "-");
        kwd = kwd.replace(/['"]/g, "-");
        var searchURL = startSearchURL + escape(kwd);
        if (searchURL != startSearchURL && $("#search_keyword").attr("value") != "Par mots-clés") {
            document.location.href = searchURL;
        }
    }

    $("#searchNavLeftCadre").hide();
    $("#searchNavLeftCadreOutInvisible").hide();
    $("#search_keyword").focus(
    function() {
        inSearchInput = true;
        onSearchArea = true;
        $("#searchNavLeftCadre").fadeIn();
        $("#searchNavLeftCadreOutInvisible").show("fast");
    });
    $("#search_keyword").blur(
    function() {
        inSearchInput = false;
        if (!onSearchArea) {
            $("#searchNavLeftCadre").fadeOut();
            $("#searchNavLeftCadreOutInvisible").hide();
        }
    });
    $("#searchNavLeftCadre").hover(
    function() {
        onSearchArea = true;
    },
    function() {
        onSearchArea = false;
        if (!inSearchInput) {
            $("#searchNavLeftCadre").fadeOut();
            $("#searchNavLeftCadreOutInvisible").hide();
        }
    }
    );
    $("#searchNavLeftCadreOutInvisible").hover(
    function() {
        onSearchArea = false;
    },
    function() {
        onSearchArea = false;
        if (!inSearchInput) {
            $("#searchNavLeftCadre").fadeOut();
            $("#searchNavLeftCadreOutInvisible").hide();
        }
    }
    );

    $("#search_keyword").keypress(
    function(event) {
        if (event.keyCode == "13") {
            search();
        }
    });
    $("#valideSearch").click(
    function() {
        search();
        return false;
    });

    // Newsletter
    function sendNewsletter() {
        var email = $("#newsletter_input").attr("value");
        if (email != "" && email != "Saisissez votre e-mail") {
            var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (emailPattern.test(email)) {
                loadBody($("#newsletterCadre2"), "newsletterok", "email=" + $("#newsletter_input").attr("value") + "&nord=" + $("#newsletterCheckboxNord").attr("checked") + "&sud=" + $("#newsletterCheckboxSud").attr("checked"));
            }
        }
    }

    $("#newsletterCadre1").hide();
    $("#newsletterCadre2").hide();
    $("#newsletterCadreOutInvisible").hide();
    $("#newsletter_input").focus(
    function() {
        inSearchInput = true;
        onSearchArea = true;
        $("#newsletterCadre1").fadeIn();
        $("#newsletterCadre2").fadeIn();
        $("#newsletterCadreOutInvisible").show("fast");
    });
    $("#newsletter_input").blur(
    function() {
        inSearchInput = false;
        if (!onSearchArea) {
            $("#newsletterCadre1").fadeOut();
            $("#newsletterCadre2").fadeOut();
            $("#newsletterCadreOutInvisible").hide();
        }
    });
    $("#newsletterCadre1").hover(
    function() {
        onSearchArea = true;
    },
    function() {
        onSearchArea = false;
        if (!inSearchInput) {
            $("#newsletterCadre1").fadeOut();
            $("#newsletterCadre2").fadeOut();
            $("#newsletterCadreOutInvisible").hide();
        }
    }
    );
    $("#newsletterCadre2").hover(
    function() {
        onSearchArea = true;
    },
    function() {
        onSearchArea = false;
        if (!inSearchInput) {
            $("#newsletterCadre1").fadeOut();
            $("#newsletterCadre2").fadeOut();
            $("#newsletterCadreOutInvisible").hide();
        }
    }
    );
    $("#newsletterCadreOutInvisible").hover(
    function() {
        onSearchArea = false;
    },
    function() {
        onSearchArea = false;
        if (!inSearchInput) {
            $("#newsletterCadre1").fadeOut();
            $("#newsletterCadre2").fadeOut();
            $("#newsletterCadreOutInvisible").hide();
        }
    }
    );

    $("#newsletterCheckboxNord").change(
    function() {
        if (!this.checked) {
            $("#newsletterCheckboxSud").attr("checked", true);
        }
    });
    $("#newsletterCheckboxSud").change(
    function() {
        if (!this.checked) {
            $("#newsletterCheckboxNord").attr("checked", true);
        }
    });
    $("#linkNewsletterNord").click(
    function() {
        $("#newsletterCheckboxNord").attr("checked", !$("#newsletterCheckboxNord").attr("checked"));
        $("#newsletterCheckboxNord").change();
        return false;
    });
    $("#linkNewsletterSud").click(
    function() {
        $("#newsletterCheckboxSud").attr("checked", !$("#newsletterCheckboxSud").attr("checked"));
        $("#newsletterCheckboxSud").change();
        return false;
    });

    $("#newsletter_input").keypress(
    function(event) {
        if (event.keyCode == "13") {
            sendNewsletter();
        }
    });
    $("#valideNewsletter").click(
    function() {
        sendNewsletter();
        return false;
    });



    //$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
    //$('#gallery a').lightBox(); // Select all links in object with gallery ID
    if ($('a.lightbox').length > 0) {
        $('a.lightbox').lightBox(); // Select all links with lightbox class
    }
    //$('a').lightBox(); // Select all links in the page
}
);

