/*
 * 	FBObject.js written by dreammonkey for VERSO
 *	To be used with AS2 Facebook classes v1.0
 *
 */

var my_swf;
var my_key;

function FBinit($api_key, $receiver, $swf_name) 
{
	/*
	 * Method to initiate the FB object 
	 * Call this method by creating a new Facebook() 
	 * and call facebook.init() in as2;
	 */
	
	my_swf = $swf_name;
	my_key = $api_key;
	
	/*
	 *	Init the Facebook Javascript API, check if the user is currently logged into Facebook;
	 *	callback function = checkState
	 */
	 
	FB.init(my_key, $receiver, {"ifUserConnected":checkState, "ifUserNotConnected":checkState});
}

function checkState() 
{
	FB.Connect.get_status().waitUntilReady( function( status ) {
		switch ( status ) {
			case FB.ConnectState.connected:
				//alert('user logged in AND connected to your app!');
				getSession();
				break;
			case FB.ConnectState.appNotAuthorized:
				//alert('user logged in but NOT connected to your app!');
				flashCallBack("notConnected");
				break;
			case FB.ConnectState.userNotLoggedIn:
				//alert('user not logged in to Facebook !');
				flashCallBack("notLoggedIn");
				break;
		}
	}); 
}

function FBConnect() 
{
	/*
	 *	Method used to prompt the FB connect dialog
	 *  Call this function when you want to enforce that the current user is logged into Facebook. 
	 */
	
	FB.ensureInit(function() { 
		FB.Connect.requireSession(getSession, isUserActionHint=true);
	});
}

function getSession() 
{
	/* 	
	 *	Method to get session / user data
	 *	Called automatically if user is connected to your app;
	 *	Called automatically when user successfuly logs in through FB prompt;
	 */
	
	FB.ensureInit(function() { 
		api = FB.Facebook.apiClient;
		flashCallBack("returnSession", api.get_session());
	});
}

function FBLogout()
{
	FB.ensureInit(function() { 
		FB.Connect.logout(onLogout);
	});
}

function FBLogoutAndRedirect(url)
{
	FB.ensureInit(function() { 
		FB.Connect.logoutAndRedirect(url);
	});
}

function FBSwitchUser()
{
	/*
	 *  Currently not working...
	 * 	http://wiki.developers.facebook.com/index.php/JS_API_M_FB.Connect.SwitchUser
	 *	See Discussion...
	 */
	 
	FB.ensureInit(function() { 
		FB.Connect.switchUser();
	});
}

function FBInviteConnectUsers()
{
	FB.ensureInit(function() { 
		FB.Connect.inviteConnectUsers();
	});
}

function FBRevokeAuthorization()
{
	FB.ensureInit(function() { 
		api.revokeAuthorization(null, onAuthRevoke);
	});
}

function FBPublish(user_message, attachment, action_links, target_id, user_message_prompt, auto_publish, actor_id) 
{
	/*
	 *	Publish to the user's wall, newsfeed, Friends' Feed
	 */
	
	FB.ensureInit(function() { 
		FB.Connect.streamPublish(user_message, attachment, action_links, target_id, user_message_prompt, onPublish, auto_publish, actor_id);
	});
}

function FBgetEvents(uid, eids, start, end, rsvp)
{
	/*
	 * 	look up Events 
	 * 	filter on  [uid; pid;] eids; startTime; endTime; rsvpStatus
	 */
	FB.ensureInit(function() { 
		api.events_get(uid, eids, start, end, rsvp, onEvents);
	});
}

function FBgetEventMembers(eid)
{
	FB.ensureInit(function() { 
		api.events_getMembers(eid, onEventMembers);
	});
}

function FBgetFriends(uid)
{
	FB.ensureInit(function() { 
		api.friends_get(uid, onFriends);
	});
}

function FBAreFriends(u1, u2)
{
	FB.ensureInit(function() { 
		api.friends_areFriends(u1, u2, onAreFriends);
	});
}

function FBgetAppUsers()
{
	FB.ensureInit(function() { 
		api.friends_getAppUsers(onAppUsers);
	});
}

function FBgetFriendsLists()
{
	FB.ensureInit(function() { 
		api.friends_getLists(onFriendsLists);
	});
}

