/*
 * The facebook_onload statement is called if facebook connect is enabled.
 * 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, curr_url) {
  // user state is either: has a session, or does not.
  // if the state has changed, detect that and reload.
  FB.ensureInit(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
      fb_refresh_page(curr_url);
    });
  });
}

/*
 * Our <fb:login-button> specifies this function in its onlogin attribute,
 * which is triggered after the user authenticates the app in the Connect
 * dialog and the Facebook session has been set in the cookies.
 */
function facebook_onlogin_ready(curr_url) {
  fb_refresh_page(curr_url);
}

/*
 * Do a page refresh after login state changes.
 * This is the easiest but not the only way to pick up changes.
 * If you have a small amount of Facebook-specific content on a large page,
 * then you could change it in Javascript without refresh.
 */
function fb_refresh_page(curr_url) {
  if (typeof curr_url == 'undefined' ) curr_url = '/';

  window.location = curr_url;
}

/*
 * Prompts the user to grant a permission to the application.
 */
function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}

/*
 * 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
  FB.ensureInit(function() {
    FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, null, null, facebook_hide_loading_feed_story_animation);
  });
}

function facebook_hide_loading_feed_story_animation() {
  // hide the "Loading feed story ..." div
  fb_feed_publish_div = document.getElementById('fb_feed_publish_loading');

  if (fb_feed_publish_div) {
    fb_feed_publish_div.style.display = "none";
  }

  return true;
}

/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden
 *
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 */
function facebook_show_feed_checkbox(sess_user_logged_in) {
  if (typeof sess_user_logged_in == 'undefined' ) sess_user_logged_in = false;

  checkbox_div = document.getElementById('publish_fb_checkbox');
  inline_div_mod = document.getElementById('publish_fb_checkbox_inline_mod');

  FB.ensureInit(function() {
    FB.Connect.get_status().waitUntilReady(function(status) {
      if (status != FB.ConnectState.userNotLoggedIn) {
        // If the user is currently logged into Facebook, but has not
        // authorized the app, then go ahead and show them the feed dialog + upsell
        if (checkbox_div) {
          if(inline_div_mod) {
            checkbox_div.style.display = "inline";
          } else {
            checkbox_div.style.display = "block";
          }
        }
      }

      if (checkbox_div) {
        if(checkbox_div.style.display == "none" && sess_user_logged_in === true) {
          if(inline_div_mod) {
            checkbox_div.style.display = "inline";
          } else {
            checkbox_div.style.display = "block";
          }
        }
      }
    });
  });
}