/*
 * Class to use the Facebook API in an easier way
 * @author Toon Verwerft
 */

function Facebook (appId, accessToken, config)
{
	this.appId = appId;
	this.accessToken = accessToken;
	
	//constants:
	this.endpoints = new Array();
	this.endpoints['graph'] = 'https://graph.facebook.com/';
	this.endpoints['api'] = 'https://api.facebook.com/';
	this.endpoints['api_video'] = 'https://api-video.facebook.com/';
	this.endpoints['api_read'] = 'https://api-read.facebook.com/';
	this.endpoints['www'] = 'https://www.facebook.com/';
	
	//basic config:
	this.fbconfig = {
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : true  // parse XFBML
	}
	//overwrite basic config:
	if (config) {
		for (key in config) {
			this.fbconfig[key] = config[key];
		}
	}
	
	/**
	 * Function to initialize the facebook functions
	 * @return void
	 */
	this.init = function() 
	{
		this.fbconfig['appId'] = this.appId;
		FB.init(this.fbconfig);
	}
	
	/**
	 * Function to let a user login
	 * @param string permissions: the permissions needed for the app
	 * @param function callback: the function to call, when data loading is ready
	 * @param function errorHandler: the function to call, when data loading is broken
	 * @return void
	 */
	this.login = function(permissions, callback, errorHandler)
	{
		FB.login(function(response) {
			if (response.session) {
				if (response.perms) {
					callback();
					return true;
				} 
			} 
			errorHandler();
		}, {perms: permissions});
	}
	
	/**
	 * add a login handler to following xfbml code:
	 * <fb:login-button perms="permissions">label</fb:login-button>
	 * @param callback the function to call when the user is logged in
	 * @param errorHandler the function to call when the user isn't logged in
	 */
	this.addLoginHandler = function (callback, errorHandler) {
		FB.Event.subscribe('auth.login', function (response) {
			if (response.session) {
				callback();
				return true;
			}
			errorHandler();
		});
	}

	
	//initialize the facebook functions
	this.init();
}

/**
 * Facebook User functions:
 * Extends Facebook class (Descriptor)
 */
FacebookUser = function(facebook, userId) {
	this.facebook = facebook;
	this.userId = userId;

	/**
	 * Get the profile picture
	 * @param string type: square|small|large
	 */
	this.getProfilePicture = function(type)
	{
		type = (!type) ? 'square' : type;
		return facebook.endpoints['graph'] + this.userId + '/picture?type=' + type;
	}
}


/**
 * Facebook Event functions:
 * Extends Facebook class (Descriptor)
 * http://developers.facebook.com/docs/reference/api/event/
 * @permissions: rsvp_event
 */
FacebookEvent = function(facebook, eventId) {
	this.facebook = facebook;
	this.eventId = eventId;
	
	/**
	 * Gets all the attending Users
	 * @param function callback: the function to call, when data loading is ready
	 * @param function errorHandler: the function to call, when data loading is broken
	 * @return void
	 */
	this.getAttending = function(callback, errorHandler) 
	{
		FB.api('/' + this.eventId + '/attending', function(response) {
			if(response.error) {
				errorHandler(response.error['message']);
			} else {
				callback(response.data);
			}
		} , {access_token: facebook.accessToken} );	   
	}

	/**
	 * Set the attending status when the user logs in
	 * @return void
	 */
	this.setAttending = function()
	{
		FB.api('/' + this.eventId + '/attending', 'post');
	}
}