function FBgetGroups(uid, gids)
{
	FB.ensureInit(function() { 
		api.groups_get(uid, gids, onGroups);
	});
}

function FBgetGroupsMembers(gid)
{
	FB.ensureInit(function() { 
		api.groups_getMembers(gid, onGroupsMembers);
	});
}

function FBgetNotifications()
{
	FB.ensureInit(function() { 
		api.notifications_get(onNotifications);
	});
}

function FBSendNotifications(toids, msg)
{
	FB.ensureInit(function() { 
		api.notifications_send(toids, msg, onSentNotifications);
	});
}

function FBNotificationsSendEmail(toids, subject, body, fbml)
{
	FB.ensureInit(function() { 
		api.notifications_sendEmail(toids, subject, body, fbml, onEmailSent);
	});
}

function FBgetPermission(perm, enable, ids)
{
	FB.ensureInit(function() { 
		FB.Connect.showPermissionDialog(perm, onPermission, enable, ids);
	});
}

function FBPagesGetInfo(fields, pids, uid)
{
	FB.ensureInit(function() { 
		api.pages_getInfo(fields, pids, uid, onPagesInfo);
	});
}

function FBPagesIsAdmin(pid)
{
	FB.ensureInit(function() { 
		api.pages_isAdmin(pid, onPagesAdmin);
	});
}

function FBPagesIsAppAdded(pid)
{
	FB.ensureInit(function() { 
		api.pages_isAppAdded(pid, onAppAdded);
	});
}

function FBPagesIsFan(pid, uid)
{
	FB.ensureInit(function() { 
		api.pages_isFan(pid, uid, onFan);
	});
}

function FBPhotosGet(subj_id, aid, pid)
{
	FB.ensureInit(function() { 
		api.photos_get(subj_id, aid, pid, onPhotosGet);
	});
}

function FBAlbumsGet(uid, aid)
{
	FB.ensureInit(function() { 
		api.photos_getAlbums(uid, aid, onAlbumsGet);
	});
}

function FBAlbumsCreate($name, $location, $description)
{
	FB.ensureInit(function() { 
		api.photos_createAlbum($name, $location, $description, onAlbumsCreate);
	});
}

function FBPhotosGetTags(pids)
{
	FB.ensureInit(function() { 
		api.photos_getTags(pids, onPhotosGetTags);
	});
}

function FBPhotosAddTags(pid, tag_uid, tag_text, xcoord, ycoord)
{
	FB.ensureInit(function() { 
		api.photos_addTag(pid, tag_uid, tag_text, xcoord, ycoord, null, onPhotosAddTags);
	});
}

function FBUsersGetInfo(uids, fields)
{
	FB.ensureInit(function() { 
		api.users_getInfo(uids, fields, onUsersGetInfo);
	});
}

function FBHasAppPermission(permType)
{
	FB.ensureInit(function() { 
		api.users_hasAppPermission(permType, onHasAppPermission);
	});
}

function FBIsAppUser()
{
	FB.ensureInit(function() { 
		api.users_isAppUser(onIsAppUser);
	});
}

function FBSetStatus($status, $clear, $verb)
{
	FB.ensureInit(function() { 
		api.users_setStatus($status, Boolyzer($clear), Boolyzer($verb), onSetStatus);
	});
}

function FBStreamGet(src_ids, start_time, end_time, limit, filter_key)
{
	FB.ensureInit(function() { 
		api.stream_get(src_ids, start_time, end_time, limit, filter_key, onStreamGet);
	});
}

function FBStreamGetFilters()
{
	FB.ensureInit(function() { 
		api.stream_getFilters(onStreamGetFilters);
	});
}

function FBStreamGetComments(post_id)
{
	FB.ensureInit(function() { 
		api.stream_getComments(post_id, onStreamGetComments);
	});
}

/*
 *	Event Listeners
 */

function onLogout()
{
	flashCallBack("notLoggedIn");
}

function onEventMembers() 
{
	flashCallBack("onEventMembers", arguments[0], arguments[1]);
}

function onEvents()
{
	flashCallBack("onEvents", arguments[0], arguments[1]);
}

