var maxID = 39957;
var nTimeoutId=0;
var isNotFirstLoad=false;
var serverArr = [
	["默认主机","http://1.yx007.com/"],
	["浙江电信1","http://2.yx007.com/"],
	["广东电信","http://3.yx007.com/"],
	["浙江电信2","http://4.yx007.com/"],
	["浙江电信3","http://5.yx007.com/"],
	["美国主机","http://6.yx007.com/"],
	["浙江网通","http://7.yx007.com/"],
	["河南网通","http://8.yx007.com/"],
	["上海电信","http://games.qq.com/images/mini/","qq1$"],
	["上海电信","http://comic.qq.com/flash/","qq2$"],
	["CDN加速1","http://xyx.duowan.com/ufile/","dw$"],
	["CDN加速3","http://games.tom.com/uldf/","tom$"],
	["福建电信","http://files2.17173.com/","sh$"],
	["CDN加速4","http://www.ff130.com:8080/4399swf/","ss$"],
	["河南网通2","http://iask.shangdu.com/flash/","sd$"]
];

Cookie = {
    set : function (name,value,days,path) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
	if(path==undefined || path ==null || path==""){path="/";}
        document.cookie = name+"="+value+expires+";domain=.4399-game.com;path="+path;
    },
    get : function (name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    remove : function (name) {
        this.set(name,"",-1);
    }
}

function AddToFav()
{
	var title="23tt小游戏";
	var url="http://www.23tt.net?fav";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
	  if(window.ActiveXObject) {//ie
		  window.external.AddFavorite( url, title);
	  } else {//chrome
		alert('请按ctrl+D收藏本站，以便你下次再访问');
	  }
	} else if(window.opera && window.print) { // Opera
	  return true; }
	 else { //safri
	 alert('请按ctrl+D收藏本站，以便你下次再访问');}
}

function SetDefaultPage(e)
{
	e.style.behavior='url(#default#homepage)';
	e.setHomePage('http://www.4399-game.com/?def');
}

function InitGame()
{
	var ret='';
	myServerCount=0;
	for (i=0; i<serverArr.length; i++)
	{
		if (serverArr[i].length>2 && gm_data.path2.indexOf(serverArr[i][2])>-1)
		{ret = ret + '<a id="btn_start_'+i+'" href="javascript:StartGame('+i+');void(0);">'+serverArr[i][0]+'</a>';}
		else
		{if(serverArr[i].length==2){ret = ret + '<a id="btn_start_'+i+'" href="javascript:StartGame('+i+');void(0);">'+serverArr[i][0]+'</a>';myServerCount++;}}
	}
	$("#serverList").html(ret);
}

function GetFlashUrl(serverIndex)
{
	var ret=null;
	path2Arr = gm_data.path2.split("|");
	if (gm_data.path.indexOf("http://")>=0){return gm_data.path;}
	if (gm_data.id>=maxID){return "http://1.yx007.com/"+gm_data.path;}

	if (serverIndex>-1)
	{
		var currentServer = serverArr[serverIndex];
		if (currentServer.length==2)
			{ret = currentServer[1]+gm_data.path;}
		else
		{
			for (i=0; i<path2Arr.length; i++)
				{if (path2Arr[i].indexOf(currentServer[2])>-1){return path2Arr[i].replace(currentServer[2],currentServer[1]);}}
		}
	}
	return ret;
}

function refreshProgress()
{
	movie = document.getElementById("flashgame");
	try
	{
			var nPercentLoaded = Math.abs(movie.PercentLoaded());
			if (movie.PercentLoaded()<0){var nPercentLoaded=100;}
	} 
	catch(e) {var nPercentLoaded=100;}

	if (typeof(oldPercentLoaded)=="undefined")
		{oldPercentLoaded=nPercentLoaded;}
	else
		{nPercentLoaded=nPercentLoaded<=oldPercentLoaded ? oldPercentLoaded*2-nPercentLoaded+1 : oldPercentLoaded=nPercentLoaded;}
	$("#bar").css("width",nPercentLoaded+"%");
	if(100==nPercentLoaded){
		if(nTimeoutId){window.clearInterval(nTimeoutId);}
		$("#loading").css("display","none");
		$("#btn_start").css("display","block");
		oldPercentLoaded=undefined;
	}
	else{nTimeoutId = setTimeout('refreshProgress()',10);}
}

