﻿function DynamicMenu()
{

    this._show = false;
    this._menu = null;
    this._sender = null;
    
    this.INVISIBLE = "none";
    this.VISIBLE = "block";
    this.TOP = "224px";
    
    this.over = function(menu)
    {
        this._menu = menu.id || menu;
        this._hideAll();            
        this._element(menu).style.display = this.VISIBLE;
        this._element(menu).style.top = this.TOP;
        this._element(menu).style.left = this._getAbsolute("px").x;
    }
    
    this.out = function(menu)
    {
        this._menu = menu.id || menu;
        var thisObject = this;
        setTimeout(function() {thisObject._out()}, 200);
    }
    
    this.setSender = function(value)
    {
        this._sender = value;
    }
    
    this.setShow = function(value)
    {
        this._show = value;
    }
    
    this.hideAll = function()
    {
        this._hideAll();
    }
    
    this._element = function(id)
    {
        return document.getElementById(id);
    }
    
    this._out = function()
    {
        if (!this._show) 
        {
            this._element(this._menu).style.display = this.INVISIBLE;
            this._show = false;
        }
    }

    this._getAbsolute = function(unit)
    {
        unit = unit || '';
        var x = y = 0;
        var sender = this._sender;
        if (sender.offsetParent)
        {
            do
            {
                x += parseFloat(sender.offsetLeft);
                y += parseFloat(sender.offsetTop);
            }
            while (sender = sender.offsetParent);
        }
        return new Object({x:x + unit, y:y + unit})
    }
    
    this._hideAll = function()
    {
        if (!this._menu) return;
        var parent = this._element(this._menu).parentNode;
        for (var node = parent.firstChild; node != null; node = node.nextSibling)
        {
            if (node.nodeType == 1)
                node.style.display = this.INVISIBLE;
        }
    }
}

var dMenu = new DynamicMenu();
