// 2009-6-17 jack 51as.com@gmail.com
/*var astojs_swf = 'flash/loadurltojs.swf';
var gif_swf ="flash/ad_temp.swf";
var swf_swf ="flash/ad_play.swf";
var withouturl_swf = "flash/ad_play_withoutURL.swf";
var tl_banner_661x78 = "flash/tl_banner.swf";
var parseCallPlay = 'flash/parse.swf'; 
var sizhiplay = 'flash/sizhiplay.swf';
var gif_doc = "img/doc.gif";
var newplay = "flash/jshow.swf";
*/

function sjmanage(config) {
	if (config.type == "swf") {
		writeFlashTo(config.div,swf_swf+"?ad_swf="+config.image+"&ad_url="+config.url,config.w,config.h, {"AllowScriptAccess":"always"});
	} else if (config.type == "gif" || config.type == "jpg") {
		document.getElementById(config.div).innerHTML = '<a target="_BLANK" href="'+unescape(config.url)+'" rel="nofollow"><img width="'+config.w+'" height="'+config.h+'" border="0" alt="" src="'+config.image+'"/></a>';
	} else {
		var t_str = config.div;
		if (t_str.indexOf("_child") != -1) {
			closediv(t_str.replace('_child', ""));
		} else {
			closediv(t_str);
		}
	}
}
//type 1:单页广告 gif swf兼容，2：不兼容swf只能播gif,3：单广告播swf不兼容gif，4：单广告播swf不屏蔽url不兼容gif，5:翻牌轮播，
function addAD(adcall, id, w, h, type, scr) {

	if (type == 1) {
		writeFlashTo(id,astojs_swf + '?url0='+adcall+'&jsfun=sjmanage&divid='+id+'&w='+w+'&h='+h,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
	} else if (type == 2) {
		this[id+'_id'] = id;
		this[id+'_fun'] = function (str) {
			document.getElementById(this[id+'_id']).innerHTML = str;
		};
		writeFlashTo(id,gif_swf + '?url0='+adcall+'&jsfun='+id+'_fun&divid='+id+'&w='+w+'&h='+h,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
	} else if (type == 3) {
		writeFlashTo(id,swf_swf + '?url0='+adcall+'&jsfun='+id+'_fun&divid='+id+'&w='+w+'&h='+h,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
	} else if (type == 4) {
		writeFlashTo(id,withouturl_swf + '?url0='+adcall+'&jsfun='+id+'_fun&divid='+id+'&w='+w+'&h='+h,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
	} else if (type == 5) {
		if (Object.prototype.toString.apply(adcall) === '[object Array]') {
			var len = adcall.length;
			var callstr = "";
			for (var i = 0; i<len; i++) {
				callstr += ('url'+i+'='+adcall[i]+'&');
			}
			writeFlashTo(id,scr + '?'+callstr,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
		} else {
			//alert("adcall 在 type=5时，必须以数组形式传入");
		}
	}
}
//对联 
//这个是对联的函数  第一是 radcall 是右边的call , 第二是左边call,第三是 对联的宽度, 第四是 高度, 第五是 t -> top 是页面上边距离多少像素
function duilian(radcall, ladcall, w, h, t, r, l) {
	var rdiv = document.createElement('div');
	var ldiv = document.createElement('div');
	rdiv.id = 'duilian1';
	ldiv.id = 'duilian2';

	rdiv.style.position = "absolute";
	rdiv.style.visibility = "visible";
	rdiv.style.zIndex = "999";
	rdiv.style.top = t+"px";
	rdiv.style.right = r+"px";
	ldiv.style.position = "absolute";
	ldiv.style.visibility = "visible";
	ldiv.style.zIndex = "999";
	ldiv.style.top = t+"px";
	ldiv.style.left = l+"px";
	rdiv.innerHTML = '<div id="duilian1_child"></div><table width="50" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"><tbody><tr><td align="right"  style="padding-top: 2px; padding-right: 3px;"><div  style="font-size: 12px; color: rgb(0, 0, 0); text-decoration: none; cursor:pointer" onclick="javascript:closediv(\'duilian1\',\'duilian2\')" >关闭</div></td></tr></tbody></table>';
	ldiv.innerHTML = '<div id="duilian2_child"></div><table width="50" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"><tbody><tr><td align="right"  style="padding-top: 2px; padding-right: 3px;"><div  style="font-size: 12px; color: rgb(0, 0, 0); text-decoration: none; cursor:pointer" onclick="javascript:closediv(\'duilian1\',\'duilian2\')" >关闭</div></td></tr></tbody></table>';
	document.body.appendChild(rdiv);
	document.body.appendChild(ldiv);
	addAD(radcall,'duilian1_child',w,h,1);
	addAD(ladcall,'duilian2_child',w,h,1);
}
//浮动消失
function fubiao(adcall, w, h, t, r) {
	var fdiv = document.createElement('div');
	fdiv.id = "fd_ad";
	fdiv.style.position = "absolute";
	fdiv.style.visibility = "visible";
	fdiv.style.zIndex = "999";
	fdiv.style.top = t+"px";
	fdiv.style.right = r+"px";
	document.body.appendChild(fdiv);
	addAD(adcall,'fd_ad',w,h,1);
	window.setTimeout(function () {
	var fd = document.getElementById('fd_ad');
	document.body.removeChild(fd);
	},8000);
}
//关闭div
function closediv(id1, id2) {
	var fd = document.getElementById(id1);
	document.body.removeChild(fd);
	fd = document.getElementById(id2);
	if (fd != null) {
		document.body.removeChild(fd);
	}
}
//ufodiv 幽灵div 随滚动条滚动
var ufoarr = [];
var ufotime = -1;

function ufodiv(adcall, w, h, p, id) {
	var ufo = document.createElement("div");
	if (id === undefined) {
		ufo.id = "ufo";
	} else {
		ufo.id = id;
	}
	var temp;
	if (p[0] == "top") {
		temp = Number(p[1])+Number(document.documentElement.scrollTop);
	} else if (p[0] == "bottom") {
		temp = Number(p[1])-Number(document.documentElement.scrollTop);
	}


	ufo.style.position = "absolute";
	ufo.style.visibility = "visible";
	ufo.style.zIndex = "999";
	ufo.style[p[0]] = temp+"px";
	ufo.style[p[2]] = p[3]+"px";
	ufo.innerHTML = '<div id="'+ufo.id+'_child"></div><table width="100" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"><tbody><tr><td align="right" style="padding-top: 2px; padding-right: 3px;"><div  style="font-size: 12px; color: rgb(0, 0, 0); text-decoration: none; cursor:pointer" onclick="javascript:closediv(\''+ufo.id+'\')"  >关闭</div></td></tr></tbody></table>';
	document.body.appendChild(ufo);
	addAD(adcall,ufo.id+'_child',w,h,1);
	ufoarr.push([ufo, p]);
	if (ufotime == -1) {
		ufotime = window.setInterval(function () {
			var len = ufoarr.length;
			if (len>=1) {
				for (var i = 0; i<len; i++) {
					var t;
					if (ufoarr[i][1][0] == "top") {
						t = Number(ufoarr[i][1][1])+Number(document.documentElement.scrollTop);
					} else if (ufoarr[i][1][0] == "bottom") {
						t = Number(ufoarr[i][1][1])-Number(document.documentElement.scrollTop);
					}
					ufoarr[i][0].style[ufoarr[i][1][0]] = t+"px";
					ufoarr[i][0].style[ufoarr[i][1][2]] = ufoarr[i][1][3]+"px";
				}
			} else {
				return;
			}
		}, 300);
	}


}
//撕纸广告
var ad_list = {};

function paper_ad(adcall, w, h, p) {
	var t_div = document.createElement('div');
	t_div.style.position = "absolute";
	t_div.style.visibility = "visible";
	t_div.style.top = "100px";
	var id = (new Date()).getTime();t_div.id =id;ad_list[id]={'p':p,'w':w,'h':h,'callback':paper_ad_callback}
	document.body.appendChild(t_div);writeFlashTo(id,parseCallPlay+'?id='+id+"&call="+adcall,1,1, {"id":id+"swf","AllowScriptAccess":"always"});

}
function paper_ad_callback(id) {

	var cfg = ad_list[id]["cfg"];
	var t_div = document.createElement('div');
	t_div.style.position = "absolute";
	t_div.style.visibility = "visible";
	var sw = ad_list[cfg.id]["w"];
	var sh = ad_list[cfg.id]["h"];
	t_div.id = cfg.id+"div";

	t_div.style[ad_list[cfg.id]["p"][0]] = ad_list[cfg.id]["p"][1]+"px";
	t_div.style[ad_list[cfg.id]["p"][2]] = ad_list[cfg.id]["p"][3]+"px";
	t_div.style.zIndex = "999";

	t_div.innerHTML = '<a href="javascript:showpaper(\''+cfg.id+'\');" style="width:'+sw+'px;height:'+sh+'px;position:absolute;top:0; left:0;z-index:9999;"><img src="'+gif_doc+'" style="width:'+sw+'px;height:'+sh+'px;"/></a><div id="'+cfg.id+'_child" ></div><table width="100" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"><tbody><tr><td align="right" style="padding-top: 2px; padding-right: 3px;"><div  style="font-size: 12px; color: rgb(0, 0, 0); text-decoration: none; cursor:pointer" onclick="javascript:closediv(\''+cfg.id+'div\')"  >关闭</div></td></tr></tbody></table>';
	document.body.appendChild(t_div);
	if (cfg.image != "") {
		var t_str = cfg.image;
		var p_str = t_str.substr(-3, 3).toLocaleLowerCase();

		if (p_str == "swf") {
			writeFlashTo(cfg.id+'_child',cfg.image,sw,sh, {"AllowScriptAccess":"always"});
		} else {
			document.getElementById(cfg.id+'_child').innerHTML = '<img width="'+sw+'" height="'+sh+'" border="0" alt="" src="'+cfg.image+'"/>';
		}

	}
	showpaper(cfg.id);
}
function showpaper(id) {
	var cfg = ad_list[id]["cfg"];
	var t_div = document.createElement('div');
	t_div.style.position = "absolute";
	t_div.style.visibility = "visible";
	t_div.id = id+"sizhi";
	t_div.style.top = "100px";
	
	t_div.style.left = (document.documentElement.clientWidth/2 +230)+"px";

	t_div.style.zIndex = 999;
	t_div.innerHTML = getFlashHtml(sizhiplay+"?adpath="+cfg.swf +"&adurl=" + cfg.url+"&div="+id+"sizhi",350,250,{ "id":id + "sizhi","AllowScriptAccess":"always"}); 
	document.body.appendChild(t_div);
}
function parseswf_callback(cfg) {
	document.body.removeChild(document.getElementById(cfg.id));
	if (cfg.error) {
		return;
	}
	if (ad_list[cfg.id]) {
		ad_list[cfg.id]["cfg"] = cfg;
		ad_list[cfg.id]["callback"](cfg.id);
		//t_div.style.top = "100px";
	}
}


function getFlashHtml(src, w, h, opt) {
	var v = "7";
	var fo = new SWFObject(src, ((opt && opt.id) || ""), w, h, v, "#FFFFFF");
	fo.addParam("wmode","transparent");//opaque
	if (opt) {
		for (var x in opt) {
			fo.addParam(x,opt[x]);
		}
	}
	return fo.getSWFHTML();
}

function ufodiv2(adcall, w, h, p, id) {
	var ufo = document.createElement("div");
	if (id === undefined) {
		ufo.id = "ufo";
	} else {
		ufo.id = id;
	}
	var temp;
	if (p[0] == "top") {
		temp = Number(p[1])+Number(document.documentElement.scrollTop);
	} else if (p[0] == "bottom") {
		temp = Number(p[1])-Number(document.documentElement.scrollTop);
	}


	ufo.style.position = "absolute";
	ufo.style.visibility = "visible";
	ufo.style.zIndex = "999";
	ufo.style[p[0]] = temp+"px";
	ufo.style[p[2]] = p[3]+"px";
	ufo.innerHTML = '<div id="'+ufo.id+'_child"></div><table width="'+w+'" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"><tbody><tr><td align="right" style="padding-top: 2px; padding-right: 3px;"><div  style="font-size: 12px; color: rgb(0, 0, 0); text-decoration: none; cursor:pointer" onclick="javascript:closediv(\''+ufo.id+'\')"  >关闭</div></td></tr></tbody></table>';
	document.body.appendChild(ufo);
	//addAD(adcall,ufo.id+'_child',w,h,1);
	var len = adcall.length;
	var callstr = "";
	for (var i = 0; i<len; i++) {
		callstr += ('url'+i+'='+adcall[i]+'&');
	}
	//writeFlashTo(id,scr + '?'+callstr,w,h,{'id':'ad'+id,"AllowScriptAccess":"always"});
	writeFlashTo(ufo.id+'_child',newplay+"?"+ callstr ,w,h, {"AllowScriptAccess":"always","id":ufo.id+"_swf"});
	ufoarr.push([ufo, p]);
	if (ufotime == -1) {
		ufotime = window.setInterval(function () {
			var len = ufoarr.length;
			if (len>=1) {
				for (var i = 0; i<len; i++) {
					var t;
					if (ufoarr[i][1][0] == "top") {
						t = Number(ufoarr[i][1][1])+Number(document.documentElement.scrollTop);
					} else if (ufoarr[i][1][0] == "bottom") {
						t = Number(ufoarr[i][1][1])-Number(document.documentElement.scrollTop);
					}
					ufoarr[i][0].style[ufoarr[i][1][0]] = t+"px";
					ufoarr[i][0].style[ufoarr[i][1][2]] = ufoarr[i][1][3]+"px";
				}
			} else {
				return;
			}
		}, 300);
	}


}
