// include the API
document.write("<div id=\"fb-root\"></div>");
/*document.write("<script src=\"http://connect.facebook.net/en_US/all.js\"></script>");*/
// initialize
FB.init({appId: '112209952124688', status: true, cookie: true, xfbml: true, oauth: true });

function FBLogin( warn ){
	//if( !perms ) perms = {perms:'email,publish_stream'};
	//perms = {perms:'email,publish_stream'};
	if( warn ) if( !confirm( "WARNING: This will create a new account if you have not previously linked your Facebook account with Prisonblock or registered using Facebook Connect" ) ) return;
	//FB.login( handleAuth, {perms:'email,publish_stream'} );
	FB.Event.subscribe( 'auth.sessionChange', handleAuth );
	FB.login( handleAuth, {'scope':'email'} );
}

function FBPublish(){
	var txt = 'Reading Connect JS documentation';
	FB.api('/me/feed', 'post', { 'body': "Oh hai thar!" }, function(response) {
	  if (!response || response.error) {
		alert('Error occured');
	  } else {
		alert('Post ID: ' + response);
	  }
	});
}

function handleAuth( response ){
	//alert( "WTF" );
	if( response.authResponse ){
		// logged in
		var form = document.createElement('form');
		form.action = window.location.href;
		form.method = 'POST';
		var cmd = document.createElement('input');
		cmd.type = 'hidden';
		cmd.name = 'cmd'; cmd.value = 'fbconnect';
		form.appendChild(cmd);
		$('fb-root').appendChild( form );
		form.submit();
	}else{
		// logged out
		window.location = '/logout';
	}
}

