
/*
 * 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.api_key) {
    window.alert("api_key is not set");
  }

  if(window.is_initialized) {
   callback();
  } else {
    FB_RequireFeatures(["XFBML", "CanvasUtil"], function() {
    //FB_RequireFeatures(["XFBML"], function() {
        //FB.FBDebug.logLevel = 4;
        FB.FBDebug.isEnabled = false;
        // xd_receiver.php is a relative path here, because The Run Around
        // could be installed in a subdirectory
        // you should prefer an absolute URL (like "/xd_receiver.php") for more accuracy
        FB.Facebook.init(window.api_key, "/xd_receiver.php");

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

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.
  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('mine');
        });
    });
}

function refresh_page(redirect_url) {
	//alert('cnn is it working?');
  window.location = 'http://theforum.cnn.com/externalstuff/facebooklogin/'+redirect_url;
}

function facebook_button_onclick(redirect_url) {
  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) {
          	alert('error logging into facebook');
            return;
          }

      		 refresh_page(redirect_url);
        });
        FB.Connect.requireSession();
    });
}

function facebook_comment_post(issue_link,issue_name,comment) {
	ensure_init(function() {
  	var comment_data = {"issue_link":issue_link,"issue_name":issue_name,"comment":''};
  	var temp=FB.Connect.showFeedDialog(29399654532, comment_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);
  });
}

function facebook_badge_post(user_id) {
	ensure_init(function() {
  	var comment_data = {"badge":'badge',"user_id":user_id};
  	//var temp=FB.Connect.showFeedDialog(29399659532, comment_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect, callback());
  	var temp=FB.Connect.showFeedDialog(29399659532, comment_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);
  });
}

