﻿var x=0;
var y=0;
var w=0;
var h=0;
var xoffset=72;
var yoffset=72;
var blinkTimes=0;
var blinkTimer;
var accountName;
var accountPassword;
var blinkSpeed=500;
var ieStr="MSIE";
var downLoadFlash=false;
var serverTime=0;
var serverTimezoneOffset=0;
var clientTimezoneOffset=0;
var timezoneStr;
var isLogout=false;

var obj_flashClient ;
var isResizeGame=false;
var zoomScale = 1;

function moveIFrame(x2,y2,w2,h2) {	
	w=parseInt(w2);
	x=parseInt(x2);
	y=parseInt(y2);
	h=parseInt(h2);
	xoffset = 0;  		
	var frameRef=document.getElementById("myFrame");
	if(document.body.clientWidth>=1000){
		frameRef.style.left=x+(document.body.clientWidth-1000)/2-xoffset;
	}else{
		frameRef.style.left=x;
	}
	frameRef.style.top=y;
	var loadFrameRef=document.getElementById("loadFrame");
	loadFrameRef.style.left = frameRef.style.left;
	loadFrameRef.style.top = frameRef.style.top;
	var iFrameRef=document.getElementById("myIFrame");
	if(isResizeGame==true)
	{
		iFrameRef.width=w*zoomScale;
		iFrameRef.height=h*zoomScale;
	}else{
		iFrameRef.width=w;
		iFrameRef.height=h;
	}
	var iFrameLoad=document.getElementById("loadIFrame");
	iFrameLoad.width = iFrameRef.width;
	iFrameLoad.height = iFrameRef.height;
}

function hideIFrame(){
	document.title = blinkTitle;	
	document.getElementById("myFrame").src ="about:blank";
	document.getElementById("loadFrame").src = "about:blank";
	moveIFrame(0,0,1,1);
    document.getElementById("myFrame").style.visibility="hidden";
    document.getElementById("loadFrame").style.visibility="hidden";
}
	
function showIFrame(){
    document.getElementById("myFrame").style.visibility="visible";    
    document.getElementById("loadFrame").style.visibility="visible";
    resetIframe();
}

function loadIFrame(url){
	document.getElementById("myFrame").innerHTML = "<iframe id='myIFrame' src='" + url + "'frameborder='0' onload='myFrameLoaded();'></iframe>";
	document.getElementById("loadFrame").innerHTML = "<table id='loadIFrame' border='0' cellpadding='0' cellspacing='0' bgcolor='#333333'><tr><td valign='top' style='color:#ffffff;font-size:12px'>loading...</td></tr></table>";
}
function myFrameLoaded() {
	document.getElementById("loadFrame").src = "about:blank";
	document.getElementById("loadFrame").style.visibility="hidden";
}
function resetIframe(){
	infoStopBlink();
	centerApp();
	var frameRef=document.getElementById("myFrame");
	if(isResizeGame==true)
	{
		frameRef.style.left=(document.body.clientWidth-1000*zoomScale)/2+22*zoomScale;
		frameRef.style.top=190*zoomScale;
	}else{
		if(document.body.clientWidth>=1000){
			frameRef.style.left=x+(document.body.clientWidth-1000)/2-xoffset;
		}else{
			frameRef.style.left=x;
		}
		frameRef.style.top=y;	
	}
	var loadFrameRef=document.getElementById("loadFrame");
	loadFrameRef.style.left = frameRef.style.left;
	loadFrameRef.style.top = frameRef.style.top;
	//重设大小
	if(w>0){
		if(isResizeGame==true)
		{
			var iFrameRef=document.getElementById("myIFrame");
			iFrameRef.width=w*zoomScale;
			iFrameRef.height=h*zoomScale;
			var iFrameLoad=document.getElementById("loadIFrame");
			iFrameLoad.width = iFrameRef.width;
			iFrameLoad.height = iFrameRef.height;
		}else{
			var iFrameRef=document.getElementById("myIFrame");
			iFrameRef.width=w;
			iFrameRef.height=h;
			var iFrameLoad=document.getElementById("loadIFrame");
			iFrameLoad.width = iFrameRef.width;
			iFrameLoad.height = iFrameRef.height;
		}
	}
}

