/*  Prototype extension to make AJAX calls to specified object methods
 *  (c) 2006 hl-studios
 *
 *  Requires the Prototype JavaScript library and a JSON encoder.
 *
/*--------------------------------------------------------------------------*/

if( !Prototype ) {

	alert( "Ajax.Server requires Prototype!" );
	
} 
else if( !JSON ) {

	alert( "Ajax.Server requires JSON!" );
	
}
else {

	Ajax.Server = "/_core/ajax/AjaxServer.php";

	Ajax.ServerCall = Class.create();
	Object.extend( Object.extend( Ajax.ServerCall.prototype, Ajax.Request.prototype ), {
	
		initialize: function( serverclass, func, args, options ) {
			this.transport = Ajax.getTransport();
			this.setOptions( options );
			this.options.method = "get";
			this.options.parameters = {
                classname: serverclass,
                func: func,
                args: JSON.stringify(args) 
            };
			// alert( this.options.parameters );
			this.request( Ajax.Server );
		}
		
	} );
    
    Ajax.UICall = Class.create();

    Object.extend( Object.extend( Ajax.UICall.prototype, Ajax.Request.prototype ), {
    
        initialize: function( view_area, view, output_format, args, options ) {
            this.transport = Ajax.getTransport();
            this.setOptions(options);
            this.options.method = "get";
            this.options.encoding = "UTF-8";
            this.options.parameters = {
                view_area: view_area,
                view: view,
                view_output_format: output_format,
                args: JSON.stringify(args) 
            };
                       
            this.request( Ajax.Server);
        }
        
    } );
	
	Ajax.ContextUpdater = Class.create();

	Object.extend( Object.extend( Ajax.ContextUpdater.prototype, Ajax.Updater.prototype ), {

		initialize: function( container, url, args, options ) {
			this.container = {
				success: $( container ),
				failure: $( container )
			}
			this.transport = Ajax.getTransport();
			this.setOptions( options );
			this.options.method = "get";
			this.options.evalScripts = true;
			this.options.parameters = {
                url: url,
                args: JSON.stringify(args)
            };
			//alert( parameters );
    		this.options.onSuccess = this.updateContent.bind( this );
			this.request( Ajax.Server);
		}

	} );
    
    Ajax.UIUpdater = Class.create();

    Object.extend( Object.extend( Ajax.UIUpdater.prototype, Ajax.Updater.prototype ), {

        initialize: function( container, view_area, view, output_format, args, options ) {
            this.container = {
                success: $( container ),
                failure: $( container )
            };
            this.transport = Ajax.getTransport();
            this.setOptions( options );
            this.options.method = "get";
            this.options.evalScripts = true;
            this.options.parameters = {
                view_area: view_area,
                view: view,
                view_output_format: output_format,
                args: JSON.stringify(args)
            };
                      
            this.options.onSuccess = this.updateContent.bind( this );
            this.request( Ajax.Server );
        }

    } );
}