function StartGame(server_index)
{
	oldPercentLoaded=undefined;
	if (server_index<myServerCount) {Cookie.set("server_index",server_index,30);}
	$("#serverList>a").css("background","#FFFF95");
	$("#btn_start_"+server_index).css("background","#FF3535");
	flashUrl = GetFlashUrl(server_index);
	baseUrl = flashUrl.substring(0,flashUrl.lastIndexOf("/")+1);
	fileType = "html";
	if (flashUrl.indexOf(".swf")>-1) {fileType="swf";}
	if (isNotFirstLoad)
	{
		if (fileType=="html") {$("#flashgame").attr("src",flashUrl);return;}
		$("#flashgame").attr("base",baseUrl);
		$("#flashgame").attr("movie",flashUrl);
	}else
	{
		isNotFirstLoad = true;
		switch (fileType) {
			case "html":window.str="<iframe id=\"flashgame\" width=\"" + gm_data.width + "\" height=\"" + gm_data.height + "\" src=\"about:blank\" marginheight=0 marginwidth=0 frameborder=0 scrolling=no></iframe>";break;
			default:window.str="<object id=\"flashgame\" style=\"display:none;margin:auto;\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" "+" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0\" "+" width=\"" + gm_data.width + "\" height=\"" + gm_data.height + "\">"+"<param name=movie value=\"" + flashUrl + "\"><param name=\"WMode\" value=\"Window\"><param name=base value=\"" + baseUrl + "\"><param name=quality value=high><embed id=\"em\" src="+flashUrl+" loop=false menu=false base='" + baseUrl + "' quality=high width='"+gm_data.width+"' height='"+gm_data.height+"' play='true' WMode='Window' type=application/x-shockwave-flash "+"pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>"+ "</object>";
		}
		$("#game").html(str);
	}
	$("#game").css({"display":"none","height":gm_data.height+"px"});
	$("#mon2").css("display","");
	$("#loading").css("display","");
	$("#flashgame").css("display","");
	if (fileType=="swf") {refreshProgress();}else{$("#loading").css("display","none");$("#btn_start").css("display","block");}
}

function Start()
{
	$("#mon2").css("display","none");
	$("#game").css("display","");
	$("#btn_start").css("display","none");
	if (fileType=="html") {$("#flashgame").attr("src",flashUrl);}
}

function GetWindowClientSize()	//获取屏幕的宽度和高度
{
var nWidth=0,nHeight=0;
try{
	if("number"==typeof(window.innerWidth)){nWidth=window.innerWidth;nHeight=window.innerHeight;}
	else
	{
	if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
	nWidth=document.documentElement.clientWidth;
	nHeight=document.documentElement.clientHeight;}
	else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
	nWidth=document.body.clientWidth;
	nHeight=document.body.clientHeight;}}
}
catch(err){}
return{width:nWidth,height:nHeight};}

function FullPlay()
{
	newWidth = GetWindowClientSize().width;
	newHeight = GetWindowClientSize().height;
	var nZoomX=(newWidth-2)/gm_data.width;
	var nZoomY=(newHeight-31)/gm_data.height;
	newWidth=Math.min(nZoomX,nZoomY)*gm_data.width;
	newHeight=Math.min(nZoomX,nZoomY)*gm_data.height;
	window.scrollTo(0,0);
	$("#closeFullTit").css("display","block");
	$("#game_box").css({"position":"absolute","top":"0px","width":GetWindowClientSize().width+"px","height":"100%","background":"#FFF"});
	$("#flashgame").css({"width":newWidth+"px","height":newHeight+"px"});
	$("#em").css({"width":newWidth+"px","height":newHeight+"px"});
	document.getElementsByTagName('body')[0].style.overflow='hidden';
	document.getElementsByTagName('body')[0].scroll="no";
}

function CloseFullPlay()
{
	$("#flashgame").css({"width":gm_data.width,"height":gm_data.height});
	$("#em").css({"width":gm_data.width,"height":gm_data.height});
	$("#game_box").css({"position":"static","width":"100%","height":"100%"});
	$("#closeFullTit").css("display","none");
	window.scrollTo(0,300);
	document.getElementsByTagName('body')[0].style.overflow='auto';
	document.getElementsByTagName('body')[0].scroll="";
}

function ListAllRemark()
{
if (typeof(isListAllRemark)=='undefined') {isListAllRemark=true;}
if (isListAllRemark)
{
	$("#remark").css({"height":"100%"});isListAllRemark=false;
	$("#btn_ListAllRemark").html("收缩");
}else
{
	$("#remark").css({"height":"150px"});isListAllRemark=true;
	$("#btn_ListAllRemark").html('显示完整攻略');
}
}