var FBConnect = function($){
	var _uid = "0";
	var _state = "none";
	var _callback = null;
	
	function setup(apiKey) {
		// Append the fb-root 
		$('body').append('<div id="fb-root"></div>');
		
		// Initialize the library with the API key
		FB.init({apiKey: apiKey});
		
		// Fetch the status on load
		FB.getLoginStatus(handleSessionResponse);
		
	}
	
	/**
	 * Login
	 *
	 * @param callback
	 */
	function login(callback) {
		_state = "login";
		_callback = callback; // Store the callback function
		if (_uid == "0") {
			FB.login(handleSessionResponse);
		}
		else {
			callCallback("allow");
		}
	}
	
	/**
	 * Handle session response
	 *
	 * @param response
	 */
	function handleSessionResponse(response) {
		var flag = (_state == "login" ? true : null);
		if (!response.session) {
			if (flag) {
				callCallback("deny");
			}
			return;
		}
		
		_uid = FB.getSession().uid;
		if (flag) {
			callCallback("allow");
		}
	}
	
	/**
	 * Callcallback 
	 *
	 * @param state
	 */
	function callCallback(state) {
		swfobject.getObjectById("Pizzabygger")[_callback](state);
		_state = state;
	}
	
	
	return {
		/**
		 *  Initialize
		 */
		init: function(apiKey){
			setup(apiKey);
		},
		
		/**
		 * Get the user _uid
		 *
		 * @return	Returns 0 if the user _uid has not been set
		 */
		getUID: function() {
			return _uid;
		},
		
		login: function(callback) {
			login(callback);
		}
	};
}(jQuery);
$(document).ready(function(){
	FBConnect.init("e40ac69516c2eaba3cea47faddc2cd30");
});
