
/*
 * The facebook_onload statement is printed out in the server. 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.
  /*
  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
          refresh_page();
        });
    });
    */
}

function facebookLogin(api_key) {
	var channel_path = 'includes/xd_receiver.htm'; 
	FB_RequireFeatures(["Api"], function(){ 
		// Create an ApiClient object, passing app's API key and 
		// a site relative URL to xd_receiver.htm 
		FB.Facebook.init(api_key, channel_path); 
		var api = FB.Facebook.apiClient; 
		// require user to login 
		window.onload = function() { $g_onloadJs };		
		api.requireLogin(function(exception){
			alert ("logged in!");
			Debug.dump("Current user id is " + api.get_session().uid); 
			// Get friends list 
			api.friends_get(null, function(result){ 
				Debug.dump(result, 'friendsResult from non-batch execution '); 
			}); 
		}); 
	}); 
}

/*
 * 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(apiKey) {
  // use this function to do AJAX calls 
  // and/or in-place replacement of page contents 
  //window.location.search = "fb_logged_in=1";
  // alert("Logged in facebook user [" + U.readCookie(apiKey + "_user") + "]");

  
	//Create an ApiClient object, passing app's api key and
	//a site relative url to xd_receiver.htm
	var api = new FB.ApiClient(apiKey, '/includes/xd_receiver.htm', null);
	
	FB.FBDebug.isEnabled = true;
	// FB.FBDebug.logLevel = 0;
	
	//Get friends list
	function getResults(result,exception) {
		FB.FBDebug.dump(api.get_session().uid,'you');
		FB.FBDebug.dump(result,'The stuff');
	}
	
	function getInfo(result,exception) {
		FB.FBDebug.dump(result,'The info');
		alert("Hi " + result[0].first_name + " " + result[0].last_name);
	}
	
	// FB.FBDebug.writeLine("Logged in facebook user [" + U.readCookie(apiKey + "_user") + "]");
	// alert("Current user id is " + api.get_session().uid);
	
	//require user to login
	/*
	api.requireLogin(function(exception) {
	  api.friends_get(null,getResults);
	  var myinfo='last_name,first_name,hometown_location,work_history,pic_small';
	  api.users_getInfo(api.get_session().uid, myinfo, getInfo);
	});
	*/
	var myinfo='last_name,first_name,hometown_location,work_history,pic_small';
  api.users_getInfo(api.get_session().uid, myinfo, getInfo);	
}

/*
 * 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 refresh_page() {
  window.location.reload();
}

/*
 * 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);
          FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);
  });
}

/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 *
 * 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() {
  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
            checkbox = ge('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}
