/*
============================= 文件摘要 ==========================================		
=       * 文件名称：    ClsMarquee.js                                           =
=       * 文件功能： 	图片滚动类                                              =
=       * 文件版本： 	1.0                                                     =
=       * 版本支持： 	Mozilla,IE                                              =
=       * 文件作者： 	周煜彬                                                  =
=       * 文件日期： 	2007-4-26                                               =
============================= 使用说明 ==========================================
=       * 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.ChildNodeID=ChildNodeID;
	if(this.ChildNodeID!=null){
		this.timer=timer?timer:100;
		this.container = container;
		this.container.style.overflow="hidden";
		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;
				do{
					i++;
					this.container.appendChild(this.ChildNodeID.cloneNode(true));
				}while(OBJ_HEIGHT>this._FistChildrenHeight*i);
				break;
			case "left":
			case "right":
				var OBJ_WIDTH=this.container.offsetWidth*2;
				var i=1;
				do{
					i++;
					this.container.appendChild(this.ChildNodeID.cloneNode(true));
				}while(OBJ_WIDTH>this._FistChildrenWidth*i);
		}
	},
	_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;
	}
} 

