function whirBannerAD(id){	
    var self=this;
	this.ID=id;
	this.type="Banner";
	this.width=100;
	this.height=100;
	this.box=null;
	this.content="";
	this.isLoaded=false;
	var Box=document.createElement("DIV");
	
	function build(){		
		if(self.isLoaded)return;
		Box.style.width=self.width+"px";
		Box.style.height=self.height+"px";
		Box.style.overflow="hidden";
		Box.innerHTML=self.content;
		if(self.box==null){			
			var Box1=document.createElement("DIV");
			Box1.appendChild(Box);
			document.writeln(Box1.innerHTML);
		}else{
			self.box.appendChild(Box);
		}
		self.isLoaded=true;		
	}
	this.load=function(){
		build();
	}
}

function whirPopupAD(id){	//弹出窗口
	var self=this;
	this.ID=id;
	this.type="PopUp";
	this.width=100;
	this.height=100;
	this.left=0;
	this.top=0;
	this.title="";
	this.content="";	
	this.isLoaded=false;
	
	function build(){		
		if(self.isLoaded)return;
		var content=self.content;
		content=content.replace("\r","").replace("\n","");
		var html="<html><head><title>"+self.title+"</title><style>body{margin:0px;}</style></head><body>"+content+"</body></html>";
		var pupwin=window.open("","_blank","width="+self.width+",height="+self.height+",top="+self.top+",left="+self.left);	
		pupwin.document.write(html);
		self.isLoaded=true;
	}
	this.load=function(){
		build();
	}
}

function whirFloatAD(id){
	//public	
	this.ID=id;
	this.type="Float";
	this.width=100;
	this.height=100;
	//this.top=0;
	//this.left=0;
	this.content="";
	this.closeButton=true;
	this.closeButtonHtml="关闭";
	this.timeStep=100;
	this.isLoaded=false;
	
	//private
	var self=this;
	var timer=null;	
	var contentDiv=document.createElement("DIV");	
	var rootDiv=document.createElement("DIV");
	var xx=yy=vx=vy=0
	var vmin=2;
	var vmax=5;
	var vx=vmin+vmax*Math.random();
	var vy=vmin+vmax*Math.random();
	var vr=2;
	var isHide=false;
	
	function build(){
		if(self.isLoaded)return;
		if(self.closeButton==true){
			var closeButton=document.createElement("SPAN");
			//closeButton.href="javascript:;";
			closeButton.innerHTML=self.closeButtonHtml;
			closeButton.style.cursor="hand";
			closeButton.onclick=function(){
			   self.hide();
			}
		
			var closeButtonDiv=document.createElement("DIV");
			closeButtonDiv.setAttribute("align","right");
			closeButtonDiv.style.position="absolute";
			closeButtonDiv.style.top="0px";
			closeButtonDiv.style.right="0px";
			closeButtonDiv.style.margin="2px";
			closeButtonDiv.style.padding="2px";
			closeButtonDiv.style.zIndex="999";
			closeButtonDiv.style.fontSize="12px";
			
			closeButtonDiv.appendChild(closeButton);
			rootDiv.appendChild(closeButtonDiv);			
		}		
		
	    contentDiv.innerHTML = self.content;
		rootDiv.style.position="absolute";
		rootDiv.style.zIndex="900";
		rootDiv.style.display="none";	
		rootDiv.style.wordBreak="break-all";	
	    rootDiv.style.width = self.width+"px";
	    rootDiv.style.height = self.height+"px";
		//rootDiv.style.border="1px red solid";
		rootDiv.style.overflow="hidden";
		
		//rootDiv.style.left = self.left;
		//rootDiv.style.top  = self.top;
		rootDiv.style.filter= "Alpha(style=0,opacity=100,finishOpacity=100)";
		//rootDiv.style.border = "1px solid #ff0000";
		rootDiv.onmouseover=function(){
			self.stop();
		}
		rootDiv.onmouseout=function(){
			self.start();
		}
		rootDiv.appendChild(contentDiv);
		document.body.appendChild(rootDiv);	
		self.isLoaded=true;
	}	
    
	function moveDiv(){
		var pageX=pageW=pageY=pageH=0;
		var isIE=(document.all)?true:false;
		if(!isIE){
			pageX=window.pageXOffset;
			pageW=window.innerWidth;
			pageY=window.pageYOffset;
			pageH=window.innerHeight;
		}else{
			pageX=window.document.body.scrollLeft;
			pageW=window.document.body.offsetWidth-8;
			pageY=window.document.body.scrollTop;
			pageH=window.document.body.offsetHeight;
		} 			
		
		xx=xx+vx;
		yy=yy+vy;
		vx+=vr*(Math.random()-0.5);
		vy+=vr*(Math.random()-0.5);
		if(vx>(vmax+vmin)) vx=(vmax+vmin)*2-vx;
		if(vx<(-vmax-vmin)) vx=(-vmax-vmin)*2-vx;
		if(vy>(vmax+vmin)) vy=(vmax+vmin)*2-vy;
		if(vy<(-vmax-vmin)) vy=(-vmax-vmin)*2-vy;
		if(xx<=pageX){
			xx=pageX;
			vx=vmin+vmax*Math.random();
		}
		if(xx>=pageX+pageW-self.width){
			xx=pageX+pageW-self.width;
			vx=-vmin-vmax*Math.random();
		}
		if(yy<=pageY){
			yy=pageY;
			vy=vmin+vmax*Math.random();
		}
		if(yy>=pageY+pageH-self.height){
			yy=pageY+pageH-self.height;
			vy=-vmin-vmax*Math.random();
		}
		rootDiv.style.display="none";
		if(!isIE){			
			rootDiv.left = xx;
			rootDiv.top = yy;
		}else{			
			rootDiv.style.left = xx;
			rootDiv.style.top = yy;
		}
		rootDiv.style.display="inline";	
		self.start();
	}
	
	this.hide=function(){
	  rootDiv.style.display="none";
	  isHide=true;
	}
	this.stop=function(){
		window.clearTimeout(timer);
		timer=null;
	}
	this.start=function(){
		if(isHide==false){
			timer=window.setTimeout(moveDiv,self.timeStep);
		}
		//moveDiv();
	}
	this.load=function(){
		build();
		self.start();		
	}	
}

