/*
 * API key, this should be initialized before any another function in this file is called.
 */
var is_initialized = false;

/*
 * Ensure Facebook app is initialized and call callback afterward
 */
function facebook_ensure_init(callback) {
    if(!window.api_key) {
        window.alert("api_key is not set");
    }
    if(window.is_initialized) {
        if (callback) callback();
    } else {
        FB_RequireFeatures(["XFBML","CanvasUtil","Api"], function() {
            FB.FBDebug.logLevel = 4;
            FB.FBDebug.isEnabled = false;
            FB.Facebook.init(window.api_key, window.xd_receiver);
            window.is_initialized = true;
            if (callback) callback();
        });
    }
}

/*
 * The facebook_onload statement is printed out in the JSP. If the user's logged in
 * status has changed since the last page load, then refresh the page to pick up
 * the change.
 *
 * This helps enforce the concept of "single sign on", so that if a user is signed into
 * Facebook when they visit your site, they will be automatically logged in -
 * without any need to click the login button.
 *
 * @param already_logged_into_facebook  reports whether the server thinks the user
 *                                      is logged in, based on their cookies
 */
function facebook_onload(already_logged_into_facebook) {
    // user state is either: has a session, or does not.
    // if the state has changed, detect that and reload.
    facebook_ensure_init(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)
            // then do nothing
            if (is_now_logged_into_facebook == already_logged_into_facebook) {
                return;
            }

            // otherwise, refresh to pick up the state change
            refresh_page(); 
        });
    });
}

function facebook_prompt_permission(permission, callbackFunc) {
    facebook_ensure_init(function() {
        //check is user already granted for this permission or not
        FB.Facebook.apiClient.users_hasAppPermission(permission,
            function(result) {
                // prompt offline permission
                if (result == 0) {
                    // render the permission dialog
                    FB.Connect.showPermissionDialog(permission, callbackFunc);
                } else {
                    // permission already granted.
                    callbackFunc(true);
                }
            });
    });
}

function facebook_logout_button_onclick(redirectUrl) {
    facebook_ensure_init(function() {
        FB.Connect.logoutAndRedirect(redirectUrl); 
    });
    return false; 
}

function facebook_revoke_authorization(uid, callback) {
    facebook_ensure_init(function() {
        FB.Connect.requireSession(function () { 
            FB.Facebook.apiClient.revokeAuthorization(uid, function() {
                FB.Connect.logout(callback);
            }); 
        });
    });
    return false;
}

function facebook_connect_button_onclick() {
    facebook_ensure_init(function() {
        FB.Connect.requireSession(function () { refresh_page(); });
    });
    return false;
}

function refresh_page() {
    window.location.reload();
}
