/**
 * @fileoverview HKMA TableHighlight 核心文件。
 */

/**
 * HKMA TableHighlight 表格高亮组件。
 * @class 该组件“继承”于 HKMA MagicHighlight,并专注于表格的输出。<br/>
 * <i><a href="http://source.dev.hkma.org.gz/common/util/tableHighlight/doc/tableHighlight.htm">
 * 相关代码示例及功能演示。
 * </a></i>
 * @constructor
 * @extends HKMA.MagicHighlight
 * @param {String} effectDie 效果消逝(事件名)
 * @param {String} effectHold 效果保持(事件名)
 * @param {int} holdLimit 保持限制
 * @param {String} tableId 表格标识
 * @param {String} className 样式表名
 * @param {String} effectActivate 效果激活(事件名)
 * @param {function} byTheWay 高亮事后接口
 */
HKMA.TableHighlight = function (effectDie,effectHold,holdLimit,tableId,className,effectActivate,byTheWay)
{
    HKMA.MagicHighlight.call(this,effectDie,effectHold,holdLimit);
    
    /**
     * 表格标识
     * @type String
     */
    this.tableId = tableId;
    
    /**
     * 高亮样式
     * @type String
     */
    this.className = className;
    
    /**
     * 效果激活(事件名)
     * @type String
     */
    this.effectActivate = effectActivate;
    
    /**
     * 高亮事后接口
     * @type function
     */
    this.byTheWay = byTheWay;

    HKMA.TableHighlight.init(this);
};

/**
 * TableHighlight 设置。
 * @param {Array} params TableHighlight 设置项
 * @type Object
 * @return TableHighlight 对象
 */
HKMA.TableHighlight.setup = function (params)
{
    paramsList = ["id"];
    
    function param_default(pname, value)
    {
        if (typeof params[pname] == "undefined")
        {
            params[pname] = value;
        }
        paramsList.push(pname);
    };
    
    // 接收事件名指定高亮效果何时消逝
    
    param_default("effectDie","onmouseout");
    
    // 接收事件名指定高亮效果何时保持。
    
    param_default("effectHold","onclick");
    
    // 接收数字指定高亮效果保持的配额。
    
    param_default("holdLimit","never");
    
    // 接收事件名指定高亮效果何时击活。
     
    param_default("effectActivate","onmouseover");
    
    /* 接收函数名指定高亮后执行的方法。
     * 内置插件参数: clickCheckBox, 当每行首个单元中包含复选
     * 框,该复选框将关联与高亮组件相同的正/反选择。
     */

    param_default("byTheWay","undefined");
    
    var tableHighlight = new HKMA.TableHighlight(params.effectDie,
                                                 params.effectHold,
                                                 params.holdLimit,
                                                 params.tableId,
                                                 params.className,
                                                 params.effectActivate,
                                                 params.byTheWay);
    return tableHighlight;
};

/**
 * TableHighlight 初始化。
 * @param {TableHighlight} _this TableHighlight 对象
 */
HKMA.TableHighlight.init = function(_this)
{
   window.onload = function anonymous()
   {
        var activateFunc = function anonymous()
        {
            var byTheWay = _this.byTheWay;
            var currentRow = this;

            byTheWay = function anonymous()
            {
                _this.byTheWay(_this, currentRow);
            }
            
            //////////////////////////////////////////////////////
            // ToDo: 以下代码为临时代码,原因为解决当点选"复选框"时
            // 高亮状态并不能瞬时响应"复选框"的状态,而导致该情况的
            // 原因可能是 MagicHighlight 中的 kindle 方法对于"鼠标
            // 动移"和"鼠标点击"的代码逻辑并未拆分.///////////////
            
            
            var checkBox = currentRow.childNodes[0].childNodes[0];
            
            if(checkBox.tagName == "INPUT")
            {
                checkBox.setAttribute(_this.effectHold, byTheWay);
            }
            
            //////////////////////////////////////////////////////
            
            _this.kindle(_this.className, this, byTheWay);
        };
        
        var rows = _this.takeRows();

        for (var num = 0; num < rows.length; num++)
        {
            rows[num].setAttribute(_this.effectActivate ,activateFunc);
        }
   }
};

/**
 * 获取父类所有方法。
 */
HKMA.TableHighlight.prototype = new HKMA.MagicHighlight();

/**
 * 获取表格中的所有行。
 * @type Object
 * @return 表格的所有的行对象
 */
HKMA.TableHighlight.prototype.takeRows = function()
{
    var table = _get(this.tableId);
    
    for (var num = 0; num < table.childNodes.length; num++)
    {
        if(table.childNodes[num].tagName == "TBODY")
        {
            return table.childNodes[num].childNodes;
        }
    }
};

