var AdMoveWithScreen = {	
	ID : null,
	ZoneName : null,
	Width : 750,
	Height : 80,
	ShowType : 0,
	Left : 100,
	Top : 100,
	Speed: .15,
	MoveType: 0,
	AdList : null,
	AdIndex : 0,
	DivName : "",
	DivName2 : "",
	
	Initialize : function(){
		var options = arguments[0];
		if(!options){
			return;
		}
		this.ID = options.ID;
		this.ZoneName = options.ZoneName;
		this.Width = options.Width;
		this.Height = options.Height;
		this.ShowType = options.ShowType;				
		this.MoveType = options.MoveType;
		this.Left = options.Left;
		this.Top = options.Top;
		this.Speed = options.Speed;
		this.AdList = new Array();
		this.DivName = "AD_MoveWithScreen_" + this.ID + "_1";
		this.DivName2 = "AD_MoveWithScreen_" + this.ID + "_2";
		var div = "<div id='"+ this.DivName +"' style='width: "+ this.Width +"; height: "+ this.Height +"; position:absolute; top: "+ this.Top +"px; Left:"+ this.Left +"px; '></div>";
		var div2 = "<div id='"+ this.DivName2 +"' style='width: "+ this.Width +"; height: "+ this.Height +"; position:absolute; top: "+ this.Top +"px; Left:"+ (document.body.clientWidth - this.Left - this.Width) +"px; '></div>";
		document.write(div);
		document.write(div2);
	},
	
	Show : function(){
		if(!this.AdList || this.AdList.length == 0) return;
		this.GetAdIndex();		
		var str = this.GetAdContent(this.AdList[this.AdIndex]);
		var div1 = document.getElementById(this.DivName);
		var div2 = document.getElementById(this.DivName2);
		div1.innerHTML = str;
		div2.innerHTML = str;
		if(this.MoveType == 0){
			div2.style.display = "none";
		}else if(this.MoveType == 1){
			div1.style.display = "none";
		}
		var me = this;
		setInterval(function(){ me.Moving(); }, 10);
	},
	
	Moving : function(){
		var div = null;
		var tempLeft = this.Left;
		for(var i=1;i<=2;i++){
			div = document.getElementById("AD_MoveWithScreen_"+ this.ID + "_"+ i);
			if(div && div.style.display != "none"){
				if(i == 2) tempLeft = document.body.clientWidth - this.Left - this.Width;
				var scroll = this.GetScrollInfo();
				if (div.offsetLeft != (scroll.left + tempLeft)){
					var dx = (scroll.left + tempLeft - div.offsetLeft) * this.Speed;
					dx = (dx > 0 ? 1 :  - 1) * Math.ceil(Math.abs(dx));
					div.style.left = div.offsetLeft + dx + "px";
				}				
				if (div.offsetTop != (scroll.top + this.Top)) {
					var dy = (scroll.top + this.Top - div.offsetTop) * this.Speed;
					dy = (dy > 0 ? 1 :  - 1) * Math.ceil(Math.abs(dy));
					div.style.top = div.offsetTop + dy + "px";
				}
			}
		}		
	},
	
	GetScrollInfo : function(){
		var top, left;
		if (typeof window.pageYOffset != 'undefined'){
			top = window.pageYOffset;
			left = window.pageXOffset;
		}else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
			top = document.documentElement.scrollTop;
			left = document.documentElement.scrollLeft;
		}else if (typeof document.body != 'undefined') {
			top = document.body.scrollTop;
			left = document.body.scrollLeft;
		}
		return { top: top, left: left};
	},
	
	GetAdContent : function(){
		var ad = arguments[0];
		if(!ad) return;
		var strContent = "";
		if(ad.AdType == 0){
			if(ad.LinkUrl){
				strContent += "<a href='http://wpa.qq.com/msgrd?v=3&uin="+ ad.LinkUrl +"&site=qq&menu=yes' target='"+ ad.OpenType +"'>";
			}
			strContent += "<img src='"+ ad.FileUrl +"' border='0' width='"+ this.Width +"' height='"+ this.Height +"' />";
			if(ad.LinkUrl){
				strContent += "</a>";
			}
		}else{
			strContent += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'";
			strContent += " width='"+ this.Width +"' height='"+ this.Height +"'>";
			strContent += "<param name='movie' value='" + ad.FileUrl + "' />";
			strContent += "<param name='wmode' value='Transparent' />";
			strContent += "<param name='quality' value='high' />";
			strContent += "<embed src='"+ ad.FileUrl +"' type='application/x-shockwave-flash' />";
			strContent += "</object>";
		}
		return strContent;
	},
	
	GetAdIndex : function(){
		switch(this.ShowType){
			case 0: 
			    var Rnd = new Array();
				for (var i = 0; i < this.AdList.length; i++) {
					for(var j = 0; j < this.AdList[i].AdRight; j++){
						Rnd[Rnd.length] = this.AdList[i];
					}	
				}
				var sum = Rnd.length;
				this.AdIndex = parseInt(Math.random() * sum);
				break;
				
			case 1: 
			    var rightValue = 0;
				var ad = null;
				for (var i = 0; i < this.AdList.length; i++){
					if(this.AdList[i].AdRight > rightValue){
						rightValue = this.AdList[i].AdRight;
						this.AdIndex = i;
					}
				}
				break;
		}
	},
	
	Add : function(){
		if(arguments[0]){
			var index = this.AdList.length;
			this.AdList[index] = arguments[0];
		}
	}
}


AdMoveWithScreen.Initialize({ ID:8,ZoneName:"在线客服",Width:90,Height:150,ShowType:1,Top:110,Left:5,Speed:0.15,MoveType:1});
AdMoveWithScreen.Add({ID:12,AdName:"在线客服",AdType:0,FileUrl:"/Uploadfiles/201108/201108050911077450.gif",LinkUrl:"2412350399",AdRight:1,OpenType:"_blank"});
AdMoveWithScreen.Show();
