function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function evalJSON(src) { 
	if (typeof(JSON) == 'object' && JSON.parse) return JSON.parse(src);
	return eval("(" + src + ")");
}

function getUserProfile() {
	var userProfile = new Object();
	userProfile.username = "";
	userProfile.rating = "1500";
	userProfile.isPremium = false;
	userProfile.defaultBadgeID = "";
	userProfile.sessionID = "";
	var cookie = getCookie("ccup");
	if (cookie.length > 0) {
		userProfile.username = cookie;
		try {
			var json = evalJSON(cookie);
			if (json.username != undefined) userProfile.username = json.username;
			if (json.rating != undefined) userProfile.rating = json.rating;
			if (json.isPremium != undefined) userProfile.isPremium = json.isPremium;
			if (json.defaultBadgeID != undefined) userProfile.defaultBadgeID = json.defaultBadgeID;
			if (json.sessionID != undefined) userProfile.sessionID = json.sessionID;
		} catch (e) {}
	}
	return userProfile;
}

function isUserSessionValid() {
	var userProfile = getUserProfile();
	var username = userProfile.username;
	var sessionID = userProfile.sessionID;	
	var jSessionID = getCookie("JSESSIONID");
	
	if(username.length == 0) return false;
	
	if (sessionID.length == 0) return true;
	
	if (jSessionID == null || jSessionID.length == 0) {
		return false;
	}
	
	if (jSessionID == sessionID) {
		return true;
	}
	return false;
}

function showsloganandPlaynow(_url) {
	if(isUserSessionValid()){
		document.write('<a href="/play"><div class="slogan"></div> </a>	');
		document.write('<div id="btnPlaynow" ><a href="/play" class="button" ></a></div>');
	} else {
		document.write('<a><div class="slogan" id="slogan"></div></a> 	');
		document.write('<div id="btnPlaynow" ><a class="button" id="playnow_open_login"></a></div>');
	}
}

function showAvatarPanel(_url) {
	var userProfile = getUserProfile();
	var username = userProfile.username; 
	if (isUserSessionValid()) {
		document.write('<div id="loginBlock" class="loggedIn">');
		document.write('<div id="avatar"><img width="33" alt="" src="http://avatar.chesscube.com/' + username.toLowerCase() + '_small.png" onerror="this.onerror=null;this.src=\'/blank.gif\'" /></div>');
		document.write('<div class="left">');
		var rating = "(" + userProfile.rating + ")";
		if (userProfile.isPremium) {
			rating = rating + ' <img id="premiumBadge" src="/wp-content/themes/chesschat/assets/images/premium.png" width="16" height="16" />';
		}
		document.write('<p>Hi, <span class="name">' + username + ' <span id="userrating" class="rating">' + rating + '</span></span></p>');
		document.write('<a href="javascript:cc_doLogout(\'' + _url + '\');" id="logoutBtn">+ Logout</a>');
		document.write('</div>');
		document.write('</div>');
	} 
}

function cc_doLogout(_url) {
	if (typeof(FB) == "undefined" || FB.Connect == undefined || FB.Connect.get_loggedInUser() == null) {
		window.location.href=_url;
	} else {
		FB.Connect.logoutAndRedirect(_url);
	}
}

var global_loggedIn = false;
$(document).ready(function() {
	global_loggedIn = false;
	if(isUserSessionValid()){
		global_loggedIn = true;
	}	
});
