﻿jQuery.Menu = {
    Timer: null,
    Open: new Array(),
    Expand: function (node) {
        var menu = $(">ul", node);
        if (menu.size()) {
            var t = this;
            $(">li", menu).hover(function () {
                t.Expand(this);
            }, function () {
                t.Collapse(this);
            });
            menu.show();
            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" });
                }
            }
        }
        this.Open.unshift(node);

        $(node).addClass(this.Open.length > 1 ? "ss" : "s");
    },
    Collapse: function (node) {
        var menu = $(">ul", node);
        if (menu.size()) {
            menu.hide();
            menu.css({ top: "0px" });
        }
        this.Open.shift();

        $(node).removeClass(this.Open.length > 0 ? "ss" : "s");
    },
    CollapseAll: function () {
        $(this.Open).find(">ul").hide();
    },
    Init: function (id) {
        var t = this;
        $("#" + id + ">ul>li").hover(function () {
            t.Expand(this);
        }, function () {
            t.Collapse(this);
        });
        $("#" + id + " a").click(function () {
            t.CollapseAll();
        });
        this.CollapseAll();
    }
};

$(document).ready(function() {
    try
    {
        $(".menu").each(function(index) {
            var menu = $.Menu;
            menu.Init($(this).attr("id"));
        });
    }
    catch (e)
    {
    }
});

