

/*
* 
* Register for session change events, echo those events to the SWF.
* 
*/
FB.Event.subscribe('auth.sessionChange', handleSessionEvent);
//FB.Event.subscribe('auth.statusChange', handleSessionEvent);


/*
*
* Wrapper function, called by ExternalInterface within SWF.
* 
* Note that the second argument to FB.getLoginStatus(), if true, forces a live server call.  This is the only way to get 
*   realtime data more than once per session, which is kind of a big deal.
* 
*/
function getFBLoginStatus()
{
	//alert('<JS> FB.getFBLoginStatus()');
	FB.getLoginStatus(handleSessionEvent, true);
}



/*
*
* Wrapper function, called by ExternalInterface within SWF.
* 
* The Facebook.api() call within the AS API appears to be as faulty as the login flow.
* This is just being broken out in the same way.
* 
*/
function getAPIMe()
{
	//alert('<JS> FB.api()');
	FB.api('/me/', onAPIMe);
}
function onAPIMe(response)
{
	var a = document.getElementsByName("flashContent");
	var i = 0;
	for(i=0; i<a.length; i++) {
		//alert('<JS> JS.onAPIMe()');
		a[i].onAPIMe(response);
	}
}


/*
*
* Wrapper function, called by ExternalInterface within SWF.
* 
*/
function postToWall(o)
{
	//alert('<JS> FB.post()');
	FB.api('/me/feed', 'post', o, onPostComplete);
}

function onPostComplete(response)
{
	var a = document.getElementsByName("flashContent");
	var i = 0;
	for(i=0; i<a.length; i++) {
		//alert('<JS> JS.onPostComplete()');
		a[i].onPostComplete(response);
	}
}


/*
*
* Any response data should be sent to the SWF.  Note that the target SWF must be contained in a div whose ID is 'flashContent'
* 
* Also note that 'handleSessionEvent' is the externally visible name of a callback function registered with the host page from
*   within the SWF.  This is not necessarily the function's name.  You must register some function as 'handleSessionEvent' for
*   this to work.
* 
*/
function handleSessionEvent(response)
{
	var a = document.getElementsByName("flashContent");
	var i = 0;
	for(i=0; i<a.length; i++) {
		//alert('<JS> JS.handleSessionEvent()');
		a[i].handleSessionEvent(response);
	}
	//flashContent.handleSessionEvent(response);
}



/*
* 
* Utility function allowing the SWF to get the session object synchronously.
* 
* Generally, use getFBLoginStatus() instead.
* 
*/
function getFBSession()
{
	var session = FB.getSession();
	return session;
}
