function LoginModule () {
	
}

LoginModule.isLoggedIn = false;
LoginModule.username = "";
LoginModule.toonixURL = "";

LoginModule.loginRefresh = false;

LoginModule.init = function (){
	var session_token = readCookie("session_token");

	if (session_token) {
	    LoginModule.getInfo();
	} else {
	    LoginModule.attachURL();
	}	
}

LoginModule.attachURL = function (){
	jQuery.ajax({
		url: '/membership/sso.php?cmd=getattachurl&#38;no-cache=3jj4dkjaf89',
		type: 'GET',
		cache: false,
		error: function(xhr) {
			//alert('Error');
		},
		success: function(data, statusText, jqXHR) {
			img = new Image();
			img.src = data;
	        img.onerror = function(e) {
	            //alert("Image loading error");
	        }
	        
	        img.onabort = function() {
	            //alert("Image loading aborted");
	        }
	        img.onload = function() {
	            LoginModule.getInfo();
	        }
		}

	});
	
}

LoginModule.getInfo = function (init){
	jQuery.ajax({
		url: '/membership/sso.php?cmd=info&#38;no-cache=3jj4dkjaf89',
		type: 'GET',
		dataType: "xml",
		cache: false,
		error: function(xhr) {
			//alert(xhr.status);
		},
		success: function(data, statusText, jqXHR) {
			var username = jQuery(data).find('user').attr('identity');
			LoginModule.username = username;
		},
		complete: function(xhr, statusText) {
			switch(xhr.status) {
				case 401:
					LoginModule.username = '';
					LoginModule.isLoggedIn = false;
					break;
				case 200:
					LoginModule.isLoggedIn = true;
					if(init == "true") {
						initContent();
					}
					break;
			}
			LoginModule.writeLoginModule();
		}
	});
	
}

LoginModule.loginCheck = function (){
	jQuery.ajax({
		url: '/membership/sso.php?cmd=info&#38;no-cache=3jj4dkjaf89',
		type: 'GET',
		dataType: "xml",
		cache: false,
		error: function(xhr) {
			//alert(xhr.status);
		},
		success: function(data, statusText, jqXHR) {

		},
		complete: function(xhr, statusText) {
			switch(xhr.status) {
				case 401:
					isNotLoggedIn();
					break;
				case 200:
					isLoggedIn();
					break;
			}
			LoginModule.writeLoginModule();
		}
	});
	
}

LoginModule.writeLoginModule = function (){
	if(document.getElementById("cnLogInUI")){ 
		if(LoginModule.isLoggedIn) {
			document.getElementById("cnLogInUI").innerHTML = '<a href="/toonix"><div id="myAvatar" class="loginavatar"></div></a><div class="leftWrapper"><div class="left"><a href="/membership"><div class="cnclublogo"></div></a><div id="greeting" class="logintext"></div></div></div>';
		}else{
			document.getElementById("cnLogInUI").innerHTML = '<a href="/toonix"><div id="myAvatar" class="loginavatar"></div></a><div class="leftWrapper"><div class="left"><a href="/membership"><div class="cnclublogo"></div></a><div id="greeting" class="logintext"></div></div></div>';
		}
		LoginModule.getAvatar();
		LoginModule.doWriteMessage();
	}
	
}

LoginModule.getToonixThumb = function (width, height, callbackFunc){
	if(LoginModule.isLoggedIn) {
		var url = 'http://'+api_domain+'/toonix/2.0/toonixsvc.php?requestType=getToonixThumb&countryCode=' + ccode + '&debug=1&username=' + LoginModule.username + '&part=full&mode=json&jsoncallback=?';
		jQuery.getJSON(url, function(data){
			if(data.results.status == "success") {
				var data = data.results.data;
				var avatar = data.root.avatar.item;
				for(var i=0; i<avatar.length; i++) {
					var avaWidth = parseInt(avatar[i].width);
					var avaHeight = parseInt(avatar[i].height);
					if(avaWidth == width && avaHeight == height) {
						callbackFunc(avatar[i].url);
					}
				}
			}else{
				//error
			}

		});
		
	}
}

