//千一网络 www.cftea.com
//CBoardExhibition v1.0

//objStr 当前对象的名称，字符串类型
//targetStr 要显示消息的 HTML 元素标签的 id，字符串类型
//msgDepth 消息来源，即消息内容与 targetStr 相差几个结点
//autoClone 是否自动克隆一份滚动内容，由于需要两份一样的滚动内容才能实现滚动效果，所以此参数决定是否由程序自动克隆其内容
//direction 滚动方向，可选值：top、down、left、right
//msgScrollDelay 滚动延迟，单位毫秒
//msgScreenDelay 屏停时间，单位毫秒
function CBoardExhibition(objStr, targetStr, msgDepth, autoClone, direction, msgScrollDelay, msgScreenDelay)
{
    this.objStr = objStr;
    this.target = document.getElementById(targetStr);
    this.target.style.overflow = "hidden";

    //以下两个函数需要提前申明
    this.CreateNodesPath = CBoardExhibition_CreateNodesPath; //私有函数，创建 Nodes 路径
    this.CleanseInvalidNodes = CBoardExhibition_CleanseInvalidNodes; //私有函数，在 NS 系列浏览器中清除 Nodes 的无意义 Node
    this.CloneMsgData = CBoardExhibition_CloneMsgData; //私有函数，如果做成公有函数由用户调用则对 width、height 支持不好
    
    this.msgDepth = msgDepth;
    this.direction = direction.toLowerCase();
    this.CleanseInvalidNodes();
    this.msgCnt = 0;
    if (autoClone)
    {
        this.msgCnt = (eval("this.target" + this.CreateNodesPath(this.msgDepth-1) + ".childNodes.length"));
        this.CloneMsgData();
    }
    else
    {
        this.msgCnt = (eval("this.target" + this.CreateNodesPath(this.msgDepth-1) + ".childNodes.length")) / 2;
    }
    
    this.msgMinIndex = 0;
    this.msgMaxIndex = 0;
    this.msgIndex = -1;
    
    if (this.direction=="up" || this.direction=="left")
    {
        this.msgMinIndex = 0;
        this.msgMaxIndex = this.msgCnt - 1;
        this.msgIndex = this.msgMinIndex - 1;
    }
    else if (this.direction=="down" || this.direction=="right")
    {
        this.msgMinIndex = this.msgCnt;
        this.msgMaxIndex = this.msgCnt*2 - 1;
        this.msgIndex = this.msgMaxIndex + 1;
    }
    
    this.msgItemWidth = this.target.offsetWidth;
    this.msgItemHeight = this.target.offsetHeight;
    this.msgScrollDelay = msgScrollDelay; //滚动延迟时间
    this.msgScreenDelay = msgScreenDelay; //一屏延迟时间
    
    this.timer = null;

    this.ShowMsg = CBoardExhibition_ShowMsg;
    this.ShowMsgUp = CBoardExhibition_ShowMsgUp; //由 this.ShowMsg 统一调用，统一管理
    this.ShowMsgDown = CBoardExhibition_ShowMsgDown; //由 this.ShowMsg 统一调用，统一管理
    this.ShowMsgLeft = CBoardExhibition_ShowMsgLeft; //由 this.ShowMsg 统一调用，统一管理
    this.ShowMsgRight = CBoardExhibition_ShowMsgRight; //由 this.ShowMsg 统一调用，统一管理
}

function CBoardExhibition_CreateNodesPath(depth)
{
    var i = 0;
    var str = "";
    for (i=0; i<depth; i++)
    {
        str += ".childNodes[0]";
    }
    
    return str;
}

//由于 Netscape、FireFox 之类浏览器会把空格换行也当作一种节点
//而我们在写 HTML 时往往为了格式的考虑都会有空格换行
//为了使这类浏览器能够正常解析，使用以下这段代码除去这类节点
function CBoardExhibition_CleanseInvalidNodes()
{
    var i = 0;
    var j = 0;
    var k = 0;
    for (i=0; i<this.msgDepth; i++)
    {
        var strp = this.CreateNodesPath(i);
        var str = strp + ".childNodes";

        for (k=0; k<eval("this.target" + str + ".length"); k++)
        {
            if (eval("this.target" + str + "[k].nodeType") == 3)
            {
                var objp = eval("this.target" + strp);
                objp.removeChild(eval("this.target" + str + "[k]"));
            }
        }
    }
}

