/**
 * Rocket JavaScript API - Facebook
 *
 * This API handles authorizing a user via the Facebook Graph API client-side.
 * The actual implementation of the API are coded in rocket/apis/facebook.php.
 *
 * @version			1.0
 * @package			Rocket Framework
 * @subpackage		JavaScript Framework
 * @category		APIs
 * @author			Mads Felskov Agersten <mads@solvationlab.dk>
 * @copyright		2010+ Solvation Lab <http://www.solvationlab.com/>
 */

ApisFacebook = {
	/**
	 * These variables configure the Facebook API.
	 *
	 * @version		1.0
	 * @author		Mads Felskov Agersten <mads@solvationlab.dk>
	 * @access		protected
	 */
	app_id		: '133183263420349',
	app_scope	: 'email,user_birthday,user_hometown,user_education_history,user_work_history,user_likes,publish_stream',

	/**
	 * This function opens a popup-window in which the user can connect with
	 * Facebook. Afterwards the window is redirected to /apis/facebook/
	 * authorize.php, where the login is verified, an access-token is exchanged
	 * with Facebook and the window is closed.
	 *
	 * @param string scope The scope of permissions to ask for (optional).
	 *
	 * @version		1.0
	 * @author		Mads Felskov Agersten <mads@solvationlab.dk>
	 * @access		protected
	 */
	authorize : function(redirect_uri) {
		// Calculate the dimensions of the current window
		var win_w = (window.innerWidth || document.documentElement.offsetWidth || document.body.offsetWidth || 0);
		var win_h = (window.innerHeight || document.documentElement.offsetHeight || document.body.offsetHeight || 0);

		// Calculate dimensions of the popup
		var popup_w = 500;
		var popup_h = 300;

		// Calculate the size of the screen
		var screen_w = screen.width;
		var screen_h = screen.height;

		// Calculate the position of the popup
		var popup_x = Math.max(0, Math.min(screen_w - popup_w, Math.round((window.screenLeft || window.screenX || 0) + (win_w - popup_w) / 2)));
		var popup_y = Math.max(0, Math.min(screen_h - popup_h, Math.round((window.screenTop || window.screenY || 0) + (win_h - popup_h) / 2)));

		// Open the popup-window
		window.open('https://www.facebook.com/dialog/oauth?display=popup&client_id='+ encodeURIComponent(this.app_id) +'&redirect_uri='+ encodeURIComponent(document.getElementsByTagName('base')[0].href + redirect_uri) +'&scope='+ encodeURIComponent(this.app_scope || ''), 'facebook_api', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizeable=1,scrollbars=1,width='+ popup_w +',height='+ popup_h +',left='+ popup_x +',top='+ popup_y);
	}
};
