
/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


/*******************************************************/

	// INIT VARS

	var flashId;
	var api_key;
	var xd_receiver;
	var SD = {
		isConnected: false, 
		detailsFetched: false
	}



/*******************************************************/

	/// INIT FB CONNECT
	
	function initFacebookConnect( _flashId, _api_key, _xd_receiver )
	{
		flashId = _flashId;
		api_key = _api_key;
		xd_receiver = _xd_receiver;
		
		setTimeout('FB.init( api_key, xd_receiver )', 5000);
	}
	


/*******************************************************/

	/// GET FLASH OBJECT
	
	function getFlashObject(flashId)
	{
	     if (navigator.appName.indexOf("Microsoft") != -1) {
	         return window[flashId];
	     } else {
	         return document[flashId];
	     }
	}
	

	// call methods in flash

	function flashDispatcher( functionName )
	{

		var flashObject = getFlashObject( flashId );
		
		if(arguments.length > 1)
			flashObject[ functionName ]( Array.prototype.slice.call(arguments).slice(1)[0] );
		else
			flashObject[ functionName ]();

		return true;
	}
	




/*******************************************************/

	// Setup/Validate User Session
	
	function facebook_requestSession()
	{
	
	    FB.ensureInit(function()
	    {

	        FB.Connect.ifUserConnected
			(
			 	/// action for if connected
			 	function ()
			 	{

					/// do soemthing if the user is already connected
			 		SD.isConnected = true;
			 		facebook_onRequestSessionComplete();

			 	},
			 	
			 	/// action for it NOT conected
				function ()
				{

					/// return the login popup
					FB.Connect.requireSession
					(
						facebook_onRequestSessionComplete,
						false
					);

				}
			);
	    });	
	
	}
	
	// flash bridge for facebook_requestSession
	function facebook_onRequestSessionComplete()
	{
		SD.isConnected = true;
		flashDispatcher( "facebook_onRequestSessionComplete", FB.Facebook.apiClient._session );
	}

	/// double check they are logged index
	function checkLoggedIn() {

		/// make sure they are logged in.
		if (SD.isConnected == false) {
			facebook_requestSession();
			return false;
		} else {
			return true;
		}

	}
	



/*******************************************************/

	/// GET USER INFO
	
	function facebook_getUserInfo()
	{

		checkLoggedIn();
	
		FB.ensureInit(function() 
		{
			/// the fields we want to fetch
			var arrProfileData = ["timezone", "status", "sex", "proxied_email", "profile_url", "pic_square_with_logo", "pic_square", "pic_small_with_logo", "pic_small", "pic_big_with_logo", "pic_big", "pic_with_logo", "pic", "name", "first_name", "last_name", "is_app_user", "hometown_location", "birthday", "about_me", "uid"];
			
			FB.Facebook.apiClient.users_getInfo( FB.Facebook.apiClient._session.uid, arrProfileData, function(result, ex) 
			{	
				//assign object to var
				userResult = eval(result[0]);
				facebook_onGetUserInfoComplete( "", userResult );

			});
		});
	}

	// flash bridge for facebook_getUserInfo
	function facebook_onGetUserInfoComplete(userdata)
	{
		flashDispatcher( "facebook_onGetUserInfoComplete", userdata );
	}


/*******************************************************/

	/// PUBLISH FEED STORY

	function facebook_publishFeedStory(template_bundle_id, template_data, target_id, body_general, story_size, user_message_prompt, user_message ) 
	{

	}
	
	
	// flash bridge for facebook_publishFeedStory
	function facebook_onPublishFeedStoryComplete(post_id, exception, data)
	{
		flashDispatcher( "facebook_onPublishFeedStoryComplete", post_id );
	}
	



/*******************************************************/

	/// PUBLISH STREAM


	// init vars
	var streamObject = {
		user_message: '', 
		attachment: '', 
		action_links: '', 
		target_id: '', 
		user_message_prompt: '', 
		auto_publish: '', 
		actor_id: ''
	}
	
	//call this from flash for the multi selector window

	function facebook_streamPublishMulti( user_message, attachment, action_links, target_id, user_message_prompt, auto_publish, actor_id ) 
	{
	
		$('#jqmContent').attr('src','/facebook.html');

		streamObject = {
			user_message: user_message, 
			attachment: attachment, 
			action_links: action_links, 
			target_id: target_id, 
			user_message_prompt: user_message_prompt, 
			auto_publish: true, 
			actor_id: actor_id
		}

		//start using the new modal multi-friend selector
		$('#modalWindow').jqmShow();

	}	


	function facebook_streamPublish( user_message, attachment, action_links, target_id, user_message_prompt, auto_publish, actor_id ) 
	{
	
		checkLoggedIn();
	
		FB.ensureInit(function() 
		{
			FB.Connect.streamPublish ( user_message, attachment, action_links, target_id, user_message_prompt, facebook_onStreamPublishComplete, auto_publish, actor_id) 
		});

	}
	
	// flash bridge for facebook_streamPublish
	function facebook_onStreamPublishComplete(post_id, exception, data)
	{

		flashDispatcher( "facebook_onStreamPublishComplete", post_id );
	}
	



/*******************************************************/

	/// REQUEST PERMS	
				
	function facebook_requestPermission(permission)
	{
		FB.Connect.showPermissionDialog(permission, facebook_onRequestPermissionComplete(result));
	}

	// flash bridge for facebook_requestPermission
	function facebook_onRequestPermissionComplete(result)
	{
		flashDispatcher( "facebook_onRequestPermissionComplete", result );
	}



