//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashcontent";

var sAppURL = "";
var sAppKey = "";

var api;
var friendResult;
var userResult;
var usersResult;
var oFlash = null;
var isFlashReady = false;
var isLoggedIn = false;
 
//Initialize Facebook
function fbInit(pAsSwfName,pApi_key,pReceiver){
  	as_swf_name = pAsSwfName;
  	FB.init(pApi_key,pReceiver);
}
 
//JavaScript Connect methods
function login(){
  FB.Connect.requireSession( onLoginHandler );
}

function getUserInfo(userId, arrProfileData){
	api.users_getInfo([userId > 0 ? userId : api._session.uid], arrProfileData, function(result, ex)
	{
		userResult = result[0];
		flashCallBack("onUserInfo", userResult);
	});
}

function getUsersInfo(arrUsers, arrProfileData){
	api.users_getInfo(arrUsers, arrProfileData, function(result, ex)
	{
		usersResult = result;
		flashCallBack("onUsersInfo", usersResult);
	});
}

function getFriends(){
	api.friends_get(null, function(result, ex)
	{
		friendResult = result;
		flashCallBack("onFriendsList", friendResult);
	});
}

function shareLink(link)
{
	FB.Connect.showShareDialog(link);
}

function publish_tofriend_wall (targetId, pop_header, name, description, text, image_link, swf, url)
{
	var attachment = {'name':name, 'href':url, 'description':description, 'media':[{'type':'flash', 'swfsrc':swf, 'imgsrc':image_link, 'width':'90', 'height':'90', 'expanded_width':'465', 'expanded_height':'236'}]};
	var actionLinks = [{ "text": text, "href":url}];
	FB.Connect.streamPublish('', attachment, actionLinks, targetId, pop_header);
	
}

function publish_user_story (pop_header, name, description, text, image_link, swf, url)
{
	var attachment = {'name':name, 'href':url, 'description':description, 'media':[{'type':'flash', 'swfsrc':swf, 'imgsrc':image_link, 'width':'90', 'height':'90', 'expanded_width':'465', 'expanded_height':'236'}]};
	var actionLinks = [{ "text": text, "href":url}];
	FB.Connect.streamPublish('', attachment, actionLinks, null, pop_header);
	
}


function permission_dialog ()
{

	FB.Connect.showPermissionDialog('publish_stream');
	
}

//Event Handlers
function onLoginHandler(){
	api = FB.Facebook.apiClient;
	if(!isLoggedIn)
	{
		isLoggedIn = true;
		flashCallBack( "onLogIn" , api._session);
	}
}

//Method to dispatch an Event to Flash
function flashCallBack ( func ) {
  if( arguments.length > 1 ){
    document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);
  }else{
    document[as_swf_name][func]();
  }
}
