﻿jQuery.Menu = {
    Open: new Array(),
    Position: function(id) {
        var width = $('#' + id + ' ul>li').width();
        $('#' + id + ">ul>li ul").each(function() {
            $(this).css({ position: "absolute", top: "0px", left: width + "px" });
        });
    },
    Expand: function(node) {
        $(node).addClass("s");
            
        var menu = $(">ul", node);
        if (menu.size()) {
            menu.show();
            this.Open.unshift(menu.get(0));
            if (menu.offset()) {
                var corr = $(window).height() + $(window).scrollTop() - menu.offset().top - menu.outerHeight();
                if (corr < 0) {
                    var height = $(">a", node).outerHeight();
                    menu.css({ top: (Math.floor(corr / height) * height) + "px" });
                }
            }
        }
    },
    Collapse: function(node) {
        $(node).removeClass("s");

        var menu = $(">ul", node);
        menu.hide();
        menu.css({ top: "0px" });
        this.Open.shift();
    },
    CollapseAll: function() {
        $(this.Open).hide();
    },
    Init: function(id) {
        var t = this;
        $("#" + id + " li").hover(function() {
            t.Expand(this);
        }, function() {
            t.Collapse(this);
        });
        $("#" + id + " a").click(function() {
            t.CollapseAll();
        });
        this.Position(id);
        this.CollapseAll();
    }
};

$(document).ready(function() {
    try
    {
        $(".menu").each(function(index) {
            var menu = $.Menu;
            menu.Init($(this).attr("id"));
        });
    }
    catch (e)
    {
    }
});
