// ----------------------- 循环显示图片的类 -------------------
/* 调用方法:
   首先包含这个文件: <script type="text/javascript" language="javascript" src="javascript/scroll.js"></script>
   <script type="text/javascript" language="javascript">
   var 变量名称 = new Scroll(containerId, Direction, Step, Interval, mouseOverStop);
   变量名称.Start(方向参数)  0: 向上或者向左滚动; 1: 向下或者向右滚动
   </script>
*/

/*
 连续滚动的类
 containerId -> 包含滚动内容的元素 ID
 Direction -> 滚动方式, 0: 横向(左右)滚动; 1: 纵向(上下)滚动. 默认为从下往上滚动
 Step -> 每次滚动的距离(像素),默认为 1
 Interval -> 滚动的时间间隔(毫秒),默认为 100
 mouseOverStop -> 鼠标经过滚动区域时是否停止滚动, 默认为 true
 */
function Scroll(containerId, direction, step, interval, mouseOverStop)
{
	var args = arguments.length;
	if (args < 1)
	{
		alert("必须指定一个包含滚动内容的容器 ID");
		return;
	}
	if (args < 2) var Direction = 1;
	if (args < 3) var Step = 1;
	if (args < 4) var Interval = 100;
	if (args < 5) var mouseOverStop = true;

	this.ContainerId = containerId;
	this.Direction = direction;
	this.Step = Math.max(1, step);
	this.Interval = interval;
	this.MouseOverStop = mouseOverStop;
	this.MouseOverStopFlag = false;
	this.dirType = 0;		// 默认向上或向左滚动
	this.container = document.createElement("div");	// 包含滚动内容的容器
	this.scrollTimer;		// 和滚动相关的定时器
};

Scroll.prototype.AddEvent = function(obj, evt, func)
{
	if (obj.attachEvent)
		obj.attachEvent("on" + evt, func);
	else
		obj.addEventListener(evt, func, false);
};


/*
 开始滚动
 dir -> 滚动方向, 0: 向上或者向左滚动; 1: 向下或者向右滚动
 */
Scroll.prototype.Start = function(dir)
{
	with(this)
	{
		dirType = dir;
		var e = document.getElementById(ContainerId);
		var html = e.innerHTML;
		e.innerHTML = "";
		e.appendChild(container);

		// 横向滚动
		if (Direction == 0)
		{
			var width = e.style.width == "" ? e.width : e.style.width;
			if (!width)
			{
				alert("容器必须必须指定一个宽度");
				return false;
			}
			container.style.width = width;
			container.style.overflowX = "hidden";
			html = "<td style='white-space: nowrap;'>" + html + "</td>";
			var htmlArr = new Array(Math.floor(parseInt(width) / 5));
			for (var i = 0; i < htmlArr.length; i++)
				htmlArr[i] = html;
			container.innerHTML = "<table style='border-width: 0;' cellspacing='0' cellpadding='0'><tr>" + htmlArr.join("") + "</tr></table>";
		}
		// 纵向滚动
		else if (Direction == 1)
		{
			var height = e.style.height == "" ? e.height : e.style.height;
			if (!height)
			{
				alert("容器必须指定一个高度");
				return false;
			}
			container.style.height = height;
			container.style.overflowY = "hidden";
			html = "<tr><td>" + html + "</td></tr>";
			var htmlArr = new Array(Math.floor(parseInt(height) / 5));
			for (var i = 0; i < htmlArr.length; i++)
				htmlArr[i] = html;
			container.innerHTML = "<table style='border-width: 0;' cellspacing='0' cellpadding='0'>" + htmlArr.join("") + "</table>";
		}

		if (MouseOverStop)
		{
			var _self = this;
			AddEvent(container, "mouseover", function() {_self.MouseOverStopFlag = true;});
			AddEvent(container, "mouseout", function() {if (_self.MouseOverStopFlag) {_self.MouseOverStopFlag = false; _self._Start(true);}});
		}

		_Start();
	}
}


Scroll.prototype._Start = function(noMoveStep)
{
	clearTimeout(this.scrollTimer);

	with (this)
	{
		if (MouseOverStopFlag) return;

		if (noMoveStep)
		{
			var _self = this;
			this.scrollTimer = setTimeout(function(){_self._Start();}, Interval);
			return;
		}

		// 横向滚动
		if (Direction == 0)
		{
			// 向左滚动
			if (dirType == 0 || dirType == "left")
			{
				if (container.scrollLeft >= container.scrollWidth / 2)
					container.scrollLeft = 0;
				container.scrollLeft += Step;
			}
			// 向右滚动
			else if (dirType == 1 || dirType == "right")
			{
				if (container.scrollLeft <= 0)
					container.scrollLeft = container.scrollWidth / 2;
				container.scrollLeft -= Step;
			}
		}
		// 纵向滚动
		else if (Direction == 1)
		{
			// 向上滚动
			if (dirType == 0 || dirType == "up")
			{
				if (container.scrollTop >= container.scrollHeight / 2)
					container.scrollTop = 0;
				container.scrollTop += Step;
			}
			// 向下滚动
			else if (dirType == 1 || dirType == "down")
			{
				if (container.scrollTop <= 0)
					container.scrollTop = container.scrollHeight / 2;
				container.scrollTop -= Step;
			}
		}
		var _self = this;
		this.scrollTimer = setTimeout(function(){_self._Start();}, Interval);
	}
}