/**
 * @fileoverview HKMA FlashMenu 核心文件。
 */

/**
 * HKMA FlashMenu 瞬时菜单。
 * @class 可定制式菜单容器，自由添加/移除菜单项。<br/>
 * <i><a href="http://source.dev.hkma.org.gz/common/util/flashMenu/doc/flashMenu.htm">
 * 相关代码示例及功能演示。
 * </a></i>
 * @constructor
 * @param {String} outerClass 外层样式(center,left,right)
 * @param {String} innerClass 内层样式(center,left,right)
 * @param {String} outerAlign 外层定位(center,left,right)
 * @param {String} innerAlign 内层定位(center,left,right)
 * @param {String} lay 菜单被包含位置(即某HTML元素ID)
 */
HKMA.FlashMenu = function(outerClass,innerClass,outerAlign,innerAlign,lay)
{
    /**
     * 外层样式(center,left,right)
     * @type String
     */
    this.outerClass = outerClass;
    
    /**
     * 内层样式(center,left,right)
     * @type String
     */
    this.innerClass = innerClass;
    
    /**
     * 外层定位(center,left,right)
     * @type String
     */
    this.outerAlign = outerAlign;
    
    /**
     * 内层定位(center,left,right)
     * @type String
     */
    this.innerAlign = innerAlign;
    
    /**
     * 菜单被包含位置(即某HTML元素ID)
     * @type String
     */
    this.lay = lay;

    /**
     * FlashMenu 内部唯一标识
     * @type String
     */
    this.id = HKMA.FlashMenu.makeID("flashMenu");
    
    HKMA.FlashMenu.fanning(this);
};

/**
 * FlashMenu 设置。
 * @param {Array} params FlashMenu 设置项
 * @type Object
 * @return FlashMenu 对象
 */
HKMA.FlashMenu.setup = function (params)
{
    paramsList = ["id"];
    
    function param_default(pname, value)
    {
        if (typeof params[pname] == "undefined")
        {
            params[pname] = value;
        }
        paramsList.push(pname);
    };
    
    // 菜单外层 CSS 样式引用名。
    
    param_default("outerClass","flashMenu_outer");
    
    // 菜单内层 CSS 样式引用名。
    
    param_default("innerClass","flashMenu_inner");
    
    // 菜单外层水平定位。
    
    param_default("outerAlign","center");
    
    // 菜单内层水平定位。
     
    param_default("innerAlign","left");

    var flashMenu = new HKMA.FlashMenu(params.outerClass,params.innerClass,params.outerAlign,params.innerAlign,params.lay);

    return flashMenu;
};

/**
 * 根据设置铺开菜单。
 * @param {FlashMenu} flashMenu 对象
 */
HKMA.FlashMenu.fanning = function (flashMenu)
{
    innerLayer = new HKMA.FlashMenu.layer();
    outerLayer = new HKMA.FlashMenu.layer();
    
    // className 样式属性的适用于 IE，class 则适用于其它浏览器。
    
    innerLayer.table.setAttribute("class",flashMenu.innerClass);
    innerLayer.table.setAttribute("className",flashMenu.innerClass);
    innerLayer.table.setAttribute("align",flashMenu.innerAlign);
    
    outerLayer.table.setAttribute("class",flashMenu.outerClass);
    outerLayer.table.setAttribute("className",flashMenu.outerClass);
    outerLayer.table.setAttribute("align",flashMenu.outerAlign);
    
    innerLayer.tr.setAttribute("id",flashMenu.id);

    outerLayer.td.appendChild(innerLayer.table);

    _get(flashMenu.lay).appendChild(outerLayer.table);
};

/**
 * 菜单层次结构。
 * @type Object
 * @return 表格结构。
 */
HKMA.FlashMenu.layer = function ()
{
    this.table = document.createElement("table");
    this.tbody = document.createElement("tbody");
    this.tr = document.createElement("tr");
    this.td = document.createElement("td");
    
    this.tr.appendChild(this.td);
    this.tbody.appendChild(this.tr);
    this.table.appendChild(this.tbody);
    
    return this;
};

/**
 * 容纳每个新建菜单编号。
 */
HKMA.FlashMenu._ids = {};

/**
 * 构成新菜单编号。
 * @param {String} code 代号
 * @param {int} id 编号，如果这个参数不存在则为自增量。
 * @type String
 * @return 菜单编号
 */
HKMA.FlashMenu.makeID = function(code, id)
{
    if (typeof id == "undefined")
    {
        if (typeof this._ids[code] == "undefined")
        {
            this._ids[code] = 0;
        }
        
        id = ++this._ids[code];
        
    }
    
    return code + id;
};

/**
 * 增加一个菜单项。
 * @param {String} id 菜单项唯一标识。
 * @param {String} code 菜单项内容(支持HTML代码)。
 */
HKMA.FlashMenu.prototype.addOne = function (id,code)
{
    td = document.createElement("td");
    
    td.setAttribute("id",id);
    
    td.innerHTML = code;
    
    // this.id 获取的是当前行(TR)的对象.
    
    document.getElementById(this.id).appendChild(td);
};

/**
 * 移除一个菜单项。
 * @param {String} id 菜单项唯一标识。
 */
HKMA.FlashMenu.prototype.delOne = function (id)
{
    td = document.getElementById(id);
    
    if(null != td)
    {
        // this.id 获取的是当前行(TR)的对象.
        
        document.getElementById(this.id).removeChild(td);
    }
};

/**
 * 增加多个菜单项。
 * @param {String} id 菜单项唯一标识数组。
 * @param {String} code 菜单项内容数组(支持HTML代码)。
 */
HKMA.FlashMenu.prototype.addMany = function (id,code)
{
    if(id.length == code.length)
    {
        for(var item in id)
        {
            this.addOne(id[item],code[item]);
        }
    }
};

/**
 * 移除多个菜单项。
 * @param {String} id 菜单项唯一标识数组。
 */
HKMA.FlashMenu.prototype.delMany = function (id)
{
    for(var item in id)
    {
        this.delOne(id[item]);
    }
};

/**
 * 显示 FlashMenu 内的菜单项。
 */
HKMA.FlashMenu.prototype.hide = function ()
{
    document.getElementById(this.id).style.display="none";
};

/**
 * 隐藏 FlashMenu 内的菜单项。
 */
HKMA.FlashMenu.prototype.show = function ()
{
    document.getElementById(this.id).style.display="block";
};

/**
 * 重新装载 FlashMenu 菜单项
 * @param {Array} id 列表项标识数组。
 * @param {Array} code 列表项内容数组。
 */
HKMA.FlashMenu.prototype.repack = function (id,code)
{
   var oldTr = document.getElementById(this.id);
   var newTr = document.createElement("tr");
   var tbody = oldTr.parentElement;
   
   newTr.setAttribute("id",oldTr.id);
   tbody.removeChild(oldTr);
   tbody.appendChild(newTr);

   this.addMany(id,code);
}

