﻿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 serverId = 0;
    var _ajax = function(url,callbackname){
        var d = new Date();
		url += "?r="+Math.random()+"&d="+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds()+"&jsonp="+callbackname;
		AsyncScript.script(url,function(){},null);
    };
    
    return {
        login : function(url,callbackname){
	        _ajax(url,callbackname);
			return false;
        }
    }
}();

/*
*tf login
*/
var fcallback = function(json){
    if(json.PID == null || json.PID == 0){
			window.location.href = "http://tf.playcomet.com";
			return false;
	}

	var url = json.Url;
	if(url.indexOf(',') > -1){
		url = url.split(',')[gameAPILogin.serverId];
	}

	document.getElementById("uid").value = json.UserID;
	document.getElementById("uname").value = json.UserName;
	document.getElementById("email").value = json.Email;
	document.getElementById("Ulgtime").value = json.Ulgtime;
	document.getElementById("Pid").value = json.PID;
	document.getElementById("Sign").value = json.Sign;
	document.getElementById("fuid").value = getFuid();
	document.getElementById("form1").action = url;
	
	document.getElementById("form1").submit();
}

function doLogin(srvId){
	gameAPILogin.serverId = srvId;
	return gameAPILogin.login("http://login.tf.playcomet.com/logingame.ashx","fcallback");
}

var _userId;
var _userName;
function setUserInfo(id,name){	
	_userId = id;
	_userName = name;
	if(document.getElementById("wl")){
		var s = $('#wl2').html();
		s = s.replace('%7B','{').replace('%7D','}');
		$('#wl2').html(s.replace('{$userencode}',name).replace('{$user}',decodeURIComponent(name)));
		$('#wl').css("display",'none');
		$('#wl2').css("display",'');
	}
}

function showLogin(){
	if(document.getElementById("wl")){
		document.getElementById("wl").style.display = '';
		document.getElementById("wl2").style.display = 'none';
	}
}

function form_onsubmit(){
	var name = document.formx.username;
	var pass = document.formx.password;
	if(name.value=='' || pass.value == ''){
		alert('Please fill in username and password!');
		return false;
	}

	return true;
}

var _fuid = "";
var _regUrl = "";
function RegBtn_onClick(){
	if(_regUrl != ""){
		window.location.href=_regUrl;
	}
}

function goToReg(){
	_regUrl = 'http://passport.txwy.com/client/register/?l=en-us&continue='+encodeURIComponent(document.URL);
	if(_fuid != ""){
		_regUrl += "&fuid=" + encodeURIComponent(_fuid);
	}
	//$("#regA2").attr("href",_regUrl);
	//$("#regA1").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=" + document.URL);
			}
		}
	});

	if(document.formx)
		document.formx["continue"].value = document.URL;
}

function getFuid(){
	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);
		}
		return parms;
	}
	return "";
}

$(function(){
	var args = document.location.search;
	var pos1 = args.indexOf("fuid=");
	if(pos1 != -1){
		var temp = args.substr(pos1);
		var pos2 = temp.indexOf("&");
		if(pos2 != -1){
			temp = temp.substr(0,pos2);
		}
		_fuid = decodeURIComponent(temp.substr(temp.indexOf("fuid=") + 5));
	}

	goToReg();
});

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 intoGame(){
	location.href='http://s1.tf.playcomet.com';
	//stat
	var url = "http://login.tf.playcomet.com/loginstat.ashx?";
	var data = "userid="+_userId+"&username="+encodeURIComponent(_userName)+"&jsoncallback=?";
	
	$.getJSON(url+data)
}