/**
 * Menu control
 * @version:  01.01.01 Alpha
 * @modified: 2009-09-24 13:15:00
 */
var menuControl = newClass({
    sMainElm : null,
    sSubElm  : null,

    curMenu  : null,
    curTimer : null,

    init : function(sMainElm, sSubElm)
    {
        this.sMainElm = sMainElm;
        this.sSubElm  = sSubElm;
        this.$w0.createStyle(".top_dropdown", "display: none;"); 
    },

    onready : function()
    {
        var aElm, a, subMn, dt, i;
        aElm = this.$$(this.sMainElm);

        for (i = 0; i < aElm.length; i++) {
            //a = aElm[i].getChild(0);
            a     = this.$("a", aElm[i]);
            subMn = this.$(this.sSubElm, aElm[i]);
            dt    = {"sm" : subMn, "mel" : aElm[i]};

            a.addListener(this, 'onmouseover', 'overMenu', dt);

            if (subMn) {
                a.addListener(this, 'onmouseout', 'outMenu');
                subMn.addListener(this, 'onmouseover', 'overMenu', dt);
                subMn.addListener(this, 'onmouseout', 'outMenu');
                subMn.addListener(this, 'onclick', 'onMenuClick');
            }
        }

        if (aElm.length > 0) {
            this.$w0.addListener(this, 'onclick', 'onBodyClick');
        }
    },

    overMenu : function (evtWr, dt)
    {
        this._setTimer(false);

        if (this.curMenu && this.curMenu != dt) this.hideCurrent();
        if (this.curMenu == dt || !dt.sm) return;

        dt.sm.move(evtWr.elmWr.getX(), evtWr.elmWr.getY() + evtWr.elmWr.getHeight() + this.config.shiftY);
        dt.sm.show();
        dt.mel.addClass("top_menu_active");

        this.curMenu = dt;
    },

    outMenu : function ()
    {
        this._setTimer(true);
    },
    onBodyClick : function (evtWr)
    {
        this._setTimer(false);
        this.hideCurrent();
    },
    onMenuClick : function (evtWr)
    {
        evtWr.stopBubbling();
    },

    hideCurrent : function ()
    {
        if (this.curMenu) {
            this.curMenu.sm.hide();
            this.curMenu.mel.removeClass("top_menu_active");
        }

        this.curMenu  = null;
        this.curTimer = null;
    },

    _setTimer : function (isStart)
    {
        if (this.curTimer) clearTimeout(this.curTimer);
        if (isStart) this.curTimer = this.setTimeout(this.config.outDelay, this, "hideCurrent");
    },

    config : {
        outDelay : 400,
        shiftY   : 1
    }

});

