/**
 * The AjaxCaller class is used to easily package function calls to PHP Ajax scripts
 * 
 * @author Joel Barker
 * 
 * 
 * Example:
 * 
 *      var caller = new AjaxCaller('ajax/ajax.php');
 * 
 *      caller.callback = function(result)
 *      {
 *          alert('The command returned: ' + result);
 *      }
 * 
 *      caller.call('multiply', 3, 4);          // calls the PHP multiply(3, 4) function in ajax/ajax.php
 * 
 * 
 * The AjaxListener class should be used on the PHP side to unpackage and execute the function call.
 *
 *
 * The AjaxCaller constructor takes one optional argument:
 * 
 *      @param string script            The Ajax script to post to; defaults to '/ajax.php'
 */
var AjaxCaller = function(script)
{
    /**
     * @public .script
     * 
     * This gets set from the constructor argument, or can be set after construction to an Ajax script path
     */
    this.script = script;
    
    if (undefined == this.script)
    {
        this.script = '/ajax.php';
    }
    
    
    /**
     * @public .callback
     * 
     * Set this to a function callback that will be executed when the Ajax call returns.
     * The callback will be passed a single value: the result of the Ajax call.
     */
    this.callback = function(result)
    {
        // this should be overriden
    };
    
    
    /**
     * @public function .call()
     * 
     * Calls an Ajax function with arguments (if the Ajax script is using AjaxListener)
     * 
     * @param mixed ...             Any number of arguments to pass to the Ajax function
     * 
     * @return void                 No meaningful return value (actually returns a Yahoo Connection object...)
     */
    this.call = function()
    {
        // arguments must be copied into an array to call another function
        var params = new Array();
        for (var i = 0; i < arguments.length; i++)
        {
            switch (true)
            {
                case false === arguments[i]:
                    params[i] = '';
                    break;
                    
                default: 
                    params[i] = arguments[i];
                    break;
            }
        }
        
	    Ext.Ajax.request({
	        url: this.script,
	        success: function(o)
	        {
	            var result = o.responseText;
	            
	            this.callback(result);
	        },
	        failure: function(o)
	        {
	            alert("Ajax connection error!\r\n\r\n" + o.statusText);
	        },
	        params: params,
	        scope: this
	    });
        
    };
};


/**
 * @public function .freezeCall
 * 
 * Package an Ajax function call into an array, but don't execute it.
 * The array can be used later to post to an Ajax script for execution.
 * 
 * @param mixed ...             Any number of arguments to pass to the Ajax function
 * 
 * @return array                The packaged function call
 */
AjaxCaller.freezeCall = function()
{
    // arguments must be copied into an array to call another function
    var args = new Array();
    for (i = 0; i < arguments.length; i++)
    {
        switch (true)
        {
            case false === arguments[i]:
                args[i] = '';
                break;
                
            default: 
                args[i] = arguments[i];
                break;
        }
    }
    
    return args;
};
