/***************************
** 功能描述：滚动控制
** 文 件 名：Scroll.js
** 创 建 人：Youniao（潘子骅）
** 创建日期：2006.12.16
** -------------------------
** 使用描述：
*****************************/
//总类
function scrollObj()
{
	//容器相关属性
	//===============================================================
	this.boxID = "scrollBox"; //容器ID
	
	this.cttHeadHeight = 0;   //滚动内容的头高度 （纵向滚动时调用）
	this.cttBodyHeight = 20;  //滚动内容循环体高度
	this.cttBottomHeight = 0; //滚动内容的尾高度
	this.cttHeadWidth = 0;    //滚动内容的头宽度 （横向滚动时调用）
	this.cttBodyWidth = 20;   //滚动内容循环体宽度
	this.cttBottomWidth = 0;  //滚动内容的尾宽度
	this.bodyCount = 8;       //每周期包含循环体数量

	this.direction = "up";    //滚动方向 up|down|left|right
	this.cycle = 8000;        //周期 (单位:毫秒) 一个周期包括了切换--滚动--停顿3过程
	this.scrollTime = 8000;   //滚动时间 (单位:毫秒)应该小于cycle的值
	//=================================================================	
	//开关控制
	//=================================================================
	this.start = function()//开始切屏
	{ 
		//事件触发时第一次滚动(可以删除)
		changeScreen(this.boxID,this.cttHeadHeight,this.cttBodyHeight,this.cttBottomHeight,this.cttHeadWidth,this.cttBodyWidth,this.cttBottomWidth,this.bodyCount,this.direction,this.cycle,this.scrollTime)
		timeObj = setInterval("changeScreen('"+this.boxID+"',"+this.cttHeadHeight+","+this.cttBodyHeight+","+this.cttBottomHeight+","+this.cttHeadWidth+","+this.cttBodyWidth+","+this.cttBottomWidth+","+this.bodyCount+",'"+this.direction+"',"+this.cycle+","+this.scrollTime+")",this.cycle);
	}
	
	this.stop = function()//停止切屏
	{ 
		clearInterval(timeObj);
	}	
	//==================================================================
}
//切屏
function changeScreen(boxID,headHeight,bodyHeight,bottomHeight,headWidth,bodyWidth,bottomWidth,bodyCount,direction,cycle,scrollTime)
{
//参数测试
/*	var str = "";
	str += "boxID:" + boxID + "\r\r\n";
	str += "headHeight:" + headHeight + "\r\r\n";
	str += "bodyHeight:" + bodyHeight + "\r\r\n";
	str += "bottomHeight:" + bottomHeight + "\r\r\n";
	str += "headWidth:" + headWidth + "\r\r\n";
	str += "bodyWidth:" + bodyWidth + "\r\r\n";
	str += "bottomWidth:" + bottomWidth + "\r\r\n";
	str += "bodyCount:" + bodyCount + "\r\r\n";
	str += "direction:" + direction + "\r\r\n";
	str += "cycle:" + cycle + "\r\r\n";
	str += "scrollTime:" + scrollTime + "\r\r\n";
	alert(str);*/
var whichEle = document.getElementById(boxID);
var length = 0;
	switch(direction){
		case "up":
			if(whichEle.scrollTop>=headHeight)//判断循环头是否已滚过
			{
				length = bodyCount * bodyHeight;
			}
			else{
				length = bodyCount * bodyHeight + headHeight;
			}
			move(boxID,"up",whichEle.scrollTop,length,scrollTime);
		break;
		case "down":		
		break;
		case "left":
		
		break;
		case "right":
		
		break;
	}
}
//滚动(容器ID，滚动方向，滚动前屏幕外距离，需要滚动的距离，滚动总时间)
var moveTimeObj;
function move(boxID,direction,sLength,length,scrollTime)
{
//参数测试
/*	var str = "";
	str += "boxID:" + boxID + "\r\r\n";
	str += "direction:" + direction + "\r\r\n";
	str += "sLength:" + sLength + "\r\r\n";
	str += "length:" + length + "\r\r\n";
	str += "scrollTime:" + scrollTime + "\r\r\n";*/
	//alert(str);
	var whichEle = document.getElementById(boxID);
	switch(direction)
	{
		case "up":
			if (whichEle.scrollTop < (sLength+length))
			{
				if(whichEle.scrollTop==parseInt(whichEle.scrollHeight/2))
				{
					whichEle.scrollTop = 0;
					return;
				}
				else
				{
					whichEle.scrollTop += 1;
					moveTimeObj=window.setTimeout(function(){move(boxID,direction,sLength,length,scrollTime)},parseInt(scrollTime/length));
				}
			}
			else{
				clearTimeout(moveTimeObj);
				return;
			}
		break;
		case "down":
		break;
		case "left":
		break;
		case "right":
		break;	
	}
		
}
//滚动开关 (参数 start | stop)
function scrollSwitch(args)
{
	var myScroll = new scrollObj();
	if (args=="start")
	{
		myScroll.start();
	}
	else if (args=="stop"){
		myScroll.stop();
	}
	else{
		window.alert("错误的输入参数！");
	}
}