﻿
var Menus = new Array();

var MenuInHover = null;
var MenuHoverWait = 300;

function Menu(CreateInWindow, Opener, AlignLeft, Width, Id)
{
    this.CreateInWindow = CreateInWindow;
    this.AlignLeft = AlignLeft;
    this.Opener = Opener;
    this.Width = Width;
    this.Id = Id;
    this.Container = null;
    this.AppendContainer = null;

    this.Opened = false;
    this.HoveringOver = false;
    this.HoverOnTimeoutId = null;
    this.HoverOffTimeoutId = null;

    this.Sections = new Array();
    this.Sections.ParentMenu = this;
    this.Sections.Add = MenuSectionCollectionAdd;

    this.Create = MenuCreate;
    this.Open = MenuOpen;
    this.Close = MenuClose;
    this.HoverOn = MenuHoverOn;
    this.HoverOff = MenuHoverOff;

    Menus[Menus.length] = this;
}
function MenuSectionCollectionAdd(MenuSection)
{
    MenuSection.ParentMenu = this.ParentMenu;
    MenuSection.Index = this.length;

    for (var s = 0; s < MenuSection.Items.length; s++)
    {
        var Item = MenuSection.Items[s];

        Item.ParentMenu = MenuSection.ParentMenu;
        Item.ParentSection = MenuSection;
    }

    this[this.length] = MenuSection;
}
function MenuSection()
{
    this.ParentMenu = null;
    this.Index = null;
    this.Container = null;

    this.Items = new Array();
    this.Items.ParentSection = this;
    this.Items.Add = MenuItemCollectionAdd;
}
function MenuItemCollectionAdd(MenuItem)
{
    MenuItem.ParentMenu = this.ParentSection.ParentMenu;
    MenuItem.ParentSection = this.ParentSection;
    MenuItem.Index = this.length;

    this[this.length] = MenuItem;
}
function MenuItem(Text, NavigateUrl)
{
    this.ParentMenu = null;
    this.ParentSection = null;
    this.Index = null;
    this.Container = null;
    this.Anchor = null;
    this.Text = Text;
    this.NavigateUrl = NavigateUrl;
}
function MenuCreate()
{
    this.Container = this.CreateInWindow.document.createElement("div");
    this.Container.id = this.Id;
    this.Container.className = "Menu";

    for (var s = 0; s < this.Sections.length; s++)
    {
        var section = this.Sections[s];

        section.Container = this.CreateInWindow.document.createElement("ul");

        if (s > 0)
        {
            section.Container.className = "Second";
        }

        for (var i = 0; i < section.Items.length; i++)
        {
            var item = section.Items[i];

            item.Container = this.CreateInWindow.document.createElement("li");
            item.Anchor = this.CreateInWindow.document.createElement("a");
            item.Anchor.innerHTML = item.Text;
            item.Anchor.href = item.NavigateUrl;
            
            item.Container.appendChild(item.Anchor);
            section.Container.appendChild(item.Container);
        }
        
        this.Container.appendChild(section.Container);
    }

    if (this.AppendContainer)
    {
        this.AppendContainer.appendChild(this.Container);
    }
    else
    {
        this.CreateInWindow.document.body.appendChild(this.Container);
    }
}
function MenuHoverOn()
{
    if (!this.HoveringOver)
    {
        this.HoveringOver = true;

        //set timer and check if still hovering when expires
        MenuInHover = this;
        this.HoverOnTimeoutId = window.setTimeout(MenuHoverOnCheck, MenuHoverWait);
    }
}
function MenuHoverOnCheck()
{
    if (MenuInHover && MenuInHover.HoveringOver)
    {
        MenuInHover.Open();
    }
}
function MenuHoverOff()
{
    if (this.HoveringOver)
    {
        if (this.HoverOnTimeoutId)
        {
            window.clearTimeout(this.HoverOnTimeoutId);

            this.HoveringOver = false;
            this.HoverOnTimeoutId = null;
        }
    }

    if (this.Opened)
    {
        //set timer and check if still not hovering when expires
        MenuInHover = this;
        this.HoverOffTimeoutId = window.setTimeout(MenuHoverOffCheck, MenuHoverWait);
    }
}
function MenuHoverOffCheck()
{
    if (MenuInHover && !MenuInHover.HoveringOver)
    {
        MenuInHover.Close();
    }
}
function MenuOpen()
{
    if (!this.Opened)
    {
        MenuCloseAll();

        this.Opened = true;
        MenuShowing = true;

        var offset = $(this.Opener).offset();

        $(this.Container).css("top", offset.top + $(this.Opener).height() + 8 + "px");
        $(this.Container).width(this.Width + "px");

        if (this.AlignLeft)
        {
            $(this.Container).css("left", offset.left + "px");
        }
        else
        {
            $(this.Container).css("left", (offset.left - 3 - (this.Width - $(this.Opener).width())) + "px");
        }

        $(this.Container).show("fast");
    }
}
function MenuClose()
{
    if (this.Opened)
    {
        this.Opened = false;

        $(this.Container).hide("fast");
    }
}
function MenuCloseAll()
{
    for (var i = 0; i < Menus.length; i++)
    {
        Menus[i].Close();
    }
}