function centerApp(){
	obj_flashClient = document.getElementById ( "WWII" );
	if(document.getElementById("webGameFlash") != null){
		var gameFlash=document.getElementById("webGameFlash");
		
		if(isResizeGame==true)
		{
			resetSize();
			if(document.body.clientWidth>obj_flashClient.width){
				gameFlash.style.left=(document.body.clientWidth-1000*zoomScale)/2;
			}else{
				gameFlash.style.left=0;
			}
		}else{
			if(document.body.clientWidth>=1000){
				gameFlash.style.left=(document.body.clientWidth-1000)/2;
			}else{
				gameFlash.style.left=0;
			}
		}

		gameFlash.style.top=0;
	}
}

//客户端获取游戏的Height
function getGameHeights(value)
{
	if(isResizeGame != value)
	{
		resetGame(value);
	}
	return obj_flashClient.height;
}
//--重置游戏大小。
function resetGame(value)
{
	isResizeGame = value;
	if(value==true)
	{
		zoomScale = document.body.clientHeight/600;
		obj_flashClient.height=600*zoomScale;
		obj_flashClient.width =1000*zoomScale;
	}else{
		obj_flashClient.width = 1000;
		obj_flashClient.height = 600;
	}
	resetIframe();
}
//--选择缩放
function resetSize () {
    if(isResizeGame==false){
	   return;
    }
    var w = document.body.clientWidth;
    var h = document.body.clientHeight;
    var rate = 6 / 10;
    var currentRate = h / w;
 
	if ( currentRate > rate ) {
        obj_flashClient.width = w;
        obj_flashClient.height = w * rate;
        checkGameSize();
    } else {
        obj_flashClient.width = h / rate;
        obj_flashClient.height = h;
        checkGameSize();
    }
}

function checkGameSize()
{
	zoomScale = obj_flashClient.height/600;
}

function initAccount(name,pwd){
	accountName=name;
	accountPassword=pwd;
}

function infoActiveBlink(reciveStrNull,reciveStrFull)
{	
	blinkStrNull=reciveStrNull;
	blinkStrFull=reciveStrFull;
	blinkTimes = 1;
} 

function syncServerTime($serverTime,$offsetTime)
{
	if (this.isLogout == false) {
		serverTime = $serverTime;
		//服务器时间和UTC时间的偏差，毫秒
		serverTimezoneOffset = $offsetTime;
		//60*60*1000 = 3600000
		hourOffset= serverTimezoneOffset/3600000;
		if(hourOffset == 0)
		{
			//当为0时，只显示GMT
			timezoneStr = " (GMT)";
		}
		else if(hourOffset > 0)
		{
			//当大于0时，需要加上“+”
			timezoneStr = " (GMT +"+hourOffset +")";
		}
		else
		{
			timezoneStr = " (GMT "+hourOffset +")";
		}

		// create Date object for current location 
		d = new Date();
		clientTimezoneOffset = d.getTimezoneOffset() * 60000;
		setInterval(runTimeStep, 1000);
	}
}

function runTimeStep() 
{
	serverTime += 1000;
	showTime(this.serverTime);
}

function showTime(time) 
{   
    fullTime = new Date(time + clientTimezoneOffset + serverTimezoneOffset);
	year = fullTime.getFullYear();
	month = (fullTime.getMonth() < 9 ? '0' : '') + (fullTime.getMonth() + 1);
	date = (fullTime.getDate() < 10 ? '0' : '') + (fullTime.getDate()); 
	hour = (fullTime.getHours() < 10 ? '0' : '') + (fullTime.getHours());
	minute = (fullTime.getMinutes() < 10 ? '0' : '') + (fullTime.getMinutes());
	second = (fullTime.getSeconds() < 10 ? '0' : '') + (fullTime.getSeconds());
	if (blinkTimes > 0) {
		blinkTitle = blinkTimes % 2 ? blinkStrNull : blinkStrFull;
		blinkTimes++;
	} else {
		blinkTitle = '';
	}
	timeStr = year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':'  + second + timezoneStr ;
	document.title = blinkTitle + title + ' ' + timeStr;
}

