/*
轮播广告类 RotatorAD

格式：new RotatorAD(商业广告数组, 非商业广告数组, 层id)
说明：第一次访问随机出现，以后访问顺序轮播；自动过滤过期广告；cookie时间24小时；商业广告数量不足时，从非商业广告中补充
*/
if(typeof(RotatorAD)!='function'){
	var RotatorAD=function (rad,nad,div_id){
		this.ary = new Array();
		this.date = new Date();
		this.w = rad.width;
		this.h = rad.height;
		this.o = rad.num; //rad.length;
		this.id = RotatorAD.id++;
		this.m = div_id+'_'+this.id;
		this.n = new Array();
		this.L = new Date();
		this.e = 0;
		var f;
		var D = false;
		var nn = 0;
		//过滤无效广告
		for(var i=0; i<rad.length; i++){
			var start = RotatorAD.strToDate(rad[i][2]);
			var end = RotatorAD.strToDate(rad[i][3],true);
			if(this.date>start && this.date<end){
				this.ary.push([rad[i][0], rad[i][1], rad[i][4]]);
			}
		}
		//补位
		if(nad.length>0){
			for(var i=0; i<rad.num; i++){
				if(i>this.ary.length-1){
					this.ary.push([nad[nn][0], nad[nn][1], ""]);
					if(++nn > nad.length-1) nn = 0;
				}
			}
		}else{
			this.o = this.ary.length;
		}
		//取id
		for(var i=0;i<this.o;i++){
			f=this.m+'_'+(i+1);
			g=RotatorAD.G(f);
			if(g!=''){
				this.n[i]=g;
				D=true;
			}else {
				this.n[i]=0;
			}
		}
		if(!D){
			var r=Math.ceil(Math.random()*this.o);
			var t=this.m+'_'+r;
			RotatorAD.S(t,this.L.getTime(),1440);
			this.e=r;
			if(this.o==1){RotatorAD.S('s_dl',r,1440);}
			//return r;
		}else {
			var R=this.n.join(',').split(',');
			var k=R.sort();
			var max=Number(k[k.length-1]);
			var min=Number(k[0]);
			var F;
			for(var i=0;i<this.n.length;i++){
				if(max==this.n[i]){
					F=i+1;
					break;
				}
			}
			if(typeof(F)!='undefined'){
				G=this.m+'_'+F;
				H=Number(RotatorAD.G(G));
				I=F%this.o+1;
				J=this.m+'_'+I;
				RotatorAD.S(J,this.L.getTime(),1440);
				if(this.o==1){
					I=-RotatorAD.G('s_dl');
					if(I==0){I=1;RotatorAD.S('s_dl',1,1440);}
					RotatorAD.S('s_dl',I,1440);
				}
				this.e=I;
				//return I;
			}
		}
		//Show AD
		if(this.e==0 || this.ary.length==0) return; //如果没有广告则不显示
		if(this.e==-1) this.e=1; //return(); //当只有一个广告时：始终显示第一个/奇数次刷新显示
		var n = this.e-1;
		var type = this.ary[n][0].substring(this.ary[n][0].length-3).toLowerCase();
		var od = document.getElementById(div_id);
		if(type=='swf'){
			var of = new sinaFlash(this.ary[n][0], div_id+'_swf', this.w, this.h, "7", "", false, "High");
			of.addParam("wmode", "opaque");
			of.addVariable("adlink", escape(this.ary[n][1]));
			of.write(div_id);
		}else if(type=='jpg' || type=='gif'){
			od.innerHTML = '<a href="'+this.ary[n][1]+'" target="_blank"><img src="'+this.ary[n][0]+'" border="0" width="'+this.w+'" height="'+this.h+'" /></a>';
		}else if(type=='htm' || type=='tml'){
			od.innerHTML = '<iframe id="ifm_'+div_id+'" frameborder="0" scrolling="no" width="'+this.w+'" height="'+this.h+'"></iframe>';
			document.getElementById('ifm_'+div_id).src = this.ary[n][0];
		}
		if(this.ary[n][2]!=""){ //ad counter
			var oImg = new Image();
			oImg.src = this.ary[n][2];
		}
	};
	RotatorAD.id=1;
	RotatorAD.G=function (N){
		var c=document.cookie.split("; ");
		for(var i=0;i<c.length;i++){
			var d=c[i].split("=");
			if(d[0]==N)return unescape(d[1]);
		}return '';
	};
	RotatorAD.S=function (N,V,Q){
		var L=new Date();
		var z=new Date(L.getTime()+Q*60000);
		var d = document.domain!=""?("domain="+document.domain+";"):"";
		document.cookie=N+"="+escape(V)+";path=/;"+d+"expires="+z.toGMTString()+";";
	};
	RotatorAD.strToDate = function(str,ext){
		var arys = new Array();
		arys = str.split('-');
		var newDate = new Date(arys[0],arys[1]-1,arys[2],9,0,0);
		if(ext){
			newDate = new Date(newDate.getTime()+1000*60*60*24);
		}
		return newDate;
	} 
};

