/*
 * =========================================================================
 * 本程序可自由复制、修改、传播，不得删除以下信息。如用于商业用途须经原作者同意方可使用。
 * =========================================================================
 * ============================= 文件摘要 ==========================================		
	* 文件名称：Marquee.js
	* 文件功能：图片滚动类
	* 文件版本：2.0
	* 版本支持：Mozilla,IE
	* 文件作者：风无痕
	* 创建时间：2007年07月03日
	* 修改时间：2008年01月05日
	* 邮箱地址：mfkwgcom@163.com
	* 版权声明：本程序属于mfkwgcom，版权归作者所有。
 * ============================= 使用说明 ==========================================
	* new Marquee(container,ChildNodeID,Width,Height,direction,speed,timer)
	* container   = 对象ID
	* ChildNodeID = 子对象ID
	* Width       = 滚动宽度(px)
	* Height      = 滚动高度(px)
	* direction   = 上(top)|下(down)|左(left)|右(right)
	* speed       = 速度(px)
	* timer       = 时间(ms)
 * ============================= 举例说明 ==========================================
	<SCRIPT language="javascript" type="text/javascript">
		new Marquee('demo','demo1',730,200,'left',3,100);
		new Marquee('demo','demo1',730,200,'left',3);
		new Marquee('demo','demo1',730,200,'left');
		new Marquee('demo','demo1',730,200);
	</SCRIPT>
 * =================================================================================
 */
function Marquee(container,ChildNodeID,Width,Height,direction,speed,timer){
	if (typeof(container) == 'string') {
		container = document.getElementById(container);
	}
	if (typeof(ChildNodeID) == 'string') {
		ChildNodeID = document.getElementById(ChildNodeID);
	}
	this.timer=timer?timer:100;
	this.container = container;
	this.container.style.overflow="hidden";
	this.ChildNodeID=ChildNodeID;
	this.ChildNodeID.align=direction=="top"||direction=="down"?"":"left";
	this.direction=direction?direction:"left";
	this.speed = speed?speed:2;
	this._slidable = true;
	this._IntervalOBJ = null;
	if(Width!=0) this.container.style.width=Width;
	if(Height!=0) this.container.style.height=Height;
	this._FistChildrenWidth=this.ChildNodeID.offsetWidth;	//取得滚动对象的宽度
	this._FistChildrenHeight=this.ChildNodeID.offsetHeight;	//取得滚动对象的高度
	this._FistChildrenSize=direction=="top"||direction=="down"?(this._FistChildrenHeight-this.speed):(this._FistChildrenWidth-this.speed);
	this._appendChild();
	this.ChildNodeID=null;
	this.container.onmouseover =function(self){return function(){self._mouseover()};}(this);
	this.container.onmouseout =function(self){return function(){self._mouseout()};}(this);
	this._loop();
}
Marquee.prototype ={
	_appendChild : function(){
		switch(this.direction){
			case "top":
			case "down":
				var OBJ_HEIGHT=this.container.offsetHeight*2;
				var i=1;
				var objSourceRow = this.ChildNodeID.tBodies[0];
				do{
					i++;
					objSourceRow.appendChild(objSourceRow.rows[0].cloneNode(true));
				}while(OBJ_HEIGHT>this._FistChildrenHeight*i&&i<10);
				break;
			case "left":
			case "right":
				var OBJ_WIDTH=this.container.offsetWidth*2;
				var i=1;
				var objSourceRow = this.ChildNodeID.tBodies[0].rows[0];
				do{
					i++;
					objSourceRow.appendChild(objSourceRow.cells[0].cloneNode(true));
				}while(OBJ_WIDTH>this._FistChildrenWidth*i&&i<10);
				
		}
	},
	_loop : function(){
		var sb=this;
		var _loop = function(){
			if (!sb._slidable) return;
			var c = sb.container;
			switch(sb.direction){
				case "top":
					if(sb._FistChildrenHeight-c.scrollTop > 0){
						c.scrollTop+=sb.speed;
					}else{	
						clearInterval(this._IntervalOBJ);
						c.scrollTop-=sb._FistChildrenSize;
					}
					break;
				case "down":
					if(c.scrollTop > sb.speed){
						c.scrollTop-=sb.speed;
					}else{	
						clearInterval(this._IntervalOBJ);
						c.scrollTop+=sb._FistChildrenSize;
					}
					break;
				case "left":
					if(sb._FistChildrenWidth-c.scrollLeft > 0){
						c.scrollLeft+=sb.speed;
					}else{	
						clearInterval(this._IntervalOBJ);
						c.scrollLeft-=sb._FistChildrenSize;
					}
					break;
				case "right":
					if(c.scrollLeft > sb.speed){
						c.scrollLeft-=sb.speed;
					}else{	
						clearInterval(this._IntervalOBJ);
						c.scrollLeft+=sb._FistChildrenSize;
					}
					break;
				default:alert("No"); 
			}
		}
		this._IntervalOBJ = setInterval(_loop,sb.timer);
	},
	_mouseover : function() {
		this._slidable = false;
	},
	_mouseout : function() {
		this._slidable = true;
	}
} 