function whirScrollAD(id){	
	this.ID=id;
	this.type="Scroll";
	this.scroll=true;
	this.closeAll=true;
	this.closeButton=true;
	this.closeButtonHtml="关闭";
	this.isLoaded=false;
	
	var self=this;
	var items=[];
	var timer=null;	
	var closedSize=0;
	var allClosed=false;
	var size=0;
	var lastScrollY=0;
	
	function build(){
		if(self.isLoaded)return;
		if(size<1)return false;
		for(var i=0;i<size;i++){
			var newDiv=document.createElement("DIV");
			newDiv.style.position="absolute";
			newDiv.style.width=items[i].width;
			newDiv.style.height=items[i].height;
			newDiv.style.zIndex=(990-i)+"";
			newDiv.style.overflow="hidden";
			newDiv.style.backgroundColor="#fafafa";
			if(items[i].left!=null){
				newDiv.style.left=items[i].left;
			}else{
				if(items[i].right!=null)newDiv.style.right=items[i].right;
			}			
			newDiv.style.top=items[i].top;
			if(typeof(items[i].content)=="object"){
				newDiv.appendChild(items[i].content);
			}else{
				newDiv.innerHTML=items[i].content;
			}
			if(items[i].canClose==true){
				var closeButton=document.createElement("SPAN");
				closeButton.innerHTML=self.closeButtonHtml;
				closeButton.style.cursor="hand";
				closeButton.index=i;
				closeButton.onclick=function(){
				   hide(this.index);
				}
			
				var closeButtonDiv=document.createElement("DIV");
				closeButtonDiv.setAttribute("align","right");
				closeButtonDiv.style.position="absolute";
				closeButtonDiv.style.top="0px";
				closeButtonDiv.style.right="0px";
				closeButtonDiv.style.margin="2px";
				closeButtonDiv.style.padding="2px";
				closeButtonDiv.style.zIndex="999";
				closeButtonDiv.style.fontSize="12px";
				
				closeButtonDiv.appendChild(closeButton);
				newDiv.appendChild(closeButtonDiv);			
			}
			items[i].el=newDiv;
			items[i].isHide=false;
			document.body.appendChild(items[i].el);
		}
		self.isLoaded=true;
	}
	this.addItem=function(data){
		var newItem={};
		newItem.left	=(data!=null && data.left!=null)?data.left:null;
		newItem.top		=(data!=null && data.top!=null)?data.top:"100px";
		newItem.right	=(data!=null && data.right!=null)?data.right:null;
		newItem.width	=(data!=null && data.width!=null)?data.width:"100px";
		newItem.height	=(data!=null && data.height!=null)?data.height:"100px";
		newItem.content	=(data!=null && data.content!=null)?data.content:"";
		newItem.canClose=(data!=null && data.canClose!=null)?data.canClose:true;
		newItem.isHide	=false;
		newItem.el      =null;
		
		items[size++]=newItem;
		return newItem;		
	}
	function hide(index){
		if(self.closeAll==true || index==-1){
			for(var i=0;i<size;i++){
				items[i].el.style.display="none";
				items[i].isHide=true;
			}
			allClosed=true;
		}else{
			items[index].el.style.display="none";
			items[index].isHide=true;
			closedSize++;
			if(size==closedSize)allClosed=true;
		}		
	}
	function start(){
		if(size<1 || allClosed==true || self.scroll==false){
			timer=null;
			return;
		}
		if(screen.width<=800){
			hide(-1);
			timer=null;
			return;
		}
		for(var i=0;i<size;i++){
			var followObj	= items[i].el;
			var ytype="top";
			var y=0; 
			var diffY=0;
			var percent=0;
			y=followObj.style.top;
			y=parseInt(y);
			if (document.documentElement && document.documentElement.scrollTop){
				diffY = document.documentElement.scrollTop;
			}else if (document.body){
				diffY = document.body.scrollTop
			}
			percent=0.1*(diffY-lastScrollY); 
			if(percent>0){
				percent=Math.ceil(percent); 
			}else{
				percent=Math.floor(percent); 
			}
			followObj.style.top=(y+percent)+"px";
/*			if(followObj.offsetTop!=(diffY+y)) {
				var dy=(diffY+y-followObj.offsetTop)*delta;
				dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
				followObj.style.top=followObj.offsetTop+dy;
			}*/

		}
		lastScrollY+=percent; 		
	}
	this.load=function(){
		build();
		timer=window.setInterval(start,100);
		//document.body.attachEvent("onscroll", start);
	}
}

