/** 
 * Get a reference to the swf object
 */
function getSWF() {
    if (navigator.appName.indexOf("Microsoft") != -1) { return window['applicationFlash']; }
    else { return document['mozillaFlash']; }
}

/** 
 * Initialize the facebook application
 * Called from swf on load
 */
function initFacebook() {

    if (in_fb_iframe) {
        // App is loaded from facebook iframe canvass.  
        // Do nothing, session vars were already passed to the swf on load
        // and we don't need the javascript api for any other actions in
        // this app
    } else {    
        // App is initialized from a non-facebook url.  
        FB_RequireFeatures(["Api", "Connect"], function() {
            FB.Facebook.init(api_key, xd_receiver, 
                            {   "ifUserConnected": onConnectStatus, 
                                "ifUserNotConnected": onConnectStatus 
                            });   
        });
    }
}

/**
 * Get the facebook session data from the SessionRecord, formatted like fb:swf
 * See: http://wiki.developers.facebook.com/index.php/JS_API_T_FB.SessionRecord
 * See: http://wiki.developers.facebook.com/index.php/Fb:swf
 */
function getSessionInfo() {
    var sessionData = FB.Facebook.apiClient.get_session();
    var fbdata = {};
    if (sessionData) {
        fbdata.fb_sig_session_key = sessionData.session_key;
        fbdata.fb_sig_ss = sessionData.secret;
        fbdata.fb_sig_user = sessionData.uid;
        fbdata.fb_sig = sessionData.sig;
        fbdata.fb_sig_api_key = api_key;
    } 
    return fbdata;
}

/** 
 * Prompt the user to login and/or authenticate the application.
 *
 * If loaded in a facebook iframe canvas page, the user is redirected
 * to the login page and then returned to the app page.
 *
 * If loaded from a facebook connect site, a popup window appears
 * and the swf updates itself after login.
 * 
 * Called from the swf.
 */
function promptConnect() {
    
    if (!in_fb_iframe) {
        // Call to FB.Connect.requireSession opens a popup window for 
        // user to allow the app.  Pass null as the callback function
        // (connect status will be updated in 'onConnectStatus') and
        // pass true for isUserActionHint to make it skip the 
        // javascript dialog and go straight to the popup window
        FB.ensureInit(function() {
            FB.Connect.requireSession(null, true);  
        });
    } else {
         // FB.Connect.requireSession doesn't work in an iframe canvas,
         // redirect to the login page instead
         self.parent.location =  'http://www.facebook.com/login.php?api_key=' + api_key
                                 + '&extern=1&fbconnect=1&v=1.0'
                                 + '&next=' + canvas_page
                                 + '&cancel_url=' + canvas_page;                     
    } 
}        

/** 
 * Facebook connect status has changed, let the swf know
 */
function onConnectStatus() {
    var swf = getSWF();
    if (swf) { swf.updateConnection(getSessionInfo()); }
}

/**
* Facebook permission pop-up
*/
function promptPermission() {
	FB.Connect.showPermissionDialog("publish_stream");
}