document.domain	="51.com";
//判断并加载JQ
if(typeof jQuery !== "function"){
	document.write('<scr'+'ipt src="http://pic.51img1.com/i/js/jquery.js" language="javascript"></s'+'cript>');
}
if(typeof(_ad2009f_init)=="undefined"){
	var _ad2009f_init=function(){	//请求广告数据，在页面最后部分
		try{
			_ad2009r	=_ad2009r.replace(/adv09_/g,",fo_").split(",").slice(1);
		}catch(e){
			return;
		}

		var _ad2009_script	=document.createElement("SCRIPT");
		_ad2009_script.src	=_ad2009v_url+"?domain="+document.domain+"&id="+_ad2009r.toString()+"&time="+(new Date()).getTime();
		document.getElementsByTagName("head")[0].appendChild(_ad2009_script);
		_ad2009f_wait_for_return();
	}

	var _ad2009f_wait_for_return	=function(){	//等待接收广告数据
		try{
			var t	=_adv09_data.s;
		}catch(e){
			window.setTimeout("_ad2009f_wait_for_return()",100);
			return;
		}
		_ad2009f_create_ad();
	}

	var _ad2009f_GetCookie=function(sName){
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++){
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0]) 
		    	return aCrumb[1];
		}
		return null;
	}

	var _ad2009f_click=function(id, sns){
		url = _ad2009c_get + '?type=2&posid=&adid=' + id+"&time="+(new Date()).getTime();	//加随机数防止IE不处理重复image url
		var _uinfo	=_ad2009f_GetCookie("FO_LSTATE");
		if(_uinfo){
			_uinfo	=_uinfo.split("\%7C");
			url+="&uin="+_uinfo[0]+"&user="+_uinfo[1];
		}

		var _ad09_img	=document.createElement("IMG");
		_ad09_img.src	=url;
		_ad09_img.style.display	="none";
		document.body.appendChild(_ad09_img);

		//活动动态 广告处理
		if ('undefined' != typeof(sns) && 1 == sns) {
			var _ad09_img_sns	=document.createElement("IMG");
			_ad09_img_sns.src	='http://act.51.com/home/adfeed.port.php?adid=' + id +"&time="+(new Date()).getTime();
			_ad09_img_sns.style.display	="none";
			document.body.appendChild(_ad09_img_sns);
		}
	}

	//得到制定id的对象
	function _ad2009_$(id) {
	    return document.getElementById(id);
	}

	//隐藏, 显示图片处理  display hidden handle
	function _ad2009_dhh (id, flag) {
	    if (flag == 0) {
			obj = _ad2009_$(id + 's');
			obj.style.display = 'none';
		    obj1 = _ad2009_$(id + 'l');
			obj1.style.display = 'block';
		}
	    if (flag == 1) {
			obj = _ad2009_$(id + 's');
			obj.style.display = 'block';
		    obj1 = _ad2009_$(id + 'l');
			obj1.style.display = 'none';
		}
	}

	//onmouseout 处理, 使其对子对象有效
	function _ad2009_mouse_out(obj, e, id, flag) {
		if (e.currentTarget) {
			if (e.relatedTarget != obj) {
				if (obj != e.relatedTarget.parentNode) {
					_ad2009_dhh(id, flag);
				}
			}
		} else {
			if (e.toElement != obj) {
				if (obj != e.toElement.parentNode) {
					_ad2009_dhh(id, flag);
				}
			}
		}
	}

	var _ad2009f_effect_ad=function(_obj,_id){	//各种广告样式处理
		switch(_obj.effect){
			case "s1":
				document.getElementById(_id).innerHTML='<iframe src="'+_ad2009g_get+'?id='+_id.replace(/adv09_/g,"fo_")+'" allowTransparency="true" frameborder="0" marginheight="0" marginwidth="0" scrolling="No" width="'+_obj.width+'" height="'+_obj.height+'"></iframe>';
				break;
			case "s2":		//固定对联
			case "s5":		//多行多列自适应文字列表
			case "s6":		//自适应图文广告
			case "s7":		//固定广告
			case "s9":		//静态悬浮广告
			case "s10":		//动态悬浮广告
				document.getElementById(_id).innerHTML=_obj.code;
				break;
			case "s3":		//图片随机轮播
			case "s4":		//带下标的数字轮播
			case "s11":		//文字轮播
			case "s13":		//特殊图片轮播
				document.getElementById(_id).innerHTML=_obj.code;
				var foAdvIsDisplaySpecifiedDiv	=function (id, display, effect) {
				    obj = document.getElementById(id);
					if (obj) {
					    if (display) {
				     		effect ? jQuery("#" + id).fadeIn("slow") : obj.style.display = '';
				        }
				        else {
				     		obj.style.display = 'none';
						}
					}
				}

				var foAdvSetTurnRandomIndexColor=function (id, css_class) {
				    obj = document.getElementById(id);
					if (obj) {
					    obj.className = css_class;
					}
				}

				var fo_ads_trunrandom_index_entry=function(all, current, id_prefix, turn_time, trun_effect) {
					//记录随机时间， 如果是用户触发， 则要清楚已经存在的函数
					//var set_timeout_handle = null;
					this.fo_ads_trunrandom_index = function (all, current, id_prefix, turn_time, trun_effect) {
					    if (current < 1) {current = 1;}
					    if (current > all) {current = all;}
						try{
							for (var j = 1; j <= all; j++) {
							    //隐藏所有图片， 恢复所有颜色
								foAdvSetTurnRandomIndexColor(id_prefix + 'index_' + j, id_prefix + 'normal');
								foAdvIsDisplaySpecifiedDiv(id_prefix + j, 0, trun_effect);
							}
							foAdvSetTurnRandomIndexColor(id_prefix + 'index_' + current, id_prefix + 'select');
						}catch(e){}
						//显示指定图片， 添加指定下标
						foAdvIsDisplaySpecifiedDiv(id_prefix + current, 1, trun_effect);
						//得到下次轮播的序号
						next_index = current + 1;
						if (current == all) {
						    next_index = 1;
						}
						if (fo_ads_trunrandom_index["set_timeout_handle_"+id_prefix] != null) {
						    clearTimeout(fo_ads_trunrandom_index["set_timeout_handle_"+id_prefix]);
						}
						fo_ads_trunrandom_index["set_timeout_handle_"+id_prefix] = setTimeout("fo_ads_trunrandom_index('"+all+"',"+next_index+",\'"+id_prefix+"\'," + turn_time + "," + trun_effect + ")", (turn_time*1000));
					}
					fo_ads_trunrandom_index(all, current, id_prefix, turn_time, trun_effect);
				}
				fo_ads_trunrandom_index_entry(_obj.total, 1, _id+'_', _obj.time, _obj.turn_effect);
				break;

			case "s8":		//文字滚动
				document.getElementById(_id).innerHTML=_obj.code;
				var adv51_scroll_text=function(id,speed) {
					var demo77 = document.getElementById(id);
					var demo88 = document.getElementById(id+'_1');
					var demo99 = document.getElementById(id+'_2');
					demo99.innerHTML=demo88.innerHTML;
					function Marquee(){ 
						  if(demo99.offsetWidth-demo77.scrollLeft<=0) 
						      demo77.scrollLeft-=demo88.offsetWidth;
						  else{
						      demo77.scrollLeft++;
						  } 
					} 
					var MyMar=window.setInterval(Marquee,speed) ;					
					demo77.onmouseover=function() {clearInterval(MyMar)}
					demo77.onmouseout=function() {MyMar=setInterval(Marquee,speed)}
				}
				adv51_scroll_text(_id, 50);
				break;
			case "s12": //背弹广告
			        open_up_cook_name = 'ad2009_tag_' + _id;
			        is_open_up_window = _ad2009f_GetCookie(open_up_cook_name);

			        if ('yes' != is_open_up_window) {
			            //打开窗口
						__xpos = 0; 
					    __ypos = 0;
					    __xpos = (screen.width - _obj.width) / 2; 
					    __ypos = (screen.height - _obj.height) / 2;  
			            __open_url = _ad2009o_get + '?id=' + _id.replace(/adv09_/g,"fo_");
			            __attr     = 'left=' + __xpos + ',top=' + __ypos + ',width=' + _obj.width + ',height=' + _obj.height + ',toolbar=no,menubar=no,scrollbars=no,status=no,location=no';
			            window.open (__open_url, 'open_vda', __attr);
			            document.cookie = "" + open_up_cook_name + "=yes";//记录cookie
			        }
			    break;
		}
	}

	var _ad2009f_check_array=function(){
		if(typeof(_ad2009r)=="string"){
			_ad2009r	=_ad2009r.replace(/fo_/,",");
			_ad2009r	=_ad2009r.split(",");
		}
	}

	var _adv09f_check_css=function(str){
			if(/msie 6\.0/.test(_adv09_br))	//IE6
				return str.replace(/\/\*IE7\*\/.*?\/\*IE7\*\//ig,"");
			else
				return str.replace(/\/\*IE6\*\/.*?\/\*IE6\*\//ig,"");
	}

	var _ad2009f_create_ad=function(){
		if(_adv09_data.s==1){	//广告获取成功
			if(_adv09_data.css!=""){
				_adv09_data.css	=_adv09f_check_css(_adv09_data.css);
				if(!_adv09_ie){	//处理公共样式
					var tmpCss	=document.createElement("DIV");
					tmpCss.innerHTML	="<style>"+_adv09_data.css+"</style>";
					document.getElementsByTagName("head")[0].appendChild(tmpCss);
				}else{
					var newcss	=document.createStyleSheet();
					newcss.cssText	=_adv09_data.css;
				}

			}
			_ad2009f_check_array();
			var _ad2009_ads_id	=new Array();
			for(var i=0;i<_ad2009r.length;i++){
				try{
					var t_obj	=_adv09_data.ad_data[_ad2009r[i].replace(/fo\_/ig,'adv09_')];
					if(!t_obj.enable) continue;
					if (null != t_obj.ad_ads_ids) {
						_ad2009_ads_id=_ad2009_ads_id.concat(t_obj.ad_ads_ids);
					}
				}catch(e){continue;}
				_ad2009f_effect_ad(t_obj,_ad2009r[i].replace(/fo\_/ig,'adv09_'));
			}
			_ad2009f_pv(_ad2009_ads_id);	//创建PV Image
		}
	}

	var _ad2009f_pv=function(ids){
		url = _ad2009c_get + '?type=1&posid=' + window._ad2009r+"&adids="+ids + "&time="+(new Date()).getTime();
		var _uinfo	=_ad2009f_GetCookie("FO_LSTATE");
		if(_uinfo){
			_uinfo	=_uinfo.split("\%7C");
			url+="&uin="+_uinfo[0]+"&user="+_uinfo[1];
		}

		//var _img	=document.createElement("IMG");
		//_img.src	=url;
		//_img.style.display	="none";
		//document.body.appendChild(_img);
		var _ad2009_script_stat	=document.createElement("SCRIPT");
		_ad2009_script_stat.src	= url;
		document.getElementsByTagName("head")[0].appendChild(_ad2009_script_stat);
	}

	window._ad2009v_url	="http://v3.apic.51.com/getConfig.php";

	window._ad2009g_get	="http://v3.apic.51.com/getThirdConfig.php";

	if(/ad09_preview=1/.test(location.href)){
	    window._ad2009v_url	="http://adv2009.51.com/preview/getConfig.php";       //预览请求地址
	    window._ad2009g_get	="http://adv2009.51.com/preview/getThirdConfig.php";  //第三方广告请求地址
	    window._ad2009o_get	="http://adv2009.51.com/preview/getOpenUp.php";  //弹出广告请求地址
	}
	window._ad2009c_get	="http://tj.apic.51.com/vda09_tj.php";
	window._ad2009o_get	="http://v3.apic.51.com/getOpenUp.php";    //弹出广告请求地址

	var _adv09_br =navigator.userAgent.toLowerCase();

	var _adv09_ie = /msie/.test(_adv09_br)&&!/opera/.test(_adv09_br);	

	if(window.ad2009_delay_show!="delay"){
		if(!_adv09_ie){
			document.addEventListener("DOMContentLoaded", _ad2009f_init, false);
		}
		else{
			var sTag		="script";
			var tmpScript	="<"+sTag+" src='javascript:void(0)' defer onreadystatechange=\"if(this.readyState=='complete'){this.onreadystatechange = null;setTimeout('_ad2009f_init()',500)}\"></"+sTag+">";
			document.write(tmpScript);
		}
	}
}