function whirRandPlayAD(id){	
	this.ID=id;
	this.type="RandShow";
	this.scroll=false;
	this.isLoaded=false;
}

function whirOrderPlayAD(id){	
	this.ID=id;
	this.type="OrderChange";
	this.scroll=false;
	this.isLoaded=false;
	
}

function whirMarqueeAD(id){	
    var self=this;
	this.ID=id;
	this.type="Banner";
	this.scroll=false;
	this.width=0;
	this.height=0;
	this.box=null;
	this.isLoaded=false;
	var HTML="";
	var Box=document.createElement("DIV");
}

function whirCurtainAD(id){
    var self=this;
	this.ID=id;
	this.type="Curtain";
	this.width=0;
	this.height=0;
	this.direction="top";
	this.autoCloseTimer=5;
	this.content=null;
	this.speed=1;
	this.closeButton=true;
	this.closeButtonHtml="关闭";
	this.parentElement=null;
	this.position="absolute";
	this.isLoaded=false;
	this.marginLeft="0px";
	
	var HTML="";
	var Box=null;	
	var timer=null;
	var autoTimer=null;
	var delay=5;
	var isHide=true;
	var isShow=false;
	
	function build(){
		if(self.isLoaded)return;
		var contentDiv=null;
		if(self.content!=null && typeof(self.content)=="object"){
			Box=self.content;
			if(self.width==0)self.width=Box.offsetWidth;
			if(self.height==0)self.height=Box.offsetHeight;				
		}else{
			Box=document.createElement("DIV");
			contentDiv=document.createElement("DIV");
			contentDiv.innerHTML = self.content;
			Box.appendChild(contentDiv);
			if(self.width==0)self.width=100;
			if(self.height==0)self.height=100;
		}
		
		if(self.direction=="top" || self.direction=="bottom"){
			Box.style.height="0px";
			Box.style.width=self.width+"px";
		}else{
			Box.style.width="0px";	
			Box.style.height=self.height+"px";		
		}
		
		Box.style.position=self.position;		
		Box.style.display="none";
		Box.style.zIndex="900";	
		Box.style.wordBreak="break-all";	
		Box.style.overflow="hidden";
		Box.style.backgroundColor="#fafafa";
		//Box.style.border="1px red solid";
        Box.style.marginLeft=self.marginLeft;
		 
		
		if(self.closeButton==true){
			var closeButton=document.createElement("SPAN");
			closeButton.innerHTML=self.closeButtonHtml;
			closeButton.style.cursor="hand";
			closeButton.onclick=function(){
			   self.close();
			}
		
			var closeButtonDiv=document.createElement("DIV");
			closeButtonDiv.setAttribute("align","right");
			closeButtonDiv.style.position="absolute";
			closeButtonDiv.style.top="0px";
			closeButtonDiv.style.right="0px";
			closeButtonDiv.style.margin="2px";
			closeButtonDiv.style.padding="2px";
			closeButtonDiv.style.zIndex="999";
			closeButtonDiv.style.fontSize="12px";
			
			closeButtonDiv.appendChild(closeButton);
			Box.appendChild(closeButtonDiv);
		}		
		if(self.content!=null && typeof(self.content)=="object"){
			if(self.parentElement!=null){
				self.parentElement.appendChild(Box);
			}else{
				self.parentElement=self.content.parentElement;
			}
		}else{
			if(self.parentElement==null)self.parentElement=document.body;
			self.parentElement.appendChild(Box);			
		}
		self.isLoaded=true;
	}
	
	function show(){
		isHide=false;
		Box.style.display="";		
		if(isShow==true){	
			if(timer!=null) window.clearInterval(timer);								
			timer=null;
			return ;
		}
		switch(self.direction){
			case "top":
			case "bottom":			
							var h=Box.style.height;
							h=parseInt(h);
							h+=delay;
							if(h>=self.height){
								isShow=true;
								h=self.height;
							}							
							Box.style.height=h+"px";
							if(self.direction=="top"){
								Box.style.top="0px";
							}else{
								//var bh=self.parentElement.offsetHeight;
								//Box.style.top=(bh-h)+"px";
								Box.style.bottom="0px";
							}
							break;
			case "left":
			case "right":
							var w=Box.style.width;
							w=parseInt(w);
							w+=delay;
							if(self.direction=="left"){
								Box.style.left="0px";
							}else{
								Box.style.right="0px";
							}
							
							if(w>=self.width){
								isShow=true;
								w=self.width;
							}							
							Box.style.width=w+"px";	
							break;
		}
	}
	function hide(){
		isShow=false;
		if(isHide==true){	
			if(timer!=null) window.clearInterval(timer);							
			timer=null;
			return ;
		}
		switch(self.direction){
			case "top":
			case "bottom":			
							var h=Box.style.height;
							h=parseInt(h);
							h-=delay;
							if(h<=0){
								h=0;
								Box.style.display="none";
								isHide=true;
							}							
							Box.style.height=h+"px";
							if(self.direction=="top"){
								Box.style.top="0px";
							}else{
								//var t=Box.style.top;
								//t=parseInt(t)+delay;
								//Box.style.top=t+"px";
								Box.style.bottom="0px";
							}
							break;
			case "left":
			case "right":
							var w=Box.style.width;
							w=parseInt(w);
							w-=delay;
							if(w<=0){
								w=0;
								Box.style.display="none";
								isHide=true;
							}							
							Box.style.width=w+"px";
							break;
		}		
	}
	this.close=function(){
		if(timer!=null) window.clearInterval(timer);
		if(autoTimer!=null) window.clearTimeout(autoTimer);
		timer=window.setInterval(hide,self.speed*10);
		//hide();
	}
	this.load=function(){
		build();
		timer=window.setInterval(show,self.speed*10);
		//show();
		if(self.autoCloseTimer>0)autoTimer=window.setTimeout(self.close,self.autoCloseTimer*1000);
	}
	
}

