var thisSub;
var timerMenu;
var lastMenu;

(function($) {
    $.widget("ui.menu", {
        init: function() {
            var options = this.options;
            if (!options.interval)
                options.interval = 0;
            $('body').mouseover(function(e) {
                SelectMenu(null);
                lastMenu = null;
                e.stopPropagation();
            });

            $('>li', this.element).mouseover(function(e) {
                if (thisSub && thisSub.parent().attr("id") == $(this).attr("id")) {
                    //window.status = thisSub.parent().attr("id");
                    e.stopPropagation();
                    return;
                }
                var ele = $(this);
                $('div.subBack').show();
                $('>a', ele).css('color', '#ffffff');

                clearTimeout(timerMenu);
                timerMenu = setTimeout(function() {
                    $('ul.menu>li').removeClass('sfhover');
                    $('ul.menu>li>ul').hide();
                    ele.addClass('sfhover');
                    thisSub = $('>ul', ele);
                    thisSub.show();
                }, options.interval);
                lastMenu = ele;
                e.stopPropagation();
            });

            $('>li>ui', this.element).mouseover(function(e) {
                $(this).show();
                e.stopPropagation();
            });
        }
    });
})(jQuery);

var menuId = null;

function SelectMenu(id) {
    if (id) {
        menuId = id;
        menuId = menuId.toString().replace(/\s/g, "_");
        var menu = $('ul.menu>li[id="' + menuId + '"]');
        if ($('>ul', menu).length > 0)
            menu.trigger('mouseover');
        else 
        {
            clearTimeout(timerMenu);
            timerMenu = setTimeout(function() {
            thisSub = null;
            $('div.subBack').hide();
            $('ul.menu>li').removeClass('sfhover');
            $('ul.menu>li>ul').hide();
            },500);
        }
    }
   
}


