var cookieValue = $.cookie("NxPassport");
var objUpdateInterval = null;
function onUpdateResponse(resultObject, responseXML)
{
	if (resultObject.ErrorCode == undefined)	// do Logout if the session doesn’t exist
	{
		NgbLogin.Logout();
	}
	else if (resultObject.ErrorCode == CommonError.NoError)		// OK
	{
		window.clearInterval(objUpdateInterval);
// After ExpireLimit(Second), do UpdateSession
		objUpdateInterval = setInterval("doUpdateSession()", resultObject.UpdateInterval * 1000);	
}
	else if (resultObject.ErrorCode == AuthSystemError.Disconnected)
	{
// Replace the following line to a custom code
		alert("Disconnected by other session.");
		NgbLogin.Logout();
	}
	else
	{
// Replace the following line to a custom code
		alert("Disconnected.");
		NgbLogin.Logout();
	}
}
function doUpdateSession()
{
	AuthSystem.UpdateSession(onUpdateResponse);
}
function startUpdateSession(b)
{
	if(b)
		setTimeout("doUpdateSession()", 1);
}
function signupBoxLoad(){
	$("#toggleLogin").click(function(){
		$(".LoginBox").load("../Modules/Passport.aspx .loginAjax", loginBoxLoad);
	});
}
function loginBoxLoad(){
	var el = $("#content input").not($(".login_window input").not('[disabled=true]'));
	el.disabled = true;
	$('.input_username').attr('value','');//ensure username resets to blank upon refresh of page
	$('.login_window input').focus(function(){
		$(this).css('color', '#333');//change the color of the text as the user is typing their info
		if ($(this).hasClass('input_username'))
			$(this).css('background-position','100% 0')//clear the ID field when user clicks
		if ($(this).hasClass('input_password'))
			$(this).css('background-position','100% 0')//show the password field for user input
	});  
	$('.checkbox_remember_me img').toggle(function(){
		$(this).attr('src','http://nxcache.nexon.net/nx/global/login_box/remember_checkbox_checked.gif')},//change the checkbox image once clicked,
	function(){
		$(this).attr('src','http://nxcache.nexon.net/nx/global/login_box/remember_checkbox_unchecked.gif')//change back to other image if clicked again
	});
	if (cookieValue != null) {
		$("input[id*=tbID]").val(cookieValue).css({color: '#333',backgroundPosition: '100% 0'});
		$('.checkbox_remember_me img').click();
	}
	$(".button_login").click(function(){
		var alertStr = ($("input[id*=tbPass]").val() == "" ? "Please enter your Password" : 
						($("input[id*=tbID]").val() == "ID" || $("input[id*=tbID]").val() == "" ? "Please input your ID" : ""));		
		if (alertStr != "") {
			alert(alertStr);
			return false;
		}
		else {
			var postParams = $.grep($("form").serializeArray(), function(value) {return (value.name != "__VIEWSTATE" && value.name != "__EVENTTARGET" && value.name != "__EVENTARGUMENT" );});
			$.ajax({
				url: "../Modules/Passport.aspx",
				type: "POST",
				data: postParams,
				datatype: "html",
				success: function(data){
					var alertData = data.split("<script language=\"javascript\">");
					if (alertData[1] != null) {
						alertData = alertData[1].split("</script>");
						eval(alertData[0]);
					}
					else {
						if ($(".checkbox_remember_me img[src*=unchecked]").size() < 1)
							$.cookie("NxPassport",postParams[0].value.toString(),{expires: 365,path: "/",domain:"nexon.net"});
						else
							$.cookie("NxPassport", null, {path: "/",domain:"nexon.net"});
						window.location.reload();
					}
				},
				error: function(xhr){
					alert("I'm sorry. There was an error. Please try again later. "+ xhr.status + " " + xhr.statusText);
				}
			});
			return false;
		}
	});
}
$(function(){
	var ts = new Date().getTime();
	if ($("#WelcomeMessage").size() > 0 && $("#WelcomeMessage span").html() != "Welcome, ") {
		startUpdateSession(isSSO);
		$(".LoginBox:visible").load("../Modules/Passport.aspx .logoutAjax", {time: ts});
	}
	else {
		if (cookieValue != null) 
			$(".LoginBox:visible").load("../Modules/Passport.aspx .loginAjax", {time: ts}, loginBoxLoad);
		else 
			$(".LoginBox:visible").load("../Modules/Passport.aspx .signupAjax", {time: ts}, signupBoxLoad);
	}
});