whirAD={
	ads:new Array(),
	adCount:0,
	
	getAdCount:function(){
		return whirAD.adCount;
	},
	
    NewAdvert:function(id,adType){
		var ad=null;
		switch(adType){
			case "Popup":	//弹出窗口
							ad=whirAD.ads[whirAD.adCount++]=new whirPopupAD(id);
							break;
			case "Float":	//浮动
							ad=whirAD.ads[whirAD.adCount++]=new whirFloatAD(id);
					 		break;
			case "Scroll":	//门帘
							ad=whirAD.ads[whirAD.adCount++]=new whirScrollAD(id);
					 		break;
			case "RandPlay"://随机显示
							ad=whirAD.ads[whirAD.adCount++]=new whirRandPlayAD(id);
					 		break;
			case "OrderPlay"://顺序切换	
							ad=whirAD.ads[whirAD.adCount++]=new whirOrderPlayAD(id);
					 		break;
			case "Banner":	//固定位置
							ad=whirAD.ads[whirAD.adCount++]=new whirBannerAD(id);
					 		break;
			case "Marquee":	//滚动
							ad=whirAD.ads[whirAD.adCount++]=new whirMarqueeAD(id);
					 		break;		
			case "Curtain"://拉幕式
							ad=whirAD.ads[whirAD.adCount++]=new whirCurtainAD(id);
							break;
		}		
		return ad;
	},
	
	New:function(id,adType){
		return whirAD.NewAdvert(id,adType);
	},
	load:function(){
		for(var i=0;i<whirAD.adCount;i++){
			if(whirAD.ads[i].isloaded!=true)whirAD.ads[i].load();
		}
	}
}

//whirAD=new whirAdvert();

window.attachEvent("onload",whirAD.load);


