﻿

function textScroll(boxID,direction,moveStep,runTimeStep)
{
	this.MoveStep=moveStep?moveStep:2; //每次移动间隔
	this.RunTimeStep=runTimeStep?runTimeStep:3000 //运行间隔（毫秒）
	
    this.Init(boxID,direction);
	this.SetAutoRun(true);
}

textScroll.prototype.Init=function(boxID,direction)
{
	var me=this;
    var _box=document.getElementById(boxID);
    if(!_box)
    {
        return;    
    }
	
	this.currentIndex=0; //当前显示的图片在imageTable的位置
	this.startIndex=0; //imageTable的开始位置
	this.isRunning=false; //是否正在运程
	this.interval=null;//记录自动运行    
    this.Direction=direction?direction:1; //移动方向（1为向上,2为向右,3向下,4向左）
    
	//xy坐标的递增方向
	if(this.Direction<=2)
		this.Dt=-1;
	else
		this.Dt=1;	
	
    var _table=_box.getElementsByTagName("table");
    if(_table.length>0)
        this.ScrollTable=_table[0];
    else
        return;    
	
    //获取列表项
	this.Items=new Array();
	var _item;
	if(this.Direction==1 || this.Direction==3)
	{
    	_item=this.ScrollTable.rows;
	}
	else if(this.Direction==2 || this.Direction==4)
	{
    	_item=this.ScrollTable.rows[0].childNodes;		
	}
	else
		return;
		
	for(i=0;i<_item.length;i++)
	{
		this.Items[i]=_item[i];
	}
	this.ItemCount=this.Items.length;
	//复制列表项
    for(i=0;i<this.ItemCount;i++)
    {
		var _copy=this.Items[i].cloneNode(true);
		if(this.Direction==1 || this.Direction==3 )
			this.ScrollTable.childNodes[0].appendChild(_copy);
		else if(this.Direction==2 || this.Direction==4 )
			this.ScrollTable.rows[0].appendChild(_copy);
			
    }   
	
	//设置移动时操作样式的对象
	if(this.Direction==1 || this.Direction==3 )
		this.CssControl="top";
	else if(this.Direction==2 || this.Direction==4 )
		this.CssControl="left";		
		
	//设定鼠标移入移出动作
	this.ScrollTable.onmouseover=function(){me.isFocus=true};
	this.ScrollTable.onmouseout=function(){me.isFocus=false};
}

textScroll.prototype.MoveGo=function()
{	
	//只有一项的，不滚动
	if(this.Items.length<=1)return;
	if(this.isRunning || this.isFocus)return;
	
	var moveStart=0;
	var moveEnd=0;
	
	//记录移动距离
	if(this.Direction==1 || this.Direction==3 )
		this.MoveDistance=this.Items[this.currentIndex].clientHeight; 
	else if(this.Direction==2 || this.Direction==4 )
		this.MoveDistance=this.Items[this.currentIndex].clientWidth; 

	//计算开始和结束位置
	var countIndex=this.Items.length+this.currentIndex;
	if(this.currentIndex==this.Items.length-1)countIndex=this.Items.length-1;
	
	this.MoveStart=-countIndex*this.MoveDistance;
	this.MoveEnd=this.MoveStart + this.Dt*this.MoveDistance;
	
	this.ScrollTable.style.setAttribute(this.CssControl,this.MoveStart+"px");
	this.isRunning=true;
	
	//移动
	this.MoveSucc=false;	
	var me=this;
	
	var Move=function()
	{
		
		var moveStep=me.MoveStep;
		
		var pos=parseInt(me.ScrollTable.style.getAttribute(me.CssControl));		
		
		pos+=me.Dt*moveStep;	
		
		if(Math.abs(pos-me.MoveEnd)<=moveStep) pos=me.MoveEnd; //最后一次移动
		me.ScrollTable.style.setAttribute(me.CssControl, pos + "px");		
		
		if(pos==me.MoveEnd)
		{
			me.MoveSucc=true;
			me.MoveStop();		
		}
		else
			window.setTimeout(Move,10);	
	}	
	
	Move();	
}

textScroll.prototype.MoveStop=function()
{
	if(!this.MoveSucc)return;
	this.isRunning=false;	
	
	//移动完后调整当前索引
	if(this.Direction<=2 )
	{
		this.currentIndex++;
		if(this.currentIndex>=this.Items.length)this.currentIndex=0;
	}
	else{
		this.currentIndex--;
		if(this.currentIndex<0)this.currentIndex=this.Items.length-1;
	}
	
}

//设置自动运行
textScroll.prototype.SetAutoRun=function(effect)
{
	var me=this;
	
	var AutoRun=function()
	{
		me.MoveGo();
	}

	//设置自动运程	
	if(effect && this.interval==null)
	{
		this.interval=window.setInterval(AutoRun,this.RunTimeStep);
	}
	else if(!effect)
	{		
		this.interval=null;
	}
}


