/*
 * 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 ensure_init(callback) {
    if(window.is_initialized) {
        callback();
    } else {
        FB_RequireFeatures(["XFBML"], function() {
        //FB.FBDebug.logLevel = 4;
        //FB.FBDebug.isEnabled = true;
        
        FB.Facebook.init(fbApiKey, '/facebook/xd_receiver');

        window.is_initialized = true;
        callback();
    });
  }
}


/*
 * "Session Ready" handler. This is called when the facebook
 * session becomes ready after the user clicks the "Facebook login" button.
 * In a more complex app, this could be used to do some in-page
 * replacements and avoid a full page refresh. For now, just
 * notify the server the user is logged in, and redirect to home.
 *
 * @param link_to_current_user  if the facebook session should be
 *                              linked to a currently logged in user, or used
 *                              to create a new account anyway
 */
function facebook_button_onclick(toAction) {
    ensure_init(function() {
        FB.Facebook.get_sessionState().waitUntilReady(function() {
            var user = FB.Facebook.apiClient.get_session() ?
                FB.Facebook.apiClient.get_session().uid :
                null;

            // probably should give some indication of failure to the user
            if (!user) {
                return;
            }
          
            session = FB.Facebook.apiClient.get_session();
            location.href = '/facebook/connect/' + user + '?session_key=' + session.session_key + '&expires=' + session.expires + '&to_action=' + toAction;
        });
    });
}

/*
 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published.
 *
 */
function facebook_publish_feed_story(form_bundle_id, template_data) {
    // Load the feed form
    ensure_init(function() {
        FB.Connect.showFeedDialog(form_bundle_id, template_data);
        //FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);

        // hide the "Loading feed story ..." div
        $('feed_loading').style.visibility = "hidden";
    });
}

function facebook_button_onclick_import_events(toAction) { 
    ensure_init(function() {
        FB.Facebook.get_sessionState().waitUntilReady(function() {
            var user = FB.Facebook.apiClient.get_session() ?
                FB.Facebook.apiClient.get_session().uid :
                null;

            // probably should give some indication of failure to the user
            if (!user) {
                return;
            }
            
            session = FB.Facebook.apiClient.get_session();
            location.href = '/facebook/connect/' + session.uid + '?session_key=' + session.session_key + '&expires=' + session.expires + '&to_action=' + toAction
       });
      //  FB.Connect.showPermissionDialog("publish_stream", function(perms) {
      //      session = FB.Facebook.apiClient.get_session();
      //      if (!perms) {
      //          // alert('Without permission');
      //          // continue_without_permission();
      //      } else {
      //          // alert('Permission granted');
      //          // save_session();
      //      }
      //      location.href = '/facebook/connect/' + session.uid + '?session_key=' + session.session_key + '&expires=' + session.expires + '&to_action=' + toAction + '&perms=' + perms;
      // });
   });
}
