/*
*	连续滚动代码 V1.0.0(Build 2)
*	created:	2007-7-15
*	modified:	2007-7-18
*	laoqiming@gmail.com
*/
if(typeof MarqueeManager == "undefined")MarqueeManager={};
if(typeof MarqueeManager.Marquees == 'undefined')MarqueeManager.Marquees=[];

function Marquee(id,contentId,interval,step,direction){
	this.id=id;
	this.contentId=contentId;
	MarqueeManager.Marquees[this.id]=this;
	this.container;
	this.mask;
	this.enabled = true;
	this._self = document.getElementById(this.id);
	if(!this._self){this.enabled=false;alert("无效的id:" + this.id);}
	this.width=(!this._self)?0:this._self.offsetWidth;
	this.height=(!this._self)?0:this._self.offsetHeight;
	if(this.width==0 || this.height==0){this.enabled=false;}
	this._content = document.getElementById(this.contentId);
	this._contentWidth = 0;
	this._contentHeight = 0;
	if(!this._content){
		this.enabled=false;
		alert("无效的contentId:" + this.contentId);
	}else{
		for(var i=0;i<this._content.childNodes.length;i++){
			this._contentWidth += isNaN(this._content.childNodes[i].offsetWidth)?0:this._content.childNodes[i].offsetWidth;
			this._contentWidth += isNaN(this._content.childNodes[i].marginLeft)?0:this._content.childNodes[i].marginLeft;
			this._contentWidth += isNaN(this._content.childNodes[i].marginRight)?0:this._content.childNodes[i].marginRight;
			this._contentHeight += isNaN(this._content.childNodes[i].offsetHeight)?0:this._content.childNodes[i].offsetHeight;
			this._contentHeight += isNaN(this._content.childNodes[i].marginTop)?0:this._content.childNodes[i].marginTop;
			this._contentHeight += isNaN(this._content.childNodes[i].marginBottom)?0:this._content.childNodes[i].marginBottom;
		}
	}
	if(this._contentWidth==0 || this._contentHeight==0){this.enabled=false;}
	this.interval=!interval?200:interval;
	this.step=!step?5:step;
	this.direction=!direction?"left":direction;
	this.timer;
	this.play = function(){
		if(this.enabled)
			eval('MarqueeManager.Marquees["' + this.id + '"].timer = setInterval(\'MarqueeManager.Marquees["' + this.id + '"].move()\',MarqueeManager.Marquees["' + this.id + '"].interval)');
	}
	this.stop=function(){
		clearInterval(this.timer);
	}
	this.move=function(){
		switch(this.direction){
			case "up":
				this._self.scrollTop += this.step;
				if(this._self.scrollTop >= this._contentHeight){
					this._self.scrollTop=0;
				}
				break;
			case "left":
				this._self.scrollLeft += this.step;
				if(this._self.scrollLeft >= this._contentWidth){
					this._self.scrollLeft=0;
				}
				break;
			case "right":
				this._self.scrollLeft -= this.step;
				if(this._self.scrollLeft <= 0){
					this._self.scrollLeft = this._contentWidth;
				}
				break;
			case "down":
				this._self.scrollTop -= this.step;
				if(this._self.scrollTop <= 0){
					this._self.scrollTop = this._contentHeight;
				}
				break;
		}
	}
	this.init=function(){
		if(this.enabled){
			//使用this._self做为遮罩,在其限定范围内显示内容
			this._self.style.overflow="hidden";
			var selfObject=this;
			this._self.onmouseover=function(){
				selfObject.stop();
			}
			this._self.onmouseout=function(){
				selfObject.play();
			}

			//创建一个容器,把要滚动的内容放该容器里
			this.container=document.createElement("div");
			this.container.id="MarqueeContainer_" + this.id;

			switch(this.direction){
				case "up":
				case "down":
					//计算要复制的内容个数
					var count=0;
					count=Math.ceil(this.height/this._contentHeight)+1;
					count = count>1?count:count+1;
					//设置容器样式
					with(this.container){
						style.width=this.width + "px";
						style.height=Math.ceil(this._contentHeight * count) + "px";
					}
					//把复制的内容添加到容器中
					for(var i=0;i<count;i++){
						//var o=this._content.cloneNode(true);
						var o=document.createElement("div");
						o.id=this.contentId + "_Copy_" + i;
						o.style.clear="both";
						o.style.float="none";
						o.style.marginTop="0px";
						o.style.marginBottom="0px";
						o.style.marginLeft="0px";
						o.style.marginRight="0px";
						o.innerHTML=this._content.innerHTML;
						this.container.appendChild(o);
					}
					break;
				case "left":
				case "right":
					//计算要复制的内容个数
					var count=0;
					count=Math.ceil(this.width/this._contentWidth)+1;
					count = count>1?count:count+1;
					//设置容器样式
					with(this.container){
						style.width=Math.ceil(this._contentWidth * count) + "px";
						style.height=this.height + "px";
					}
					//把复制的内容添加到容器中
					for(var i=0;i<count;i++){
						var o=this._content.cloneNode(true);
						//var o=document.createElement("div");
						o.id=this.contentId + "_Copy_" + i;
						o.style.float="left";
						o.style.clear="none";
						o.style.marginTop="0px";
						o.style.marginBottom="0px";
						o.style.marginLeft="0px";
						o.style.marginRight="0px";
						//o.innerHTML=this._content.innerHTML;
						this.container.appendChild(o);
					}
					break;
			}
		//把容器插入DOM中
		this._self.appendChild(this.container)

		//隐藏原内容
		this._content.style.display="none";
		}
	}
	this.init();
}
