var api = {
	/**
	 * @params url The url to call (after API_DIR)
	 * @params data An object with the data which will be $_POST'ed
	 * @params callback The callback function, called on success
	 * @params errorHandler This callbackfunction is called when an exception is thrown.
	 */
	post: function(url, data, callback, errorHandler, async) {
		if(!url) {
			return;
		}
		
		var options = {
			type: "POST",
			url: API_DIR + url,
			data: data,
			dataType: "json",
			success: callback,
			async: async == undefined ? true : async,
			errorHandler: errorHandler
		};
		$.ajax(this.processSuccessFunction(options));
		
	},

	processSuccessFunction: function(options) {
		var oldsuccesfunction = options.success;
		if (typeof oldsuccesfunction != "function") {
			oldsuccesfunction = function(response) { }
		}

		options.success = function(response) {

			//an error occured..
			if(response.status != 'success'){

				//..but we have a custom error function, which we will call now
				if(typeof options.errorHandler == 'function'){
					options.errorHandler(response);

				//..but we do -not- have a custom error function, smack the error in our face
				}else{
					if(typeof admin == 'object'){
						alert('Error ' + response.code +' : ' + response.message + ' (write proper error display) ');
					}else{
						alert('Error ' + response.code +' : ' + response.message);
					}
				}
				
			//everything went well. Call the original success function
			}else{
				oldsuccesfunction(response.data);
			}
		}
		return options;
	}
}
