﻿(function($) {
    var menuAnimationLength = 200;
    var dropDownPersistInterval = 1000;
    $(function() {

        var rootMenu = $("ul.horizontal-menu");
        // find the third-level active item
        var thirdLevelActive = $("ul.dropdown-menu>li.active");
        if (thirdLevelActive.length > 0) {
            var secondLevel = $("ul.horizontal-submenu>li[data-open=" + thirdLevelActive.parent().attr("data-menu-id") + "]").addClass("active").parent().show();
            var firstLevel = rootMenu.find("li[data-open=" + secondLevel.attr("data-menu-id") + "]").addClass("active");
        } else {
            // find the second-level active item
            var secondLevelActive = $("ul.horizontal-submenu>li.active");
            if (secondLevelActive.length > 0) {
                secondLevelActive.parent().show();
                rootMenu.find("li[data-open=" + secondLevelActive.parent().attr("data-menu-id") + "]").addClass("active");
            } else {
                // find the corresponding second level and display it
                var activeRoot = rootMenu.find("li.active");
                if (activeRoot.length > 0) {
                    $("ul[data-menu-id=" + activeRoot.attr("data-open") + "]").show();
                }
            }
        }
        // set up click handler for root menu
        rootMenu.find(">li").each(function() {
            var current = $(this);
            // find corresponding submenu and attach handler
            var submenu = $("ul[data-menu-id=" + current.attr("data-open") + "]");
            if (submenu.length > 0) {
                // found submenu, attach click handler for root menu entry
                current.click(function() {
                    window.location = submenu.find("a:first").attr("href");
                    /*
                    $("ul.horizontal-menu>li").removeClass("active");
                    current.addClass("active");
                    $("ul.horizontal-submenu,ul.dropdown-menu").not(submenu).slideUp(menuAnimationLength);
                    submenu.slideDown(menuAnimationLength);
                    */
                    return false;
                });
            }
        });

        // set up hover display for dropdowns
        $("ul.horizontal-submenu>li").each(function() {
            var current = $(this);
            // find correct submenu, if exists, and set up hover
            var submenu = $("ul[data-menu-id=" + current.attr("data-open") + "]");
            if (submenu.length > 0) {
                // found corresponding dropdown target
                current.mouseenter(function() {
                    // check if the current dropdown is already displayed
                    if (!submenu.is(":visible")) {
                        $("ul.dropdown-menu").slideUp(menuAnimationLength);
                        submenu.css({
                            left: current.offset().left + "px",
                            top: current.offset().top + current.height() + 6 + "px"
                        }).slideDown(menuAnimationLength);
                    }
                });
                var timerInt;
                submenu.mouseleave(function() {
                    timerInt = setTimeout(function() { submenu.slideUp(menuAnimationLength); }, dropDownPersistInterval);
                });
                submenu.mouseenter(function() {
                    if (timerInt !== null) {
                        clearTimeout(timerInt);
                        timerInt = null;
                    }
                });
            } else {
                current.mouseenter(function() {
                    $("ul.dropdown-menu").slideUp(menuAnimationLength);
                });
            }
        });
        $("body").click(
            function() { $("ul.dropdown-menu").slideUp(menuAnimationLength); }
        );
        $(window).resize(
            function() { $("ul.dropdown-menu").slideUp(menuAnimationLength); }
        );
    });
})(jQuery);