﻿var loginFlag = false;
var _serverId = 0;

AsyncScript = function(){

	var userAgent = navigator.userAgent.toLowerCase();
	Browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
		isSafari: /webkit/.test( userAgent ),
		isOpera: /opera/.test( userAgent ),
		isMsie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		isMozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	}

	var _node = function(nodeType,attributes,win){
		var w = win || window, d=w.document, n=d.createElement(nodeType);
		for (var i in attributes) {
			if (attributes[i]) {
				n.setAttribute(i, attributes[i]);
			}
		}
		return n;
	};

	var _scriptNode = function(url,win,charset){
		var c = charset || "UTF-8";
		return _node("script",{
		"id":"alicn",
		"type":"text/javascript",
		"charset": c,
		"src":url
		},win);
	};

	return {
		script:function(url,fn,scope){
			var w=scope||window, d=w.document, h=d.getElementsByTagName("head")[0], n;
			n = _scriptNode(url,w,"UTF-8");
			
			if(Browser.isMsie){
					n.onreadystatechange = function(){
						var rs = this.readyState;
						if("loaded" === rs || "complete" === rs){
							h.appendChild(n);
							fn();
						}
					}
				}else{
					h.appendChild(n);
					n.onload = function(){
					fn();
				}
			}
		}
	}
}();

var gameAPILogin = function(){
    var _ajax = function(url,callbackname){
        var d = new Date();
		url += "?r="+Math.random()+"&d="+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds()+"&jsonp="+callbackname+"&serverid="+_serverId;
		AsyncScript.script(url,function(){},null);
    };
    
    return {
        login : function(url,callbackname){
	        _ajax(url,callbackname);
			return false;
        }
    }
}();


/*
*WWII login
*/
var WWII_fcallback = function(sjson){
	var json = eval(sjson);
    if(json.AuthKey == null){
			window.location.href = "http://passport.bitcomet.com/client/login/?l=en-us&continue="+encodeURIComponent("http://ww2.playcomet.com/");
			return false;
	}

	var url = json.Url;
	if(url.indexOf(',') > -1){
		url = url.split(',')[_serverId];
	}

	document.WWII_form.auth.value = json.AuthKey;
	document.WWII_form.verify.value = json.Verify;
	document.WWII_form.action = url;
	document.WWII_form.password.value = "";
	document.WWII_form.fuid.value = json.FUID;
	document.WWII_form.username.value = json.UserName;
	//document.WWII_form.target="WWII_win_" + _serverId; 
	
	document.WWII_form.submit();
}

function WWII_doLogin(svrid){
	_serverId = svrid;
    return gameAPILogin.login("http://login.ww2.playcomet.com/login/login.ashx","WWII_fcallback");
}

/*
*Warrior login
*/
var Warrior_fcallback = function(sjson){
	var json = eval(sjson);
    if(json.AuthKey == null){
			return;
			window.location.href = "http://passport.bitcomet.com/client/login/?l=en-us&continue="+encodeURIComponent("http://wl.playcomet.com/");
			return false;
	}

	var url = json.Url;
	if(url.indexOf(',') > -1){
		url = url.split(',')[_serverId];
	}

	document.Warrior_form.auth.value = json.AuthKey;
	document.Warrior_form.verify.value = json.Verify;
	document.Warrior_form.action = url;
	document.Warrior_form.password.value = "";
	document.Warrior_form.fuid.value = json.FUID;
	document.Warrior_form.username.value = json.UserName;
	//document.Warrior_form.target="Warrior_win"; 
	
	document.Warrior_form.submit();
}

function Warrior_doLogin(svrid){
	_serverId = svrid;
    return gameAPILogin.login("http://login.wl.playcomet.com/login/login.ashx","Warrior_fcallback");
}

/*
*login info
*/
function setUserInfo(id,name){	
	if(document.getElementById("loginUL")){
		$('#loginUL').css("display",'none');

		var s = $('#logoutUL').html();
		s = s.replace('%7B','{').replace('%7D','}');
		$('#logoutUL').html(s.replace('{$userencode}',name).replace('{$user}',decodeURIComponent(name)))
		.css("display",'');
		
		ShowGameLoginForm();
	}
}

function showLogin(){
	if(document.getElementById("loginUL"))
		document.getElementById("loginUL").style.display = '';
}

function goToReg(){
	var url = encodeURIComponent(document.URL);
	var regurl = 'http://passport.bitcomet.com/client/register/?l=en-us&continue='+url;
	$("#regA1").attr("href",regurl);
	$("#regA2").attr("href",regurl);

	$("a").each(function(){
		var h = $(this).attr('href');
		if(typeof h != "undefined"){
			if(h.indexOf('continue=') != -1){
				$(this).attr('href',h.substr(0,h.indexOf('continue=')) + "continue=" + url);
			}
		}
	});
}

function ShowGameLoginForm(){
	$('#WWII_loginBtnSpan').css("display","none");
	$('#WWII_loginFormSpan').css("display","");

	$('#Warrior_loginBtnSpan').css("display","none");
	$('#Warrior_loginFormSpan').css("display","");

	$('#TF_loginBtnSpan').css("display","none");
	$('#TF_loginFormSpan').css("display","");
}

var switchList = [];
function showSwitchService(o){
	document.getElementById(o).style.display="";
	SwitchService(o, "none");
	
	if(document.getElementById("overlay"))
	document.getElementById("overlay").style.visibility="visible";
}

function hideSwitchService(o){
	document.getElementById(o).style.display="none";
	if(document.getElementById("overlay"))
	document.getElementById("overlay").style.visibility="hidden";
}
function SwitchService(o, displayType){
	var isIn = 0;
	if(switchList.length > 0){
		for(var i = 0; i < switchList.length; i++){
			if(o == switchList[i]){
				isIn = 1;
				continue;
			}
			document.getElementById(switchList[i]).style.display=displayType;
		}

		if(!isIn) switchList[switchList.length] = o;
	}else{
		switchList[0] = o;
	}
}

function txj(gameid){
	var parms = document.location.search;
	var pos = parms.indexOf("fuid=");
	if(pos > -1){
		parms = parms.substr(pos + 5, parms.length);
		pos = parms.indexOf("&");
		if(pos > -1){
			parms = parms.substr(0, pos);
		}
		AsyncScript.script("http://stat.playcomet.com/AdClick?gameid=" + gameid + "&fuid=" + parms, function(){}, this);
	}
}

$(function(){
	goToReg();
	txj(0);
});