var AdBanner = {	
	ID : null,
	ZoneName : null,
	Width : 750,
	Height : 80,
	ShowType : 0,
	AdList : null,
	LoopNum : 0,
	DivName : "",
	Delay: 3000,
	IsIE : true,
	Speed: 15,
	
	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.AdList = new Array();
		this.DivName = "AD_Banner_" + this.ID;
		this.IsIE = this.Browser();
		this.Speed = this.IsIE ? 15 : 5;
		var div = "<div id='"+ this.DivName +"' style='width: "+ this.Width +"; height: "+ this.Height +"; '></div>";
		document.write(div);
	},
	
	Show : function(){
		if(!this.AdList || this.AdList.length == 0) return;
		this.GetAdIndex();
		this.GetAd(this.LoopNum).style.display = "";		
		if(this.ShowType == 2 && this.AdList.length > 1){
			var me = this;
			setTimeout(function(){ me.Loop(); }, this.Delay);
		}
	},
	
	GetAd : function(n){		
		return document.getElementById(this.DivName +"_"+ n);
	},
	
	Browser : function(){
		var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'
		return !!window.attachEvent && !isOpera;
	},
	
	Loop : function(){		
		var banner = document.getElementById(this.DivName);
		var divs = banner.getElementsByTagName("div");
		var current = this.LoopNum;	
		var alpha = 100;
		var me = this;	
		if(this.LoopNum < this.AdList.length - 1){
			this.LoopNum++; 
		}else{
			this.LoopNum = 0;
		}
		this.GetAd(this.LoopNum).style.display = "block";
		this.GetAd(this.LoopNum).style.zIndex = 1;
		this.GetAd(current).style.zIndex = 2;
		
		var clock = setInterval(function(){
				alpha = Math.max(alpha - me.Speed, 0);
				me.GetAd(current).style.opacity = alpha / 100;
				me.GetAd(current).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+ alpha +")";
				if (!alpha){
					clearInterval(clock);
					me.GetAd(current).style.display = "none";
					me.GetAd(current).style.opacity=100;
					me.GetAd(current).style.filter = "";			
				}
			}, 20);		
		setTimeout(function(){ me.Loop(); }, this.Delay);
	},
	
	GetAdContent : function(){
		var ad = arguments[0];
		if(!ad) return;
		var strContent = "";
		if(ad.AdType == 0){
			if(ad.LinkUrl){
			strContent += "<a href='"+ ad.LinkUrl +"' 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.LoopNum = 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.LoopNum = i;
					}
				}
				break;
				
			case 2: //按顺序循环显示，此方式仅对矩形横幅有效
			    this.LoopNum = 0;
				break;
		}
	},
	
	Add : function(){
		if(arguments[0]){
			var index = this.AdList.length;
			this.AdList[index] = arguments[0];
			var banner = document.getElementById(this.DivName);
			var child = document.createElement("div");
			child.id = this.DivName + "_" + index;
			child.style.display = "none";
			child.style.zIndex = 0;
			child.style.position = "absolute";
			child.innerHTML = this.GetAdContent(arguments[0]);
			banner.appendChild(child);
		}
	}
}


AdBanner.Initialize({ ID:2,ZoneName:"抬头广告",Width:468,Height:60,ShowType:0});
AdBanner.Add({ID:4,AdName:"三亚",AdType:0,FileUrl:"/Uploadfiles/2011/03/24/201103242136393630.jpg",LinkUrl:"",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:5,AdName:"邮轮假期",AdType:0,FileUrl:"/Uploadfiles/2011/07/08/201107081916191439.jpg",LinkUrl:"/Travel/ShowClass.aspx?ClassId=4",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:7,AdName:"东部华侨城",AdType:0,FileUrl:"/Uploadfiles/2011/04/16/201104161747352207.jpg",LinkUrl:"/Travel/Search.aspx?DepartCity=%B9%E3%D6%DD&DepartCityId=48&ClassId=&Price=&ThemeId=0&TravelDays=&TravelName=%B6%AB%B2%BF%BB%AA%C7%C8%B3%C7",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:8,AdName:"会议旅游",AdType:0,FileUrl:"/Uploadfiles/2011/04/16/201104162101025838.jpg",LinkUrl:"/Meeting",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:9,AdName:"明明白白旅游",AdType:0,FileUrl:"/Uploadfiles/2011/04/16/201104162101445473.gif",LinkUrl:"/Travel",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:18,AdName:"自组团",AdType:0,FileUrl:"/Uploadfiles/2011/05/17/201105171042230089.jpg",LinkUrl:"/Travel",AdRight:1,OpenType:"_blank"});
AdBanner.Add({ID:20,AdName:"节后旅游",AdType:0,FileUrl:"/Uploadfiles/2011/06/12/201106121039468928.jpg",LinkUrl:"/Travel",AdRight:1,OpenType:"_blank"});
AdBanner.Show();
