var userId = null;
var facebook = {

	init: function (callback,applicationID){
		if(!applicationID){
			applicationID = 'e41c04ab569dcd310074152238496d34';
		}
		window.fbAsyncInit = function() {
			FB.init({appId: applicationID, status: true, cookie: true,	xfbml: true});
			if(callback)
				(callback)();
		};
		
		$("body").prepend('<div id="fb-root"></div>');
		
		var loader = function(){
			var e = document.createElement('script');
			e.async = true;
			e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
			document.getElementById('fb-root').appendChild(e);
		};
		
		if(window.FB != null){

			FB_RequireFeatures(["CanvasUtil"], function() {
				FB.CanvasClient.setCanvasHeight("3000px"); ;
				window.FB = null;
				(loader)();
			});
		}
		else{
			(loader)();
		};
	},

	login: function(loginCallback,permissions){
		var options = null;
		if(permissions)
			options =  {perms:permissions};
		FB.login(function(response) {
				if (response.session) {
					userId = response.session.uid;
					var requiredPermission = permissions;
					if(permissions.indexOf(",") != -1)
						requiredPermission = permissions.substring(0,permissions.indexOf(","));
					var haveRequiredPermissions = !permissions || (response.perms && response.perms.indexOf(requiredPermission) != -1);
					if (haveRequiredPermissions) {
						loginCallback.loginSuccessful(response.perms);
					}
					else {
						loginCallback.permissionNotGranted(response.perms);
					}
				}
				else {
					loginCallback.loginCancelled();
				}
			}, options);
	},
	
	fql: function(callback,queryTemplate,queryData){
		var waitable = FB.Data.query(queryTemplate,queryData);
		waitable.wait(callback);
	},
	
	bindEventListener: function(event,listener){
		FB.Event.subscribe(event, listener);
	},
	
	postToWall: function(message,name,description){
		var uiCallObj = {
			method: 'stream.publish',
			message: message,
			attachment: {
				name: name,
				description: description,
				href: 'http://www.afairchance.eu/game/'
				,
				'media': [
					{	'type': 'image',
						'src': 'http://www.afairchance.eu/images/logo.png',
						'href': 'http://apps.facebook.com/afairchance/'
					}]
			},
					
			action_links: [
				{ text: 'Play', href: 'http://afairchance.eu/game/' }
			],
			user_prompt_message: 'Give others "A fair chance to save the world"'
			};
		
	   FB.ui(uiCallObj);
	},
	
	forcePostToWall: function(message,name,description){
		var uiCallObj = {
			method: 'stream.publish',
			message: message,
			attachment: {
				name: name,
				description: description,
				href: 'http://www.afairchance.eu/game/'
				,
				'media': [
					{	'type': 'image',
						'src': 'http://www.afairchance.eu/images/logo.png',
						'href': 'http://apps.facebook.com/afairchance/'
					}]
			},
					
			action_links: [
				{ text: 'Play', href: 'http://afairchance.eu/game/' }
			],
			user_prompt_message: 'Give others "A fair chance to save the world"'
			};
		
	    	FB.api(
	                  uiCallObj,
	                  function(response) {
	                  }
	                );
	}
};

function LoginCallback(){
	this.loginSuccessful = function(){};
	this.loginCancelled = function(){};
	this.permissionNotGranted = function(){};
}

