// FacebookByondJavascript.xml

// Save pending post data for various callbacks to make sure it is still
// around after we've logged in.
var fb_pending_message = '';
var fb_pending_attachment = null;
var fb_pending_action = null;

// These state variables are necessary because Facebook's javascript is
// spam-happy and likes to call our callbacks multiple times.  These
// ensure we don't perform actions more than once.
var fb_logged_in_uid = 0;
var fb_logging_in = 0;
var fb_publishing = 0;


// Called when a post attempt returns.  If the post_id is non-null, it was
// successful, so delete the pending variables.
//
// Otherwise if we aren't in the middle of publish or login operations, tell
// Facebook to force the session to refresh.  This is necessary in case we
// logged out of Facebook since loading this page.  In that case, the sequence
// of events is roughly (with multiple-callback spam filtered out):
// -> initiate_fb_post()
//    -> FB.Connect.ifUserLoggedIn() [thinks we're logged in but we're not]
//       -> do_fb_post()
//          -> FB.Connect.streamPublish()
//            -> fb_post_callback() [no post_id, not logging in or publishing]
//               -> FB.Connect.forceSessionRefresh()
//                  -> Then somehow post_not_connected() gets called (sometimes
//                     twice), which leads to successful login and eventually
//                     do_fb_post() which publishes the post.  I'm not sure how
//                     this happens since it isn't specified as a callback to
//                     FB.Connect.forceSessionRefresh().  This behavior actually
//                     makes the user experience smoother - but if it goes away
//                     (is fixed by FB), no big deal; the user will just have
//                     to click the link a second time, which most people
//                     would do after no feedback.
// Calling FB.Connect.forceSessionRefresh() after user-cancel (while logged in),
// which is indistinguishable from logged-out error, doesn't hurt anything.
function fb_post_callback(post_id, exception) {
	fb_publishing -= 1;
	if (exception) {
		alert('Error posting to Facebook: ' + exception);
	}
	if (post_id && post_id != 'null') {
		fb_pending_message = '';
		fb_pending_attachment = null;
		fb_pending_action = null;
	} else {
		if (!fb_logging_in && !fb_publishing) {
			FB.Connect.forceSessionRefresh();
		}
	}
}

// Called when we are logged in and want to post.  Note that Facebook is
// spam-happy on callbacks and likes to call this multiple times, so we
// make sure we aren't already in the middle of a publish or login operation.
function do_fb_post(uid) {
	if (uid) {
		fb_logged_in_uid = uid;
		fb_logging_in = 0;
	}
	if (fb_logged_in_uid && fb_publishing == 0 && fb_logging_in == 0 && (fb_pending_message || fb_pending_attachment || fb_pending_action)) {
		fb_publishing += 1;
		FB.Connect.streamPublish(fb_pending_message, fb_pending_attachment, fb_pending_action, '', 'Add your comments', fb_post_callback);
	}
}

// Called when we attempted to post but weren't logged in.  Facebook will
// pop up the login form.
function post_not_connected() {
	if (!fb_logging_in) {
		fb_logging_in = 1;
		FB.Connect.requireSession(do_fb_post, logged_out_fb, false);
	}
}

// This is the entry point from the HTML.
function initiate_fb_post(message, attachment, action) {
	fb_pending_message = message;
	fb_pending_attachment = attachment;
	fb_pending_action = action;
	// do_fb_post() is the callback to successful login; otherwise
	// post_not_connected() is the callback.
	FB.Connect.ifUserConnected(do_fb_post, post_not_connected);
}

// This may be called at page load.  Right now, it doesn't do much.
function logged_in_fb(uid) {
	if (uid == fb_logged_in_uid) return;
	fb_logged_in_uid = uid;
	fb_logging_in = 0;

	// Currently unused...
	var user_box = document.getElementById("fb_connect");
	if (user_box) {
		// add in some XFBML. note that we set useyou=false so it doesn't display "you"
		user_box.innerHTML =
			"<span>"
			+ "<fb:profile-pic uid=loggedinuser facebook-logo=true></fb:profile-pic>"
			+ "<br>You are connected to Facebook as <fb:name uid=loggedinuser useyou=false></fb:name>"
			+ '<br><a href="" onclick="FB.Connect.logout(logged_out_fb); return false;" class="fbconnect_login_button FBConnectButton FBConnectButton_Medium"><span class="FBConnectButton_Text">Logout of Facebook</span></a>'
			+ "</span>";

	// because this is XFBML, we need to tell Facebook to re-process the document 
		FB.XFBML.Host.parseDomTree();
	}
}

function logged_out_fb() {
	fb_logged_in_uid = 0;
	fb_logging_in = 0;

	// Currently unused...
	var user_box = document.getElementById("fb_connect");
	if (user_box) {
		user_box.innerHTML =
			'You must connect to Facebook before submitting the post. '
			+ '<fb:login-button v="2" size="medium" onlogin="logged_in_fb();">Connect to Facebook</fb:login-button>'
		FB.XFBML.Host.parseDomTree();
	}
}

