﻿var PreviousMenu = null;
var Interval = 100;
var Timer = 0;
var MenuZindex = 100;
(function($) {

    $(document).ready(function() {
							   
        $('.menu').each(function() {
            var SubMenuId = $(this).attr('SubMenu');
            if (SubMenuId) {
                var _SubMenu = $('#' + SubMenuId);
                if (!_SubMenu) return false;
                SetSrollerHeight($(this), _SubMenu);
                $(this).bind('mouseenter', function() {
                    $('.menu-container').each(function() { $(this).hide(); });
                    ShowChildMenu(this, null);
                });
                $(this).bind('mouseleave', function() {
                    HideAllMenu();
                });
            }
        });
		
        $('.menu-container').each(function() {
			
            var MenuContainer = $(this)
            MenuContainer.css('display', 'none');
            MenuContainer.css('visibility', 'visible');
            MenuContainer.css('z-index', MenuZindex);

            elements = MenuContainer.find('div').find('div').find('div');
            elements.each(function() {
                if ($(this).attr('SubMenu')) {

                    var SubMenuId = $(this).attr('SubMenu');
                    var SubMenu = $('#' + SubMenuId);
                    if (!SubMenu) return false;
                    SetSrollerHeight($(this), SubMenu);
                    $(this).bind('mouseenter', function() 
					{
                        $(this).parent().find('div').each(function() {																   
                            var _SubMenu = $('#' + $(this).attr('SubMenu'))
                            if (_SubMenu) {
								_SubMenu.css('z-index', MenuZindex);
                                _SubMenu.hide();
                            }
                        });
                        ShowChildMenu(this, MenuContainer);
                    });
                    $(this).bind('mouseleave', function() {
                        $(this).parent().find('div').each(function() {
                            var _SubMenu = $('#' + $(this).attr('SubMenu'))
                            if (_SubMenu) {
								//_SubMenu.css('z-index',MenuZindex);
                                _SubMenu.hide();
                            }
                        });
                    });
                }
            });


            MenuContainer.bind('mouseenter', function() {
                clearTimeouts();
                $(this).show();
            });
            MenuContainer.bind('mouseleave', function() {
                clearTimeouts();
                HideAllMenu();
            });

        });

    });

})(jQuery);


function ShowChildMenu(menu, Container) {
    var Menu = $(menu);
    var SubMenuId = Menu.attr('SubMenu');
    var SubMenu = $('#' + SubMenuId);
	
    clearTimeouts();
    if (Container) 
	{
		SubMenu.css('z-index', (Container.css('z-index') * 1 + 2));
		Container.show();
	}
    var pos = Menu.offset();
    var Left = pos.left + Menu.width();
    var Top = pos.top;
    SubMenu.css({ "left": +(Left - 2) + "px", "top": +(Top - 2) + "px" });
    SubMenu.show();
    if (Left <= 0) {
        ShowChildMenu(menu, Container);
    }
}

function HideAllMenu() {
    $('.menu-container').each(function() {
        window.setTimeout('$("#' + this.id + '").hide();', Interval);
    });
}

function SetSrollerHeight(Menu, SubMenu) {
    var TotalHeight = 0;
    TotalHeight = Menu.attr('MenuHeight');
    var ParentMenu;

    var Scroller = SubMenu.children().children();
    if (TotalHeight && parseInt(TotalHeight) > 0) {
        Scroller.css('height', TotalHeight + 'px');
        Scroller = Scroller.children('.simply-scroll-clip');
        Scroller.css('height', TotalHeight + 'px');
    }
    else {
        Scroller.children().each(function() {
            if ($(this).attr("class") == 'simply-scroll-btn-container') {
                $(this).hide();
            }
        });
        Scroller.css("height", '100%')
        Scroller = Scroller.children('.simply-scroll-clip');
        Scroller.css("height", '100%');
        if ($.browser.msie == false) {
            SubMenu.css('padding-bottom', '0px');
            Scroller.parent().parent().css('margin-bottom', '0em');
            SubMenu.css('height', (SubMenu.height() - 18) + 'px');
        }
    }
}
