function facebook_onload(already_logged_into_facebook,FB_REDIRECT) {
   //window.alert('onload');
  // 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;

	  //window.alert(already_logged_into_facebook);

          // 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();
		var temp;
		if (FB_REDIRECT == temp)
		{
			facebook_onlogin_ready();
		}
			facebook_onlogin_ready(FB_REDIRECT);
				
        });
    });
}

/*
 * 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(FB_REDIRECT) {
	//window.alert("onlogin" + FB_REDIRECT);
	refresh_page(FB_REDIRECT);
}


/*
 * 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(FB_REDIRECT) {
      FB.ensureInit(function() {
                FB.Facebook.apiClient.users_getLoggedInUser(function(result,ex)
            {
                if (result != null)
                {
			var FIRST;
			var LAST;
			var sql = "SELECT first_name, last_name FROM user WHERE uid =" + result;
              		FB.Facebook.apiClient.fql_query(sql, function(result,ex)
               			{
                         		  for ( x in result )
                      		{
					FIRST = result[x]['first_name'];
					LAST = result[x]['last_name'];
				}
			});

			var temp;
			//window.alert(FB_REDIRECT);
			FB_SESSION=FB.Facebook.apiClient.get_session().session_key;
			
			if (FB_REDIRECT == temp){
				location.replace('/user/facebook-platform/facebook_login.amp?facebook_id=' + result + "&FB_SESSION=" + FB_SESSION + "&FIRST=" + FIRST + "&LAST=" + LAST + "&FB_REDIRECT=" + FB_REDIRECT);
			}
			else if (FB_REDIRECT == 'LOG_IN_USER'){
					
				location.replace('/user/login.amp?u=' + result + '&FB_SESSION=' + FB_SESSION + '&FBID=' + result);
			}
			else
			{
				//window.alert(FB_SESSION);
				//location.replace('/user/facebook-platform/facebook_login.amp?facebook_id=' + result + "&FB_SESSION=" + FB_SESSION + "&FIRST=" + FIRST + "&LAST=" + LAST + "&SECTION=" + SECTION);
				location.replace('/user/facebook-platform/facebook_login.amp?facebook_id=' + result + "&FB_REDIRECT=" + FB_REDIRECT + "&FB_SESSION=" + FB_SESSION+ "&FIRST=" + FIRST + "&LAST=" + LAST);
			}
			
                }
                	else
                {
                   	//window.alert('Not Logged in');
                }

            });
        });
}


/*
 * Prompts the user to grant a permission to the application.
 */

function facebook_prompt_permission(permission) 
{
  FB.ensureInit(function() 
   {

      FB.Connect.showPermissionDialog(permission,callBack);
      function callBack(perm)
      {
	var empty = '';
	if (!perm)
	{
		perm = 'none';
	}
	
	//window.alert('perm:' + perm + ' empty: ' + empty);
	location.replace('/event/add.amp?SECTION=events&granted=' + perm);
      }

  });

}

/*
 * 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);

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

/*
 * 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";
            }
          }
        });
    });
}

function postToFacebook(redirect_url,template_id,VERB,NOUN,SITE_NAME,BODY,FULL_BODY,ACTION_LINK_TEXT,ACTION_LINK,IMAGE,IMAGE_HREF,MESSAGE_PROMPT,USER_MESSAGE, FLEX_APP)
{
	var REDIRECT_URL = redirect_url;
	var TEMPLATE_ID = template_id;

	
        var comment_data = {
                "verb":VERB,
                "noun":NOUN,
                "site_name":SITE_NAME,
                "body":BODY,
                "full_body":FULL_BODY,
                "action_link_text":ACTION_LINK_TEXT,
                "action_link":ACTION_LINK,
                "images":[{'src':IMAGE,'href':IMAGE_HREF}]
                };


	var attachment = {
		'name':'I ' + VERB + " " + NOUN,
		'href':ACTION_LINK,
		'caption':'{*actor*} ' + VERB + " " + NOUN + " " + BODY,
		"media":[{'type':'image','src':IMAGE,'href':IMAGE_HREF}]
		};

	var action_links = [{'text':ACTION_LINK_TEXT,'href':ACTION_LINK}];

        FB.ensureInit(function(){
		var user_message_prompt = MESSAGE_PROMPT; 
		var user_message = {value: USER_MESSAGE}; 
		//FB.Connect.showFeedDialog(TEMPLATE_ID, comment_data, null, null, null, FB.RequireConnect.promptConnect, feedDialogCallback, user_message_prompt, user_message);
		FB.Connect.streamPublish('', attachment,action_links,null, null, feedDialogCallback);
		//location.replace(REDIRECT_URL);
        });
	
	function feedDialogCallback()
	{
		//closePopUp('DIV_MOVIE_REVIEW');
		if (REDIRECT_URL == 'none')
		{
			document.getElementById('DIV_ALL').style.display = 'none';
			document.getElementById('flex_movies_div').style.visibility = 'visible';
			if (FLEX_APP == 'Y')
			{
				flexFunction();
			}
		}
		else if (REDIRECT_URL == 'close')
		{
			//DO Nothing
		}
		else
		{
			//window.alert('relocating...' + REDIRECT_URL);

			location.href(REDIRECT_URL);
		}
		
	}
}