// NEW MULTILAYER NAVIGATION 

jQuery(document).ready(function () {
    // add First and last classes
    // in order to realize "slanted" boxes


    jQuery('ul.nav-a > li:first').addClass('first');
    jQuery('ul.nav-a > li:last').addClass('last');
    jQuery('ul.nav-a > li').hover(function () {
        if (jQuery(this).prev().hasClass("act")) {
            jQuery(this).prev().addClass('act-previous-current');
        }
        else {
            jQuery(this).prev().addClass('previous-current');
        }
        if (jQuery(this).next().hasClass("act")) {
            jQuery(this).next().addClass('act-next-current');
        }
        else {
            jQuery(this).next().addClass('next-current');
        }
        if (jQuery(this).hasClass("act")) {
            jQuery(this).prev().addClass('previous-current-act');
        }

        doPosition(jQuery(this));

    }, function () {
        if (jQuery(this).prev().hasClass("act")) {
            jQuery(this).prev().removeClass('act-previous-current');
        }
        else {
            jQuery(this).prev().removeClass('previous-current');
        }
        if (jQuery(this).next().hasClass("act")) {
            jQuery(this).next().removeClass('act-next-current');
        }
        else {
            jQuery(this).next().removeClass('next-current');
        }
        if (jQuery(this).hasClass("act")) {
            jQuery(this).prev().removeClass('previous-current-act');
        }

    });

    jQuery('.nav-a > li.act').prev().addClass('previous-act');
    jQuery('.nav-a > li.act').next().addClass('next-act');

    // New Subnavigation Multilevel Dropdown
    // now with timeout when hover on menuitems level 1 finished

    var opened = false;
    var opened2 = false;
    var toID;
    var toID2;

    jQuery(".nav-b ul").css({display: "none"}); // Opera fixed()
    jQuery(".nav-a ul.nav-b.openable").css({display: "none"}); // Opera fixed()


    function doPosition(t) {
        t.each(function() {
            var offset = jQuery(this).position().left;
            jQuery(this).find('ul.nav-b').css('margin-left', -offset);
        });
    }

    function show(t, to) {
        var state = jQuery(t).find('ul:first').data("hidden");

        // clear timeout
        if (to) {
            clearTimeout(to);
            to = undefined;
        }

        if (state == true || state == undefined) {
            // hide all other elements
            jQuery(t).parent().find('>li').each(function(index, obj) {
                // close all other branches of the same level immediately
                if (index != jQuery(t).index()) {
                    hide(obj);
                }
            })
            jQuery(t).find('ul:first').css({visibility: "visible", display: "none"}).show(100);
            jQuery(t).find('ul:first').data("hidden", false);
            jQuery(t).addClass('over');
        }
    }

    function hide(t) {
        jQuery(t).find('ul').hide(100, function() {
            jQuery(this).data("hidden", true);

        });
        jQuery(t).removeClass('over');
    }

    function hideMain() {
        hide(opened);
    }

    function hideMain2() {
        hide(opened2);
    }

    function showLevel2(evt) {
        opened = evt.currentTarget;
        show(opened, toID);
    }

    function hideLevel2(evt) {
        toID = setTimeout(hideMain, 1000);
    }

    function showLevel3(evt) {
        show(evt.currentTarget, toID);
    }

    function hideLevel3(evt) {
        hide(evt.currentTarget);
    }

    function showLevelRoot(evt) {
        opened2 = evt.currentTarget;
        show(opened2, toID2);
    }

    function hideLevelRoot(evt) {
        toID2 = setTimeout(hideMain2, 1000);
    }

    function initialize() {
        jQuery(".nav-b").not('.openable').css({display: "block"});
        // level1 menu-items should have a timeout, only if subitems are there
        jQuery(".nav-b>li").has("ul").hover(showLevel2, hideLevel2);
        // leve2 menu-items should not
        jQuery(".nav-b li li").hover(showLevel3);
        // level1 menu-items should have a timeout, only if subitems are there
        jQuery(".nav-a>li").has("ul.openable").hover(showLevelRoot, hideLevelRoot);
        // reposition submenu
        doPosition(jQuery('ul.nav-a>li').has('ul'));
    }

    // wait until fonts are loaded
    jQuery(window).bind("load", function() {
           initialize();
    });

});