function onFriends()
{
	flashCallBack("onFriends", arguments[0], arguments[1]);
}

function onPublish(post_id, exception, data)
{
	flashCallBack("onPublish", post_id, exception, data);
}

function onAreFriends()
{
	flashCallBack("onAreFriends", arguments[0], arguments[1]);
}

function onAppUsers()
{
	flashCallBack("onAppUsers", arguments[0], arguments[1]);
}

function onFriendsLists()
{
	flashCallBack("onFriendsLists", arguments[0], arguments[1]);
}

function onGroups()
{
	flashCallBack("onGroups", arguments[0], arguments[1]);
}

function onGroupsMembers() 
{
	flashCallBack("onGroupsMembers", arguments[0], arguments[1]);
}

function onAuthRevoke()
{
	flashCallBack("onAuthRevoke", arguments[0], arguments[1]);
}

function onNotifications()
{
	flashCallBack("onNotifications", arguments[0], arguments[1]);
}

function onSentNotifications()
{
	flashCallBack("onSentNotifications", arguments[0], arguments[1]);
}

function onEmailSent()
{
	flashCallBack("onEmailSent", arguments[0], arguments[1]);
}

function onPermission()
{
	flashCallBack("onPermission", arguments[0], arguments[1]);
}

function onPagesInfo()
{
	flashCallBack("onPagesInfo", arguments[0], arguments[1]);
}

function onPagesAdmin()
{
	flashCallBack("onPagesAdmin", arguments[0], arguments[1]);
}

function onAppAdded()
{
	flashCallBack("onAppAdded", arguments[0], arguments[1]);
}

function onFan()
{
	flashCallBack("onFan", arguments[0], arguments[1]);
}

function onPhotosGet()
{
	flashCallBack("onPhotosGet", arguments[0], arguments[1]);
}

function onAlbumsGet()
{
	flashCallBack("onAlbumsGet", arguments[0], arguments[1]);
}

function onAlbumsCreate()
{
	flashCallBack("onAlbumsCreate", arguments[0], arguments[1]);
}

function onPhotosGetTags()
{
	flashCallBack("onPhotosGetTags", arguments[0], arguments[1]);
}

function onPhotosAddTags()
{
	flashCallBack("onPhotosAddTags", arguments[0], arguments[1]);
}

function onUsersGetInfo()
{
	flashCallBack("onUsersGetInfo", arguments[0], arguments[1]);
}

function onHasAppPermission()
{
	flashCallBack("onHasAppPermission", arguments[0], arguments[1]);
}

function onIsAppUser()
{
	flashCallBack("onIsAppUser", arguments[0], arguments[1]);
}

function onSetStatus()
{
	flashCallBack("onSetStatus", arguments[0], arguments[1]);
}

function onStreamGet()
{
	flashCallBack("onStreamGet", arguments[0], arguments[1]);
}

function onStreamGetFilters()
{
	flashCallBack("onStreamGetFilters", arguments[0], arguments[1]);
}

function onStreamGetComments()
{
	alert('flashCallBack \narguments.length: ' + arguments.length + ' \narguments[0]: '  +arguments[0] + ' \narguments[1]: '  +arguments[1]);
	flashCallBack("onStreamGetComments", arguments[0], arguments[1]);
}
function flashCallBack() 
{
	/* 
	 * Method to dispatch Data back to Flash
	 * Can take multiple arguments
	 */
	
	//alert('flashCallBack \narguments.length: ' + arguments.length + ' \narguments[0]: '  +arguments[0] + ' \narguments[1]: '  +arguments[1]);
	
	if(arguments.length > 1) {
		var args = []; 
		for(var i = 1; i < arguments.length; i++) {
			args.push(arguments[i]);
		}
		thisMovie(my_swf)[arguments[0]](args);
	} else {
		thisMovie(my_swf)[arguments[0]]();
	}
}
function thisMovie(movieName) 
{
	/*
	 * Target the right html div through reference with the swf name
	 * depending on browser type...
	 */

	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	}
	else {
		return document[movieName];
	}
}

function Boolyzer(arg)
{
	switch(arg){
		case true:
			return "true";
		case false:
			return "false";
		default:
			return null;
	}
}
