﻿
var $$ = function(id){
    return "string" == typeof id ? document.getElementById(id) : id;
};

function addEvent(oTarget, oEventType, handleFn)
{
    if(oTarget.addEventListener)
        oTarget.addEventListener(oEventType, handleFn, false);
    else if(oTarget.attachEvent)
        oTarget.attachEvent("on" + oEventType, handleFn);
    else
        oTarget["on" + oEventType] = handleFn;
}

Object.extending = function(destination,source){
    for(var property in source)
        destination[property] = source[property];
    return destination;
};

var myClass = {
    create : function(){
        return function() {
            this.initialize.apply(this,arguments);
        };
    }
};

var myScroller = myClass.create();
myScroller.prototype = {
    initialize : function(idPanel,idSubPanel,options){
        var oPanel = $$(idPanel);
        var oSubPanel = $$(idSubPanel);
        var oThis = this;
        
        this.setOptions(options);
        this.scroller = oPanel;
        this.scrollSide = this.options.ScrollSide;
        this.speed = this.options.Speed;
        this.timer = null;
        this.pauseHeight = 0;
        this.pauseWidth = 0;
        this.side = 0;
        this.pause = 0;
        //记数，到设置的停止
        this.pixelCount = 0;

        this.panelHeight = parseInt(oPanel.style.height) || oPanel.offsetHeight;
        this.subPanelHeight = oSubPanel.offsetHeight;
        
        this.panelWidth = parseInt(oPanel.style.width) || oPanel.offsetWidth;
        this.subPanelWidth = oSubPanel.offsetWidth;
        
        oPanel.style.overflow = "hidden";
        
    },
    setOptions : function(opt){
        this.options = {
            Step : 30,
            Speed : 1,
            ScrollSide : "left",
            Length : 1,
            PauseHeight : 0,
            PauseWidth : 0,
            PauseStep : 1000
        };
        Object.extending(this.options, opt || {});
    },
    scrollUpDown : function(){},
    scrollLeftRight : function(){
        
        if(this.pixelCount < this.options.PauseWidth)
        {
            this.pause = this.pauseWidth;
            this.scroller.scrollLeft = this.scrollCalculate(this.scroller.scrollLeft, this.panelWidth, this.subPanelWidth, this.options.PauseWidth);
            this.pauseWidth = this.pause;
        
            var oThis = this;
            this.timer = window.setTimeout(function(){ oThis.ScrollPanel(oThis.options)}, this.speed);
            
            this.pixelCount += this.options.Step; //记数
        }
        else
            this.Stop(); //停止滚动
            
        
    },
    scrollCalculate : function(pScrollLeft, pScrollWidth, pSubWidth, pPauseWidth){
        var pStep = this.options.Step * this.side;
        
        if(this.side > 0)
        {
            if(pScrollLeft >= (pSubWidth * 2 -pScrollWidth))
                pScrollLeft -= pSubWidth;
        }
        else
        {
            if(pScrollLeft <= 0)
                pScrollLeft += pSubWidth;
        }
        
        /*** 按时间停顿后重新滚动
        this.speed = this.options.Speed;
        if(pPauseWidth > 0)
        {
            if(Math.abs(this.pause) >= pPauseWidth)
            {
                this.speed = this.options.PauseStep;
                this.pause = pStep = 0;
            }
            else
                this.pause += pStep;
        }
        */
        
        return pScrollLeft + pStep;
    },
    ScrollPanel : function(opt){
        
        this.setOptions(opt);
        
        switch(this.options.ScrollSide.toLowerCase())
        {
            case "left":
                if(this.subPanelWidth < this.panelWidth ) return;
                this.side = 1;
                this.scrollLeftRight();
                break;
            case "right":
                if(this.subPanelWidth < this.panelWidth ) return;
                this.side = -1;
                this.scrollLeftRight();
                break;
        }
    },
    Stop : function(){
        window.clearTimeout(this.timer);
    }
};
