FB.init( jsFB_APPKEY, "/connect/xd_receiver.htm" );

function facebook_onload(already_logged_into_facebook) 
{
	// user state is either: has a session, or does not.\n
	// if the state has changed, detect that and reload.\n
	FB.ensureInit(function() {
		FB.Facebook.get_sessionState().waitUntilReady(function(session) {
			var is_now_logged_into_facebook = session ? true : false;
			// if the new state is the same as the old (i.e., nothing changed)\n
			// then do nothing\n
			if (is_now_logged_into_facebook == already_logged_into_facebook) 
			{
				return;
			}
			// otherwise, refresh to pick up the state change
			refresh_page();
		});
	});
}

function facebook_login() 
{
	// user state is either: has a session, or does not.\n
	// if the state has changed, detect that and reload.\n
	FB.ensureInit(function() {
		FB.Facebook.get_sessionState().waitUntilReady(function(session) {
			var is_now_logged_into_facebook = session ? true : false;
			// if an active fb session exists create native session also
			if (is_now_logged_into_facebook) 
			{
				// redirect to initiate session via fb init script
				window.location = '/logi_facebook/';
			}
			else
			{	// otherwise juset return
				return;
			}
		});
	});
}

function cf_logout() 
{
	var url = "/?session_invalidate=1";
	window.location = url;
}

function refresh_page() 
{
	var url = "/";
	window.location = url;
}
