var servers = new Array();
servers[0]=['电信服务器', 'http://tel.15175.com/medias03300220/'];
servers[1]=['江苏电信',	  'http://js.15175.com/medias03300220/'];
servers[2]=['广东电信',	  'http://gd.15175.com/medias03300220/'];
servers[3]=['网通服务器', 'http://cnc.15175.com/medias03300220/'];
var currentserver=getCookie('currentserver');
if(currentserver!=null && currentserver<servers.length){
serverid=currentserver;
}else{
var randserver=Math.floor(Math.random()*100);
var serverid=0;
if(randserver>=30) serverid=1;
if(randserver>=75) serverid=2;
}
var a_html="<iframe src='http://www1.gamediad.com:7990/adpolicy.aspx?gameid=9&adspotid=917505' height='400' width='550' scrolling='no' frameborder='0' MARGINHEIGHT=0 MARGINWIDTH=0></iframe>";
var lnetshowtime=8000;
var minlnetshowtime=5000;
var servername = servers[serverid][0];
var server     = servers[serverid][1];
setCookie('currentserver',serverid,'/');
var ffile;
var file;
var islogin;
var ml;
var tt = 0;
var width = 550;
var height = 400;
function getPageSize(){
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	arrayPageSize = new Array((windowWidth-28),windowHeight);
	return arrayPageSize;
}
var wsize=getPageSize();
if(typeof(pwidth)!="undefined" && typeof(pheight)!="undefined"){
	if(pwidth>0 && pheight>0){
		var xxx;
		width=pwidth;
		height=pheight;
		if(width>wsize[0] || height>wsize[1]){ 
			if(width/height>wsize[0]/wsize[1]){
				xxx=width/wsize[0];
				width=wsize[0];
				height=height/xxx;
			}else{
				xxx=height/wsize[1];
				height=wsize[1];
				width=width/xxx;
			}
		}
	}
}
var loadnet = "<div id='load_flash' style='background:#000;width:640px;height:475px;padding-top:15px;font-size:18px;font-weight:bold;color:red;'><a href='javascript:window.external.AddFavorite(\"http://www.4399xyx.com/?Favorite\", \"4399\")' style='text-decoration:none;'><font style='font-family:\"Verdana\", \"Arial\";color:#79E80B;font-size:18px;'>www.4399xyx.com</font></a>&nbsp;&nbsp;&nbsp;游戏载入中，请稍候...<br /><br />"+a_html+"<br /></div>";
var Type;
var loadingbar;
var fullplay = false;
var gamehtml;
function getType(){
	var pattern = /(.*)\.(.*)$/gi;
	var fftt=file.split('?');
	var arr = pattern.exec(fftt[0]);
	return RegExp.$2;
}
function fuck(){
	if(!file) {
		sid = (sid=='4399xyx.com')?'':sid+'/';
		file = sid + s + f
	}
	ffile = file;
	if((file.slice(0,7)!="http://") && (file.slice(0,1)!="/")){
		if(server==undefined) server = servers[0][1];
		file=server+file;
	}
}
function addfav(){
	if (!islogin){
		var pageurl = document.location.href.replace("#best","")+"?f";
		window.external.AddFavorite(pageurl, title+' - 4399小游戏');
	}else{
		var msg;
		var url="/user/?action=addfav&id="+id+"&a=1&s="+Math.round(Math.random()*100);
		var req=getreq(url);
		if (req.readyState == 4) { 
			if (req.status == 200) { 
				if (req.responseText == 'notlogin') {
					alert("只有登录帐号后才能使用收藏夹功能!");
				} else if (req.responseText == 'repeat') {
					alert("你选择的小游戏 ["+title+"] 已经在你的收藏夹内了!");
				} else if (req.responseText == 'succeed') {
					alert(title+"小游戏已成功添加到您的收藏夹!");
				} else {
					alert("系统忙,请稍后再试!");
				}
			}
		} else {
			alert("暂时无法使用收藏夹功能");
		}
	}
}
function copyToClipBoard(){
	var clipBoardContent=document.location.href.replace('#best',''); 
	clipBoardContent+='\n这个是我很喜欢的小游戏，名字叫《' + title + '》，真诚向你推荐，希望你也会喜欢哦!';
	window.clipboardData.setData("Text",clipBoardContent);
	alert("耶！复制成功喽！你可以粘贴在QQ消息里送给你的好朋友呀！再次感谢您对本站的支持哦！");
}
function Replay() {
	if(Type=="swf" && document.all){
		$('flash').movie=" ";
		$('flash').movie=server+ffile;
	} else {
		document.location.reload();
	}
}
function startgame(){
	$('game').innerHTML=gamehtml;
	if(Type=="swf") loaded();
	window.setTimeout(closeloadnet,lnetshowtime)
}
function stopgame(){
	if(loadingbar) loadingbar.style.display='none';
	$('game').innerHTML="<div style='background:#ccc;width:"+width+"px;height:"+(height-150)+"px;padding-top:150px;'><a style='font-size:18px;font-weight:bold;color:red;' href='javascript:startgame();'>游戏暂停中，点击继续</a></div>";
}
function FullPlay() {
	var ft = ffile.split("?");
	stopgame();
	playwin = window.open("/FullPlay.htm?id="+id+"&file="+ft[0]+((ft[1])?"&fv="+encodeURIComponent(file.replace(ft[0]+'?','')):"")+"&title="+encodeURIComponent(title)+"&anw="+anw,"","fullscreen");
}
function changeserver(sid){
	for (var i=0; i<servers.length; i++){
		$('s'+i).className=(sid==i)?'current':'';
	}
	servername = servers[sid][0];
	server = servers[sid][1];
	if((ffile.slice(0,7)!="http://") && (ffile.slice(0,1)!="/")){
		if(Type=="swf"){
			$('flash').movie=server+ffile;
			loaded();
		}
		if(Type=="htm")	igame.location=server+ffile+"?width="+width+"&height="+height+"&anw="+anw;
	}
	setCookie('currentserver', sid, '/');
}
function makeFlashPlayer(src, id, width, height){
	var wmode="window";
	if(navigator.userAgent.indexOf("Firefox")>0) wmode='opaque';
	if(typeof(anw)=="undefined") anw=0;
	var allownetworking=(anw==1)?"internal":"all";
	if(typeof(fv)!="undefined") src=src+"?"+fv;
	document.write("<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' id='"+id+"' WIDTH='"+width+"' HEIGHT='"+height+"' allownetworking='"+allownetworking+"'><PARAM NAME=src VALUE='"+src+"'><param name='allowScriptAccess' value='sameDomain'><PARAM NAME='AllowNetworking' VALUE='"+allownetworking+"'><PARAM NAME=quality VALUE='high'><param name='wmode' value='"+wmode+"'><EMBED src='"+src+"' id='"+id+"' loop=false menu=true WIDTH="+width+" HEIGHT="+height+" TYPE='application/x-shockwave-flash' wmode='"+wmode+"' PLUGINSPAGE='=http://www.macromedia.com/shockwave/download/index.cgi?P1_Pro d_Version=ShockwaveFlash' allownetworking='"+allownetworking+"'></EMBED></OBJECT>");
}
function makeDcrPlayer(src, id, width, height){
	document.write('<span id="sfhelp" style="font-weight:bold;color:red;">如果这个游戏无法播放，请 <a href="/help/help.htm" target="_blank" style="color:#fff;">点这里安装插件！</a> <a href="javascript:$s(\'sfhelp\')" style="color:red">[关闭提示]</a></span><object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=11,0,0,465" width="'+width+'" height="'+height+'" id="'+id+'"><param name="src" value="'+src+'"><param name="logo" value="false"><param name="playerVersion" value="11"><param name="menu" value="false"><param name="bgColor" value="#000000"><embed src="'+src+'" id="'+id+'" logo="false" menu="false" pluginspage="http://www.macromedia.com/shockwave/download/" width="'+width+'" height="'+height+'"></embed></object>');
}
function makeIframe(src, id, width, height){
	if(typeof(anw)=="undefined") anw=0;
	if(typeof(fv)!="undefined") {
		src=src+"?"+fv;
	} else {
		src=(file.slice(0,13)=="/gamelink.htm")?src:src+"?width="+width+"&height="+height+"&anw="+anw;
	}
	document.write("<iframe name='igame' id='"+id+"' src='"+src+"' width='"+width+"' height='"+height+"' marginwidth=0 marginheight=0 scrolling=no border=0 frameborder=0></iframe>");	
}
function loaded(){
	tt = tt+30;
	if(loadingbar){
		loadingbar.style.display='';
		bg = $('bar');
		bb = $('percent');
	}
	if($('flash').PercentLoaded()<0) {
		hid();
	} else {
		if(loadingbar){
			bg.style.width=4 * $('flash').PercentLoaded();
			bb.innerHTML=$('flash').PercentLoaded() + '%';
		}
	}
	if($('flash').PercentLoaded()<100) {
		if(tt>lnetshowtime) hid();
		var timeout = window.setTimeout(loaded,30);
	} else {
		if(loadingbar) loadingbar.style.display='none';
		setCookie('visited'+id,'yes','/');
	}
}
function hid(){
	//if(document.all && $('flash').PercentLoaded()==100 && !fullplay) document.location.href="#best";
	if($('load_flash')!=null) $('load_flash').style.display='none';
}
function sflash(s){
	var fs=$('flash').style;if(s){fs.width=width;fs.height=height;$('flash').style.display='';}else{fs.width=fs.height=0;$('flash').style.display='none';}
}
function closeloadnet(){
	$('load_flash').style.display='none';
	lnetshowtime=0;
	sflash('s');
}
function play(){
	fuck();
	if($('percentdiv')!=null) loadingbar = $('percentdiv');
	Type = getType();
	if(!fullplay){
		var s_html="";
		for (var i=0; i<servers.length; i++){
			if(serverid==i){
				s_html+="<a hideFocus id='s"+i+"' class='current' href='javascript:changeserver("+i+");' title='当前所在服务器'>"+servers[i][0]+"</a>";
			} else {
				s_html+="<a hideFocus id='s"+i+"' href='javascript:changeserver("+i+");'>"+servers[i][0]+"</a>";
			}
		}
		var qqsq = "<a href=\"javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri=\'+encodeURIComponent(document.location.href+'?qq')+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)\"><font color=\"#fc470f\">收藏到QQ书签</font></a>";
		if($('servers')!=null)
		$('servers').innerHTML="<div class=\"help\"><a href=\"/app/speed.htm\" target=\"_blank\">请选择较快的服务器</a> → </div><div class=\"center\">"+s_html+"</div><div class=\"right\"><a href=\"/help/\" target=\"_blank\">游戏帮助</a>&nbsp; &nbsp;<a href=\"javascript:window.external.AddFavorite('http://www.4399xyx.com/?Favorite', '4399')\">收藏4399小游戏</a></div><div class=\"spacer\"></div>";
	}
	if(loadingbar) loadingbar.style.display='none';
	if(getCookie('visited'+id)=='yes') lnetshowtime=minlnetshowtime;
	switch(Type){
	case "dcr":
	case "dir":
		makeDcrPlayer(file, "flash", width, height);
		break;
	case "htm":
	case "html":
		makeIframe(file, "flash", width, height);
		sflash();
		document.write(loadnet);
		if(lnetshowtime>0) window.setTimeout(closeloadnet,lnetshowtime);
		break;
	default: 
		if(document.all && ml!=false){
			if(loadingbar) loadingbar.style.display='';
       			makeFlashPlayer(file, "flash", width, height);
			sflash();
			loaded();
			document.write(loadnet);
			if(lnetshowtime>0) window.setTimeout(closeloadnet,lnetshowtime);
		} else {
       			makeFlashPlayer("/images/1.swf", "load_flash", width, height);
       			makeFlashPlayer(file, "flash", width, height);
			$('flash').style.display='none';
		}
		break;
	}
	if(!fullplay) gamehtml=$('game').innerHTML;
}