﻿
MainMenu = function (id, lang) {
    this.Init(id);
    this._el = null;
    this._init = true;
    this._lang = lang || '0';
}

MainMenu.prototype.Init = function (id) {
    var i = 1, o, li, anchor, attrName, mm = this;
    this._menuBlock = new Array();
    this._timer = new Array();
    this._init = true;

    try {
        this._el = document.getElementById(id.toString()) || null;
        if (this._el == null) throw Error('找不到指定的主選單區塊');
        li = this._el.getElementsByTagName('li');
        for (i; i <= li.length; i++) {
            o = li[i - 1];
            attrName = o.attributes.name;
            if (attrName != undefined && attrName.value == 'mmBlock') {
                this._menuBlock.push(o);
                $(o).bind({
                    "mouseover": function () { mm.ShowSubMenu(this.id); },
                    "focusin": function () { mm.ShowSubMenu(this.id); },
                    "mouseout": function () { mm.HiddenSubMenu(this.id); },
                    "focusout": function () { mm.HiddenSubMenu(this.id); }
                });
            }
        }

        this.ShowSubMenu();
    }
    catch (e) {
        alert(e.message);
    }
    finally {
        i = null;
        o = null;
        attrName = null;
    }
}

/**
 * 顯示次選單
 * @param {string} id - 主選單id
 */
MainMenu.prototype.ShowSubMenu = function (mid) {
    var i = 1, ul, scope = this, img;

    try {
        clearTimeout(this._timer[mid]);
        this._timer[mid] = null;

        for (i; i <= this._menuBlock.length; i++) {
            ul = this._menuBlock[i - 1].getElementsByTagName('ul');
            img = this._menuBlock[i - 1].getElementsByTagName('img');
            if (ul.length == 0) continue;

            if ((mid != undefined && mid != null) && this._menuBlock[i - 1].id == mid.toString()) {
                if ($(ul[0]).css("visibility") == "visible") continue;
                if ($.browser.msie && $.browser.version.toString() == "6.0") {
                    if (this._lang == '1') {
                        $(ul[0]).attr("style", "visibility:visible; display:block; overflow:hidden; width:".concat($(img)[0].scrollWidth, "px"));
                    }
                    else {
                        $(ul[0]).attr("style", "visibility:visible; display:block; z-index:5000; top:35px; left:0px; position:absolute; width:".concat($(img)[0].scrollWidth, "px"));
                    }
                }
                else {
                    $(ul[0]).attr("style", "visibility:visible; display:block; overflow:hidden; width:".concat($(img)[0].scrollWidth, "px"))
                    .animate({ height: ul[0].scrollHeight - 40 }, 150);
                }
            }
            else {
                if (this._init) {
                    $(ul[0]).attr("style", "visibility:hidden; display:none; overflow:hidden; background-image:none;");
                } else {
                    $(ul[0]).attr("style", "visibility:hidden; display:none; height:20px; top:-30px; background-image:none;");
                }
            }
        }
        this._init = false;
    }
    catch (ex) {
        return;
    }
    finally {
        i = null;
        ul = null;
    }
}

/**
 * 隱藏次選單
 * @param {string} id - 主選單id
 */
MainMenu.prototype.HiddenSubMenu = function (mid) {
    try {
        if ($.browser.msie && $.browser.version.toString() == "6.0") {
            $('#'.concat(mid.toString(), " > ul")).attr("style", "top:-5000px; position:absolute;");
        }
        else {
            this._timer[mid] = setTimeout(function () {
                var el = $('#'.concat(mid.toString(), " > ul"))[0];
                //此為計時結束觸發事件
                clearTimeout(this);
                $(el).animate({
                    height: 0
                }, 300, function () {
                    $(el).attr("style", "visibility:hidden; display:none; height:0px; top:-30px; background-image:none;");
                });
            }, 0);
        }
    }
    catch (ex) {
        return;
    }
};
