(function(){
var w=window, d=w.document, r=Math.ceil(( Math.random()*1000000)), $=null;

w.sguni_ie = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;
w.sguni_ie = (navigator.appVersion.indexOf("MSIE 5")!=-1 || w.sguni_ie==true) ? true : false;
w.sguni_ie = (document.all || w.sguni_ie==true)? true:false;

var sguni_idx = new Array();
sguni_idx[1] = 0;
sguni_idx[2] = 1;
sguni_idx[4] = 2;
sguni_idx[5] = 3;
sguni_idx[6] = 4;
sguni_idx[7] = 5;
sguni_idx[8] = 6;
sguni_idx[9] = 7;
sguni_idx[10] = 8;
sguni_idx[11] = 9;
sguni_idx[12] = 10;
sguni_idx[13] = 11;
sguni_idx[14] = 12;
sguni_idx[15] = 13;
sguni_idx[16] = 14;
sguni_idx[17] = 15;
sguni_idx[18] = 16;
sguni_idx[19] = 17;
sguni_idx[20] = 18;
sguni_idx[21] = 19;
sguni_idx[22] = 20;
sguni_idx[23] = 21;
sguni_idx[24] = 22;
sguni_idx[25] = 23;
sguni_idx[26] = 24;
sguni_idx[27] = 25;
sguni_idx[28] = 26;
sguni_idx[29] = 27;
sguni_idx[30] = 28;
sguni_idx[31] = 29;
sguni_idx[32] = 30;
sguni_idx[33] = 31;
sguni_idx[34] = 32;
sguni_idx[35] = 33;
sguni_idx[36] = 34;
sguni_idx[37] = 35;
sguni_idx[38] = 36;
sguni_idx[39] = 37;
sguni_idx[40] = 38;
sguni_idx[41] = 39;
sguni_idx[42] = 40;
sguni_idx[56] = 41;
sguni_idx[43] = 42;
sguni_idx[44] = 43;
sguni_idx[45] = 44;
sguni_idx[48] = 45;
sguni_idx[46] = 46;
sguni_idx[47] = 47;
sguni_idx[49] = 48;
sguni_idx[50] = 49;
sguni_idx[51] = 50;
sguni_idx[52] = 51;
sguni_idx[53] = 52;
sguni_idx[54] = 53;
sguni_idx[55] = 54;
sguni_idx[57] = 55;
sguni_idx[58] = 56;
sguni_idx[59] = 57;
sguni_idx[60] = 58;
sguni_idx[61] = 59;
sguni_idx[62] = 60;
sguni_idx[63] = 61;
sguni_idx[64] = 62;
sguni_idx[65] = 63;
sguni_idx[66] = 64;
sguni_idx[67] = 65;
sguni_idx[68] = 66;
sguni_idx[69] = 67;
sguni_idx[70] = 68;
sguni_idx[71] = 69;
sguni_idx[72] = 70;
sguni_idx[73] = 71;
sguni_idx[74] = 72;
sguni_idx[75] = 73;
sguni_idx[76] = 74;
sguni_idx[77] = 75;
sguni_idx[78] = 76;
sguni_idx[79] = 77;
sguni_idx[80] = 78;
sguni_idx[81] = 79;
sguni_idx[82] = 80;
sguni_idx[83] = 81;
sguni_idx[84] = 82;
sguni_idx[85] = 83;
sguni_idx[86] = 84;
sguni_idx[87] = 85;
sguni_idx[88] = 86;
sguni_idx[89] = 87;
sguni_idx[90] = 88;

var IE_all_cache = new Object(); //{{{ 定义 $

function IE_getElementById(id) {
	if (IE_all_cache[id] == null)
		IE_all_cache[id] = d.all[id];
	return IE_all_cache[id];
};

if (d.all) {
	if (!d.getElementById)
	{
		d.getElementById = IE_getElementById;
	}
};
	
var $=function(){
	if (typeof(arguments[0])!='string' || !arguments[0] || arguments[0]=="")
		return false;
	return d.getElementById(arguments[0]);
};
//}}}

function _es(s,u) //{{{ 编码URL
{
	if (typeof(encodeURIComponent) == 'function')
	{
		if (u)
			return encodeURI(s);
		else
			return encodeURIComponent(s);
	} else {
		return escape(s);
	}
}
//}}}

function _ues(s) //{{{ 解码URL
{
	if (typeof(decodeURIComponent) == 'function')
	{
		return decodeURIComponent(s);
	} else {
		return unescape(s);
	}
}
//}}}

function _adp(m,v,a)//{{{ 添加参数到路径和跟踪
{
	if (v!=="")
	{
		if (!a)
		{
			w.sguni_url+="/"+m+"_"+_es(v);
		}
		w.sguni_log_url+="&"+m+"="+_es(v);
	}
}
//}}}

function _cgu(){ //{{{ 清空变量
	w.sguni_client = "sguni";
	w.sguni_sclient = "0";
	w.sguni_style = "1";
	w.sguni_url = "";
	w.sguni_action = "Fixed";
	w.sguni_css_width = 0;
	w.sguni_css_height = 0;
}
//}}}

function _hax(d){ //{{{
	if (!d || d=="") return 1;
	var h=0,g=0;
	for (var i=d.length-1;i>=0;i--)
	{
		var c=parseInt(d.charCodeAt(i));
		h=((h << 6) & 0xfffffff) + c + (c << 14);
		if ((g=h & 0xfe00000)!=0) h=(h ^ (g >> 21));
	}
	return h;
}
//}}}

function D() //{{{ Get Div + Iframe
{
	if (!w.sguni_number || w.sguni_number==0)
		w.sguni_number=1;
	else
		w.sguni_number++;

	var zindex = 5461 + w.sguni_number;
	
	w.sguni_div_html = '<div id="sgunid_' + w.sguni_number + '" align="center" valign="center" style="z-index:'+zindex+';';

	w.sguni_iframe_html = '<iframe marginheight="0" marginwidth="0" id="sgunif_' + w.sguni_number + '" frameborder="0" scrolling="no" ';

	// w.sguni_div_html += ' border: 1px solid #cccccc;';

	if (w.sguni_css_width && w.sguni_css_width>0)
	{
		w.sguni_div_html += ' width:' + w.sguni_css_width + 'px;';
		w.sguni_iframe_html += ' width="' + w.sguni_css_width + '"';
	} else {
		return false;
	}
	
	if (w.sguni_css_height && w.sguni_css_height>0)
	{
		w.sguni_div_html += ' height:' + w.sguni_css_height + 'px;';
		w.sguni_iframe_html += ' height="' + w.sguni_css_height + '"';
	} else {
		return false;
	}

	w.sguni_iframe_html += ' src="' + w.sguni_url + '" style="overflow:hidden; top:0px; left:0px;"></iframe>';

	var _dH = document.documentElement.offsetHeight - w.sguni_css_height;
	var _dW = document.documentElement.offsetWidth - w.sguni_css_width;

	if (!w.sguni_ie)
	{
		_dH = document.body.clientHeight - w.sguni_css_height;
		_dW = document.body.clientWidth - w.sguni_css_width;
	}

	var _top = Math.ceil(Math.random()*_dH );
	var _left = Math.ceil(Math.random()*_dW);
	//alert(_top + " - " + _left)
	if (w.sguni_action=="LeftTop" || w.sguni_action=="LeftBottom" || w.sguni_action=="RightTop" || w.sguni_action=="RightBottom" || w.sguni_action=="Floats")
		w.sguni_div_html += ' position:absolute; top:' + _top + 'px; left:' + _left + 'px;">' + w.sguni_iframe_html + '</div>';
	else
		w.sguni_div_html += '" >' + w.sguni_iframe_html + '</div>';

	return true;
}
//}}}

function U() //{{{ Get Url
{
	w.sguni_url = "http://unigg.17sanguo.com/adsite/html";
	w.sguni_log_url = "http://unigg.17sanguo.com/adsite/sguni_ck.php?type=pv&r=" + r;

	if (w.sguni_style && w.sguni_style!=null)
		_adp('s', w.sguni_style);
	else
		_adp('s', 1);

	// alert(w.sguni_style + ' : ' + sguni_idx[w.sguni_style]);
	if (typeof(sguni_idx[w.sguni_style])=="number")
		_adp('idx', sguni_idx[w.sguni_style]);
	else
		_adp('idx', 0);

	if (!w.sguni_action || w.sguni_action!="") { w.sguni_action="Fixed"; }
	_adp('a', w.sguni_action);

	if (w.sguni_css_width && w.sguni_css_width!=null && w.sguni_css_width!="")
		_adp('w', w.sguni_css_width, true);
	else
		_adp('w', 0, true);

	if (w.sguni_css_height && w.sguni_css_height!=null && w.sguni_css_height!="")
		_adp('h', w.sguni_css_height, true);
	else
		_adp('h', 0, true);
	
	if (w.sguni_client && w.sguni_client!=null && w.sguni_client!="")
		client=w.sguni_client;
	else
		client= 0;

	if (!w.sguni_client || w.sguni_client=="")
		w.sguni_client = '0';
	var result = w.sguni_client.match(/^(.*?),(.*)$/i);

	if (result==null) {
		client = w.sguni_client;
		sclient = 0;
	} else {
		client = result[1];
		sclient = result[2];
	}

	_adp('u', client);
	_adp('u2', sclient);

	_ur=document.referrer;
	if (!_ur || _ur=="") { _ur="-"; }
	_adp('ur', _ur);

	_ul=window.location.href;
	if (!_ul || _ul=="") { _ul="-"; }
	_adp('ul', _ul);

	_ut = '-';
	try{
		_ut=window.top.location.href;
	} catch(e) {}
	
	if (!_ut || _ut=="") { _ut="-"; }
	_adp('ut', _ut);

	_adp('sg','uni.html');
	
	try{
		var im = new Image(0,0);
		im.src = w.sguni_log_url;
	} catch(e){return;}
}
//}}}

function A() //{{{ Get Action "Fixed/LeftTop/LeftBottom/RightTop/RightBottom/Floats/Popup";
{
	if (w.sguni_action=="Popup") {
		w.open(w.sguni_url, "sgunio_" + w.sguni_number, 'height=' + w.sguni_css_height + ', width=' + w.sguni_css_width + ', top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
		return;
	} else  {
		document.writeln(w.sguni_div_html);
	}

	if (w.sguni_action=="LeftTop" || w.sguni_action=="LeftBottom" || w.sguni_action=="RightTop" || w.sguni_action=="RightBottom" || w.sguni_action=="Floats")
	{
		if((mye=$("sgunid_" + w.sguni_number))!=null)
		{
			if (typeof(w.sguni_absolute)!=='object') {
				w.sguni_absolute = new Array;
				sguni_M();
			}

			var key = w.sguni_absolute.length;
			w.sguni_absolute[key] = new Array;
			w.sguni_absolute[key]['id'] = w.sguni_number;
			w.sguni_absolute[key]['action'] = w.sguni_action;
			w.sguni_absolute[key]['topmove'] = Math.ceil(Math.random()*6)-3;
			w.sguni_absolute[key]['topmove'] = (w.sguni_absolute[key]['topmove']==0) ? 1 : w.sguni_absolute[key]['topmove'];
			w.sguni_absolute[key]['leftmove'] = Math.ceil(Math.random()*6)-3;
			w.sguni_absolute[key]['leftmove'] = (w.sguni_absolute[key]['leftmove']==0) ? 1 : w.sguni_absolute[key]['leftmove'];
			w.sguni_absolute[key]['_topmove'] = Math.abs(w.sguni_absolute[key]['topmove']);
			w.sguni_absolute[key]['_leftmove'] = Math.abs(w.sguni_absolute[key]['leftmove']);
			w.sguni_absolute[key]['__topmove'] = 0;
			w.sguni_absolute[key]['__leftmove'] = 0;

			mye.onmouseover=function(){
				w.sguni_absolute[key]['__topmove'] = w.sguni_absolute[key]['topmove'];
				w.sguni_absolute[key]['__leftmove'] = w.sguni_absolute[key]['leftmove'];
				w.sguni_absolute[key]['topmove'] = 0;
				w.sguni_absolute[key]['leftmove'] = 0;
			};

			mye.onmouseout=function(){
				w.sguni_absolute[key]['topmove'] = w.sguni_absolute[key]['__topmove'];
				w.sguni_absolute[key]['leftmove'] = w.sguni_absolute[key]['__leftmove'];
			};
		}
	}
}
//}}}

function G(){ //{{{ 运行
	U(); // Get Url
	if (D()) // Get Div + Iframe
		A(); // Get Action
	_cgu();
}

G();
//}}}
})()

function sguni_M() //{{{ 移动页面
{
	var w = window;
	//alert(uniNumber + ' - ' + uniAction + ' - ' + topMove + ' - ' + leftMove);
	if (typeof(w.sguni_absolute)!=='object')
		return;

	var bT = document.body.scrollTop;
	var bL = document.body.scrollLeft;

	var dH = document.documentElement.offsetHeight;
	var dW = document.documentElement.offsetWidth;

	if (!w.sguni_ie)
	{
		dH = document.body.clientHeight;
		dW = document.body.clientWidth;
	}

	// alert(' ' + bT + ' :' + bL + ' : ' + dH + ' : ' + dW);

	for (ii=0; ii<w.sguni_absolute.length; ii++)
	{
		// alert(' action:' + w.sguni_absolute[ii]['action'] + "\n id:" + w.sguni_absolute[ii]['id'] + "sguni_" + w.sguni_absolute[ii]['id']);
		var mye = null;
		if((mye=document.getElementById("sgunid_" + w.sguni_absolute[ii]['id']))==null)
			continue;
		var action = w.sguni_absolute[ii]['action'];

		if (action=="LeftTop" || action=="LeftBottom" || action=="RightTop" || action=="RightBottom" || action=="Floats") {
			if (!mye) 
				continue;
			else if (!mye.style.position || mye.style.position!="absolute")
				continue;
		} else {
			continue;
		}

		var pW = mye.offsetWidth + 0;
		var pH = mye.offsetHeight + 0;

		var top=mye.style.top.replace("px","")-0, left=mye.style.left.replace("px","")-0;

		if (action=="Floats")
		{
			if (top<=5)				{ w.sguni_absolute[ii]['topmove']=w.sguni_absolute[ii]['_topmove'];};
			if (top>=(dH-5-pH))	{ w.sguni_absolute[ii]['topmove']=0-w.sguni_absolute[ii]['_topmove'];};
			if (left<=5)			{ w.sguni_absolute[ii]['leftmove']=w.sguni_absolute[ii]['_leftmove'];};
			if (left>=(dW-24-pW))	{ w.sguni_absolute[ii]['leftmove']=0-w.sguni_absolute[ii]['_leftmove'];};
			top += w.sguni_absolute[ii]['topmove'];
			left += w.sguni_absolute[ii]['leftmove'];
		}  else if (action=="LeftTop") {
				top=left=2;
		} else if (action=="RightTop") {
				top = 2;
				left = dW-pW-24;
		} else if (action=="LeftBottom") {
				top = dH-pH-5 ;
				left = 2;
		} else if (action=="RightBottom") {
				top = dH-pH-5;
				left = dW-24-pW;
		}
		mye.style.top = top + bT;
		mye.style.left = left + bL;
	}
	w.setTimeout("sguni_M()",30);
}
//}}}