﻿var _jTimeOut;
var dureeTimeOut = 2000;


/// ---------------------------------------
/// Ferme le sous menu
/// ---------------------------------------
function fermeSecondNav() {
    supprInterval();
    $("#header ul#nav a").removeClass("hover");
    $("#second-nav ul").stop().hide();
    $("#second-nav img.jMenuPointe").hide();
}


/// ---------------------------------------
/// Ajoute le compte à rebourd
/// ---------------------------------------
function ajouteInterval() {
    supprInterval();
    _jTimeOut = setTimeout("fermeSecondNav()", dureeTimeOut);
}

function supprInterval() {
    if (_jTimeOut != null && _jTimeOut != "undefined") {
        clearTimeout(_jTimeOut);
        _jTimeOut = null;
    }
}

function navigation() {
    /// ---------------------------------------
    /// Ouvre le sous menu correspondant au menu survolé
    /// ---------------------------------------
    $("#header ul#nav a").hover(function() {
        /// Masque les menus
        fermeSecondNav();

        /// Ouvre le sous menu
        var _id = $(this).attr("id");
        var elt = $("#nav-" + _id);
        if (elt.length > 0) {
            $(this).addClass("hover");
            pos = $(this).findPos();
            elt.css({ top: pos.y + 63 + "px", left: pos.x + "px" });
            $("#second-nav img.jMenuPointe").css({ top: pos.y + 54 + "px", left: pos.x + 40 + "px" });

            elt.show();
            $("#second-nav img.jMenuPointe").show();
        }
    }, function() {
        /// Lance le compte à rebours
        ajouteInterval();
    });


    /// ---------------------------------------
    /// Survole d'un sous menu
    /// ---------------------------------------
    $("#second-nav ul").hover(function() {
        /// Annule le compte à rebours
        supprInterval();
    }, function() {
        /// Lance le compte à rebours
        ajouteInterval();
    });


    /// ---------------------------------------
    /// Premier sous menu survolé
    /// ---------------------------------------
    $("#second-nav ul li:first-child").hover(function() {
        /// Affiche la pointe orange
        var _src = $("#second-nav img.jMenuPointe").attr("src");
        _src = _src.replace("pointe-haut.png", "pointe-haut-on.png");
        $("#second-nav img.jMenuPointe").attr("src", _src);      
    }, function() {
        /// Affiche la pointe blanche
        var _src = $("#second-nav img.jMenuPointe").attr("src");
        _src = _src.replace("pointe-haut-on.png", "pointe-haut.png");
        $("#second-nav img.jMenuPointe").attr("src", _src);
    });


    /// ---------------------------------------
    /// Click sur un sous menu
    /// ---------------------------------------
    var _hoverLinkSecondNav = false;
    $("#second-nav ul li").click(function() {
        if (!_hoverLinkSecondNav) {
            window.location = $(this).find("a").attr("href");
        }
    });

    $("#second-nav ul li a").hover(function() {
        _hoverLinkSecondNav = true;
    }, function() {
        _hoverLinkSecondNav = false;
    });
}