//克隆消息
function CBoardExhibition_CloneMsgData()
{
    var str = this.CreateNodesPath(this.msgDepth-1);
    
    for (i=0; i<this.msgCnt; i++)
    {
        eval("this.target"+str+".appendChild(this.target"+str+".childNodes["+i+"].cloneNode(true))");
    }
}

//显示消息
function CBoardExhibition_ShowMsg()
{
    if (this.direction == "up")
    {
        this.target.scrollTop = 0;
        this.ShowMsgUp();
    }
    else if (this.direction == "down")
    {
        this.target.scrollTop = this.msgMaxIndex * this.msgItemHeight;
        this.ShowMsgDown();
    }
    else if (this.direction == "left")
    {
        this.target.scrollLeft = 0;
        this.ShowMsgLeft();
    }
    else if (this.direction == "right")
    {
        this.target.scrollLeft = this.msgMaxIndex * this.msgItemWidth;
        this.ShowMsgRight();
    }
}

//上滚
function CBoardExhibition_ShowMsgUp()
{
    if (this.msgIndex > this.msgMaxIndex)
    {
        //一个周期已经滚完，从头再开始
        this.msgIndex = 0;
        this.target.scrollTop = 0;
    }

    if (this.target.scrollTop < (this.msgIndex+1)*this.msgItemHeight)
    {
        //一屏还没有滚到头
        this.target.scrollTop++;
        this.timer = setTimeout(this.objStr+".ShowMsgUp();", this.msgScrollDelay);
    }
    else
    {
        //一屏已经滚完，继续下一屏
        this.msgIndex++;
        this.timer = setTimeout(this.objStr+".ShowMsgUp();", this.msgScreenDelay);
    }
}

//下滚
function CBoardExhibition_ShowMsgDown()
{
    if (this.msgIndex < this.msgMinIndex)
    {
        //一个周期已经滚完，从头再开始
        this.msgIndex = this.msgMaxIndex;
        this.target.scrollTop = this.msgMaxIndex*this.msgItemHeight;
    }
    
    if (this.target.scrollTop > (this.msgIndex-1)*this.msgItemHeight)
    {
        //一屏还没有滚到头
        this.target.scrollTop--;
        this.timer = setTimeout(this.objStr+".ShowMsgDown();", this.msgScrollDelay);
    }
    else
    {
        //一屏已经滚完，继续下一屏
        this.msgIndex--;
        this.timer = setTimeout(this.objStr+".ShowMsgDown();", this.msgScreenDelay);
    }
}

//左滚
function CBoardExhibition_ShowMsgLeft()
{
    if (this.msgIndex > this.msgMaxIndex)
    {
        //一个周期已经滚完，从头再开始
        this.msgIndex = 0;
        this.target.scrollLeft = 0;
    }
    
    if (this.target.scrollLeft < (this.msgIndex+1)*this.msgItemWidth)
    {
        //一屏还没有滚到头
        this.target.scrollLeft++;
        this.timer = setTimeout(this.objStr+".ShowMsgLeft();", this.msgScrollDelay);
    }
    else
    {
        //一屏已经滚完，继续下一屏
        this.msgIndex++;
        this.timer = setTimeout(this.objStr+".ShowMsgLeft();", this.msgScreenDelay);
    }
}

//右滚
function CBoardExhibition_ShowMsgRight()
{
    if (this.msgIndex < this.msgMinIndex)
    {
        //一个周期已经滚完，从头再开始
        this.msgIndex = this.msgMaxIndex;
        this.target.scrollLeft = this.msgMaxIndex*this.msgItemWidth;
    }
    
    if (this.target.scrollLeft > (this.msgIndex-1)*this.msgItemWidth)
    {
        //一屏还没有滚到头
        this.target.scrollLeft--;
        this.timer = setTimeout(this.objStr+".ShowMsgRight();", this.msgScrollDelay);
    }
    else
    {
        //一屏已经滚完，继续下一屏
        this.msgIndex--;
        this.timer = setTimeout(this.objStr+".ShowMsgRight();", this.msgScreenDelay);
    }
}
