﻿/* 
* ContentSwitcher: 页面内容滚动器
* 公司：易企达（北京）科技有限公司
* 开始：2009-06-10
* 目的：实现页面上指定模块内容可以自动滚动（左右或上下）
* 历史：
*/

function $(aID)
{
    return document.getElementById(aID);
}
function _ScrollCallHelper(aObject, aType)
{
    if (aType == 0)
        return function() { aObject.Start(); };
    else if (aType == 1)
        return function() { aObject.Roll(); };
    else if (aType == 2)
        return function() { if (aObject.Scroller != null) aObject.Scroller.Pause(); };
    else if (aType == 3)
        return function() { if (aObject.Scroller != null) aObject.Scroller.Start(); };
    else
        return function() { alert(aType); };
}
function Scroller(direction, speed, step)
{
    this.Direction = direction;
    if (this.Direction != "up" && this.Direction != "left")
        this.Direction = "up";
    this.Speed = speed;
    if (isNaN(this.Speed))
        this.Speed = 400;
    if (this.Speed < 100)
        this.Speed = 100;

    this.Step = step;
    if (isNaN(this.Step))
        this.Step = 10;
    if (this.Step < 5)
        this.Step = 5;

    //是否可以滚动
    this.RollAble = true;

    this.CanvaList = new Array();
    this.Interval = 0;
}

Scroller.prototype.AddCanvas = function(aContentID, aWidth, aHeight, aStyle)
{
    var canv = new ScrollCanvas(aContentID, this, aWidth, aHeight, aStyle);
    if (canv)
    {
        this.CanvaList.push(canv);
    }

    return canv;
}
Scroller.prototype.Roll = function()
{
    if (!this.RollAble) return;

    var Canva = this.CanvaList[0];
    if (this.Direction == 'up')
    {
        if (Canva.Div2.offsetHeight - Canva.Contenter.scrollTop <= 0)
            Canva.Contenter.scrollTop -= Canva.Div1.offsetHeight - this.Step;
        else
        {
            Canva.Contenter.scrollTop += this.Step;
        }
    }
    else if (this.Direction == "left")
    {
        if (Canva.Div2.offsetWidth - Canva.Contenter.scrollLeft <= 0)
            Canva.Contenter.scrollLeft -= Canva.Div1.offsetWidth - this.Step;
        else
        {
            Canva.Contenter.scrollLeft += this.Step;
        }
    }

    //    if (this.Direction == 'left')
    //    {
    //        if (Canva.Div2.offsetHeight - Canva.Contenter.scrollTop <= 0)
    //            Canva.Contenter.scrollTop -= Canva.Div1.offsetHeight - this.Step;
    //        else
    //        {
    //            Canva.Contenter.scrollTop += this.Step;
    //        }
    //    }
}
Scroller.prototype.Start = function()
{
    if (!this.RollAble) return;

    if (this.Interval == 0)
    {
        this.Interval = setInterval(_ScrollCallHelper(this, 1), this.Speed);
    }
}
Scroller.prototype.Pause = function()
{
    if (this.Interval != 0)
    {
        clearInterval(this.Interval);
        this.Interval = 0;
    }
}

function ScrollCanvas(aContentID, aScroller, aWidth, aHeight, aStyle)
{
    this.ContentID = aContentID;
    this.Width = aWidth;
    this.Height = aHeight;
    this.Style = aStyle;
    this.Scroller = aScroller;
    this.Contenter = $(aContentID);
    if (this.Contenter)
    {
        this.Contenter.style.overflow = 'hidden';
        this.Contenter.innerHTML = "<div id='" + aContentID + "_div1' style='display:inline;overflow:hidden;width:" + this.Width + ";" + aStyle + "' ></div>" +
                              "<div id='" + aContentID + "_div2' style='display:inline;overflow:hidden;width:" + this.Width + ";" + aStyle + "' ></div>";
        if (this.Scroller != null)
        {
            this.Contenter.onmouseover = _ScrollCallHelper(this, 2);
            this.Contenter.onmouseout = _ScrollCallHelper(this, 3)
        }
    }

    this.Div1 = $(aContentID + "_div1");
    this.Div2 = $(aContentID + "_div2");
}
ScrollCanvas.prototype.AddContent = function(aContentHTML)
{
    if (this.Div1 && this.Div1 != null)
        this.Div1.innerHTML = aContentHTML;
    if (this.Div2 && this.Div2 != null && this.Scroller && this.Scroller.RollAble)
        this.Div2.innerHTML = aContentHTML;
}

