/*
File: mooremote.js
This file needs to be included after Mootools and before the JS generated by <MooRemote::getJavascript>.

License:
	MIT-style license

Author:
	Jan Kassens
	
Dependencies:
	- Mootools >= 1.2dev
	- Remote.XHR and Remote.Json
*/
var MooRemote = new Class
({
	options: null,
	
	initialize: function(functions, options)
	{
		MooRemote.options = options || {};
		
		var url = MooRemote.options.url || window.location.href;
		var self = this;
		
		functions.each(function(fn)
		{
			if (fn.klass)
			{
				self[fn.klass] = {};
				
				fn.methods.each(function(method)
				{
					self[fn.klass][method] = function() { 							
						return new MooRequest(url, [fn.klass, method], arguments[0]);
					}
				});
			}
			else
			{
				self[fn] = function() {
					return new MooRequest(url, fn, arguments[0]);
				}
			};
		});
	}
});
/*
Class: MooRequest
Any call with mooRemote.* returns an instance of this class. There are two events you can register with addEvent.

Events:
	onSuccess - is fired after a successful function call. The return value of the PHP function is passed as the first 
	argument of the event.
	onError - is fired if an error was triggered or an exception thrown in your PHP code. Argument is the error string.
*/

var MooRequest = XHR.extend
({
	id_ajax_loading:		null,
	result:					null,
	std_ajax_loader:		false,
	
	/**
	 * 
	 */
	initialize: function(url, fn, arguments)
	{
		this.parent();
		this.setHeader('X-Requested-With', 'MooRequest');
		
		if ( $defined(arguments.update) ) {
			this.result = $type(arguments.update) == 'string' ? $(arguments.update) : arguments.update; 
		}

		if ( $defined(arguments.id) ) {
			this.id_ajax_loading = $type(arguments.id) == 'string' ? $(arguments.id) : arguments.id;
		}
		
		if ( $defined(arguments.std_ajax_loader) )
		{
			this.std_ajax_loader = arguments.std_ajax_loader;
			
			if (arguments.std_ajax_loader) {
				this.id_ajax_loading.addClass('ajax-loading');
			}
			else {
				if ( this.id_ajax_loading !== null) {
					this.id_ajax_loading.removeClass('hidden');
				}			
			}
		}
		
		this.addEvent('onError', eval(MooRemote.options.onError));
		this.send(url, "call=" + encodeURIComponent(Json.toString({'method': fn, 'params': arguments.params})));
	},
	
	/**
	 * 
	 */
	onSuccess: function()
	{
		var response = Json.evaluate (this.transport.responseText);
				
		if (this.id_ajax_loading !== null)
		{
			if ( this.id_ajax_loading.id == this.result.id && this.std_ajax_loader)
			{
				this.id_ajax_loading.removeClass('ajax-loading');
			}
			else {
				this.id_ajax_loading.addClass('hidden');
			}
		}
		
		if ($defined(response.error)) {
			this.fireEvent('onError', response.error);
		}
		else
		{
			if ( $defined(response.result) )
			{
				if ( response.result !== false )
				{
					if ( $defined(this.result) )
					{
						if (response.html != "") {
							this.result.setHTML(response.html);
						}
					}
				}
			}
			return this.fireEvent('onSuccess', $defined(response.result) ? [response.result] : []);
		}
	},
	
	/*
	Method: callback
	callback is a shortcut for addEvent("onSuccess", fn)
	
	Parameters:
		fn - Callback function.
	
	Example:
	(code)
	function myCallback(result){
	    alert(result);
	}
	mooRemote.foobar().callback(myCallback);
	// which is the same as
	mooRemote.foobar().addEvent('onSuccess', myCallback);
	(end code)
	*/
	callback: function(fn) {
		return this.addEvent('onSuccess', fn);
	}

});