jQuery(document).ready(function() {
    FB.init(fbapikey,"/public/html/xd_receiver.htm", {"ifUserConnected" : notifyFlash});
});

/**
 * trigger facebook session check
 */
function facebookLogin()
{
	// check the users status...prompt them to finish their profile if necessary
	FB.Connect.get_status().waitUntilReady(function(status) {
		if (status != FB.ConnectState.userNotLoggedIn) {
			notifyFlash();
		}
	});

    FB_RequireFeatures(["Connect", "Api"], function() {
        FB.init(fbapikey, "xd_receiver.htm");
        FB.Connect.requireSession();
    });
}

/**
 * notify flash of login
 */
function notifyFlash()
{
    var flashObject = null;
    var name = "flashSite";

    if (window.document[name]) {
        flashObject = window.document[name];
    }

    if (jQuery.browser.msie) {
        flashObject = document.embeds[name];
    } else {
        flashObject = document.getElementById(name);
    }
    var api = FB.Facebook.apiClient;

    api.requireLogin(function(exception){
        api.users_getInfo([api.get_session().uid], ['uid', 'first_name', 'last_name', 'pic_square'], function(result, exception) {
            // send response data to flash
            if (flashObject && flashObject['receiveFacebookLogin']) flashObject.receiveFacebookLogin(result[0]);
        });
    });
}