
/*
 * The facebook_onload statement is printed out in the PHP. 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, redirect) 
{ 
	// 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(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(redirect) 
{	
	if (redirect == false) {
		window.location = location.href;
	} else {
		window.location = redirect;
	}
}

/*
 * 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);

          // hide the "Loading feed story ..." div
          //ge('feedloading').style.visibility = "hidden";
	});
}

/*
 * Show the user the publish form.
 *
 */
function streamPublish (msg, attachment, actionLink) 
 {
 	 FB.ensureInit(function () {
 	    FB.Connect.streamPublish('', attachment, actionLink);
 	 });
 }
 
function facebook_prompt(cookieName, domainName)
{
	FB.ensureInit(function() {
		FB.Connect.get_status().waitUntilReady(function(status) {
			if (status == FB.ConnectState.appNotAuthorized) {
				// set the cookie so that we don't annoy the user
				YAHOO.util.Cookie.set(cookieName, 1, {
			        path: "/", 
			        domain: domainName   
				});
				FB.Connect.requireSession(); return false;
			}
        });
    });		
}

/* YAHOO.namespace("trinibeatFConnectCheck");
YAHOO.trinibeatFConnectCheck.init = function(){*/
function fConnectCheck ()
{
	var currentUrl = location.href.replace(/#/g, "");
	
	FB.ensureInit(function() {
		FB.Facebook.get_sessionState().waitUntilReady(function(state) {
			var S = '{"uid": "' + state.uid + '", "expires": "' + state.expires + '", "session_key": "' + state.session_key + '", "current_url": "' + currentUrl + '"}';
			YAHOO.util.Connect.asyncRequest("GET", "/facebook/connect/?session=" + escape(S),
	                {
	                    success: function (o) {
							/*alert(o.Tid);
							alert(o.status);
							alert(o.statusText);
							alert(o.getAllResponseHeaders);
							alert(o.responseText);
							alert(o.responseText);*/
	                    	var currentLocation = location.href.replace(/#/g, "");
							refresh_page(currentLocation);
	                    },
	                    failure: function (o) {
	            			window.location = "http://"+window.location.hostname+"/registration/";
	                    }
	        });
		});
		FB.Connect.requireSession();
	});
	
}
/* var ids = ["flogin", "flogin2", "flogin3"];
YAHOO.util.Event.addListener(ids, 'click', function(){
	YAHOO.trinibeatFConnectCheck.init();
});*/