LoginModule.getAvatar = function (){
	if(LoginModule.isLoggedIn) {
		
		LoginModule.getToonixThumb(76, 74, 
			function(toonixURL){
				LoginModule.toonixURL = toonixURL;
				LoginModule.doWriteAvatar();
			}
		);
		
	}else{
		LoginModule.toonixURL = make_url('/layout/' + lang + '/loginModule/img/top_toonix_default.png', cdndomain);
		LoginModule.doWriteAvatar();
	}
}

LoginModule.doWriteAvatar = function (){
	if(document.getElementById("myAvatar")){
		// have to offset avatar for user toonix 
		if(LoginModule.username != "") {
			document.getElementById("myAvatar").innerHTML = "<img class='navatar' src='" + LoginModule.toonixURL + "' border='0'>";
		}else{
			document.getElementById("myAvatar").innerHTML = "<img class='davatar' src='" + LoginModule.toonixURL + "' border='0'>";
		}
	}
}

LoginModule.doWriteMessage = function (){
	if(document.getElementById("greeting")){ 
		if(LoginModule.username != "") {
			document.getElementById("greeting").innerHTML = "<a href=\"/membership\">" + LoginModule.username + "</a><br><a href=\"javascript:LoginModule.logout(true);\">logout</a>";
		}else{
			document.getElementById("greeting").innerHTML = "<a href=\"javascript:LoginModule.showPopup()\">login</a> <a href=\"javascript:LoginModule.goRegistration();\">sign up</a>";
		}
	}
}

LoginModule.goRegistration = function() {
	var cid = getQueryVariable('cid');
	window.location = "http://" + cndomain + "/signup?cid="+cid;
	return false;
}

LoginModule.showPopup = function(init) {
	if(popupType == "page") {
		window.location = "/membership/login.php";
	}else{
		if(!LoginModule.isLoggedIn) {
		
			if(init){
				LoginModule.loginRefresh = true;
			}
			jQuery("#ajax_loading").hide();
			document.getElementById("init").value = init;
			//document.getElementById("overlay").style.display='block';
			//document.getElementById("loginPopup").style.display='block';
			document.getElementById("loginPopupContainer").style.display='block';
			if(document.getElementById("game")) {
				document.getElementById("game").style.display='none';
			}
		}
	}
}

LoginModule.hidePopup = function() {

	//document.getElementById("overlay").style.display='none';
	//document.getElementById("loginPopup").style.display='none';
	document.getElementById("loginPopupContainer").style.display='none';
	
	if(document.getElementById("game")) {
		document.getElementById("game").style.display='block';
	}

}

LoginModule.logout = function(refresh) {
	
	if (location.href.indexOf("signup") != -1){
		window.location = '/membership/logged_out.php';
	}else{
		jQuery.ajax({
			url: '/membership/sso.php?cmd=logout&#38;no-cache=3jj4dkjaf89',
			type: 'GET',
			cache: false,
			error: function(xhr) {
				//alert(xhr.status);
			},
			success: function(data, statusText, jqXHR) {
				LoginModule.hidePopup();
				LoginModule.getInfo(jQuery("#init").val());
			},
			complete: function(xhr, statusText) {
				if(refresh == true) {
					window.location.reload();
				}
			}
		});
	}
	
}

LoginModule.cnloginbox_login = function() {
	
	if (jQuery("#username").val() == '') {
		jQuery("#result").text("Please enter your username");
		jQuery("#result").show();
	} else if (jQuery("#password").val() == '') {
		jQuery("#result").text("Please enter your password");
		jQuery("#result").show();

	} else {

		jQuery('#ajax_loading').show();  
		jQuery('#submit').hide();  
		jQuery("#result").hide();

		jQuery.ajax({
			url: '/membership/sso.php?cmd=login&#38;no-cache=3jj4dkjaf89',
			type: 'POST',
			cache: false,
			data: {username: jQuery("#username")[0].value, password:jQuery("#password")[0].value},
			error: function(xhr) {
				//alert(xhr.status);
			},
			success: function(data, statusText, jqXHR) {
				LoginModule.hidePopup();
				LoginModule.getInfo(jQuery("#init").val());
				
				if(LoginModule.loginRefresh){
					window.location.reload();				
				}
				
			},
			complete: function(xhr, statusText) {
				if(xhr.status == 401){
					jQuery("#result").show();
					jQuery("#result").text("Incorrect password. Please try again.")
					jQuery('#ajax_loading').hide();  
					jQuery('#submit').show();  
				}
			}
		});
	}

}

LoginModule.init();

