/**
 * Provides several Gateway-Function to the Facebook-API
 *
 * @authors		Martin Widemann
 * @copyright	Copyright 2011 - Netigo GmbH
 * @version		1.0
 * @modified	2011-01-04
 */
var Facebook_Gateway = {

	/**
	 * Attaches the Facebook-JS Client to the Document
	 * 
	 * @param string root ID of the documents Root-Container
	 * @param string language Language to use within the JS-Client
	 */
	loadFb: function(root, language)
	{
		var fb = document.createElement('script');
		fb.src = document.location.protocol + '//connect.facebook.net/' + language + '/all.js';
		fb.async = true;
		document.getElementById(root).appendChild(fb);
	},

	/**
	 * Shows the Login-Dialog
	 * 
	 * A Description of the available Permissions can be found here:
	 * http://developers.facebook.com/docs/authentication/permissions
	 * 
	 * @param string perms List of Permissions
	 * @param function callback Reference to Callback-Function
	 */
	dialogLogin: function (scope, callback)
	{
		FB.login(
			callback,
			{
				scope: scope
			}
		);
	},

	/**
	 * Posts something to a Users Board
	 *
	 * A Description of all Contents, that can be posted to a Users Board
	 * can be found here:
	 * http://developers.facebook.com/docs/api
	 * http://developers.facebook.com/docs/reference/api/post/
	 *
	 * @param string user_id ID of User to post at its Board
	 * @param object content Content-Object to post
	 * @param function callback Reference to Callback-Function
	 */
	postToBoard: function (user_id, content, callback)
	{
		user_id = Facebook_Gateway.checkUserId(user_id);

		FB.api(
			'/' + user_id + '/feed', 
			'post', 
			content, 
			callback // fbPostToBoardCallback
		);
	},

	/**
	 * Publishes something in the Users Stream
	 *
	 * A Description of all Items, that can be published to the Users Stream
	 * can be found here:
	 * http://developers.facebook.com/docs/reference/rest/stream.publish
	 *
	 * @param object attachment Attachment-Object to be published
	 * @param array action_links Array of Link-Objects to be published
	 * @param function callback Reference to Callback-Function
	 */
	dialogPublishToStream: function(attachment, action_links, callback)
	{
		FB.ui(
			{
				method: 'stream.publish',
				attachment: attachment,
				action_links: action_links
			},
			callback
		);
	},

	/**
	 * Fetches the users Data
	 *
	 * @param string user_id ID of User
	 * @param function callback Reference to Callback-Function
	 */
	getUser: function(user_id, callback)
	{
		user_id = Facebook_Gateway.checkUserId(user_id);

		FB.api(
			'/' + user_id, 
			callback
		);
	},

	/**
	 * Checks, if App is currently embedded in Facebook
	 *
	 * @return bool
	 */
	embedded: function()
	{
		return Boolean(facebook_embedded);
	},

	checkUserId: function(user_id)
	{
		if(!user_id){
			return 'me';
		} // if

		return user_id;
	},
	
	logout: function (callback)
	{
		FB.logout(
			callback
		);
	}

}

