/*author: ducsduyen*/

$(document).ready(function () {
    var timeoutId, cleartimeout;
    var el = $("ul.nav li.over");

    /*Mouse event*/
    $("ul.nav>li").hover(function () {
        clearTimeout(timeoutId);
        $("ul.nav>li").removeClass('over');
        $(this).addClass('over');
    }, function () { //on hover out...
        clearTimeout(timeoutId);
        timeoutId = setTimeout(function () {
            $("ul.nav>li").removeClass('over');
            el.addClass("over");
        }, 1000);
    });

    /*Align submenu*/
    $('ul.nav>li').each(function () {
        var children = $(this).children("ul.nav>li>ul");
        if (!children) return;

        var intDis = $(this).offset().left - $("ul.nav").offset().left + $(this).outerWidth(true) / 2;
        var intsubw = itemW(children);
        var intmargin = Math.round(intDis - intsubw / 2);
        if ((intmargin + intsubw) > 950) intmargin = 950 - intsubw - 5;
        if (intmargin < 0) intmargin = 5;
        children.css("padding-left", intmargin);

        function itemW(objitem) {
            var w = 0;
            $(objitem).children("li").each(function () {
                w += $(this).outerWidth(true);
            });


            return Math.round(w);
        }
    });

    /*Set active menu item*/
    var location = document.location.href + '';
    if (document.location.pathname != "/") {
        $('ul.nav a').each(function (i, e) {
            var href = $(e).attr("href") + '';
            var breadcrumb = $.cookie('breadcrumb') + '';
            if (typeof (breadcrumb) == "string" && breadcrumb.length > 0 && href.indexOf(breadcrumb) > 0) {
                $(e).addClass('over'); el.removeClass('over');
                $(e).parents('ul.nav>li').addClass("over");
                el = $(e).parents('ul.nav>li');
            }

            if (location.indexOf(href) >= 0) {
                ismatch = true;
                $(e).addClass('over'); el.removeClass('over');
                $(e).parents('ul.nav>li').addClass("over");
                el = $(e).parents('ul.nav>li');
            }
        });
    } 
});  