function setLogoutFlag(logoutFlag)
{
	this.isLogout = logoutFlag;
}

function infoStopBlink()
{
    blinkTitle="";
	blinkTimes=0;
}

function getNavigator()
{ 
   if(navigator.userAgent.indexOf("MSIE") != -1 && window.ActiveXObject) {
        return ieStr;
   }
   if(navigator.userAgent.indexOf("Firefox") != -1 && document.getBoxObjectFor){
        return "Firefox";
   }
   if(navigator.userAgent.indexOf("Safari") != -1 && window.openDatabase) {
        return "Safari";
   } 
   if(navigator.userAgent.indexOf("Camino") != -1){
        return "Camino";
   }
   if(navigator.userAgent.indexOf("Gecko/") != -1){
        return "Gecko";
   }
   if(navigator.userAgent.indexOf("Opera") != -1 && window.opera){
        return "Opera";
   }
   if(window.MessageEvent && !document.getBoxObjectFor){
	    return "Chrome";
   }
   if(navigator.userAgent.indexOf("Chrome") != -1){
        return "Chrome";
   }
   return "other";
}

function getMsie(){
	return ieStr;
}

function openPopup(url){
	var popwindow;         
	popwindow = window.open(url);         
	// detect popup blocked
	if (popwindow==null || typeof(popwindow)=="undefined"){
	        return false;
	}
	return true;
}

function GetCookieVal(offset)
//获得Cookie解码后的值
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//设定Cookie值
{
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
		+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
		+((secure == true) ? "; secure" : "");
}

function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 86400000);
var cval = GetCookie (name);
SetCookie(name,cval,exp.toGMTString(),"/","wistone.com");
}

//获得Cookie的原始值
function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

//添加收藏夹
function addToFavList(){	
	if (document.all){
		window.external.AddFavorite(siteUrl, siteTitle);
    }else if (window.sidebar){
    	window.sidebar.addPanel(siteTitle, siteUrl, '');
    }
}

//登陆到bbs。
function openBBS(username, password) {
	if (autoBBSLogin) {
		autoLoginBBS(getBBSPageUrl(), username, password);
	} else {
		openPopup(getBBSPageUrl());
	}
}

function autoLoginBBS(formAction, username, password) {
	var bodyNode = document.getElementsByTagName('body')[0];
	//创建表单
	var form = document.createElement('form');
	form.target = "_blank";
	form.style.display = 'none';
	form.setAttribute('action',formAction);
	form.setAttribute('method','post');
	bodyNode.appendChild(form);
	
	//登陆方式
	var logintypeInput = document.createElement('input');
	logintypeInput.setAttribute('type','hidden');
	logintypeInput.setAttribute('name','loginfield');
	logintypeInput.setAttribute('value','username');
	form.appendChild(logintypeInput);
	
	//用户名
	var usernameInput = document.createElement('input');
	usernameInput.setAttribute('type','hidden');
	usernameInput.setAttribute('name','username');
	usernameInput.setAttribute('value',username);
	form.appendChild(usernameInput);
	
	//密码
	var passwordInput = document.createElement('input');
	passwordInput.setAttribute('type','hidden');
	passwordInput.setAttribute('name','password');
	passwordInput.setAttribute('value',password);
	form.appendChild(passwordInput);
	
	//提交
	form.submit();
	bodyNode.removeChild(form);

}

//wa 数据采集
window.onbeforeunload = function (){
	traceGameExit(wadi,true);
}

function trackPageview(uri,dateId,defined){ 
	
	try {
		windowOnload(uri,dateId,defined);
	} catch(err) {}
}

function traceGameExit(id,mark){
	
	if(!mark){
		wadi = id;
	}

	if( mark && !isNaN(wadi) ) {
		try {
			windowOnunload(wadi);
		} catch(err) {}
	}
}

