/*
=head1 NAME

takkle.server

=head1 DESCRIPTION

=head1 INHERITS FROM

=head1 METHODS

=over 4

*/

dojo.provide( 'takkle.server' );
dojo.declare( 'takkle.server', takkle, {

/*
=item * obj = new( params )

=back

*/
    constructor: function ( params ) {
        this.params = params;

    },


    request: function ( params ) {
        if ( !params ) { params = {}; }
        if ( !params.args ) { params.args = {}; }

		if (!params.method) { params.method = 'post'; }
		if (!params.path) { throw('no path supplied to server.request'); }
		if (!params.onComplete) { params.onComplete = function() {}; }
		if (!params.onError) { params.onError = function() {}; }
		if (!params.sync) { params.sync = false; }

        var self = this;
		var args = {
            url: params.path,
            handleAs: "json",
            timeout: 50000, //Time in milliseconds
            preventCache: false,
            content: params.args,
			form: params.form,
			sync: params.sync,
			handle: function(response, ioArgs){
                if(response instanceof Error){
                    if(response.dojoType == "cancel"){
                        //The request was canceled by some other JavaScript code.
                        throw("Request canceled.");
						params.onError({ message: 'cancelled' });
                    }else if(response.dojoType == "timeout"){
                        //The request took over 5 seconds to complete.
                        throw("Request timed out.");
						params.onError({ message: 'timed_out' });
                    }else{
						try {
							// manually parse response since dojo doesn't do it
							params.onError( dojo.fromJson(ioArgs.xhr.responseText) );
						}
						catch(e) {
							// not json
							params.onError({ args:response, message: 'server error' });
						}
                    }
                }else{	
					params.onComplete((typeof response == 'object' && response.data) ? response.data : response);
                    console.debug("HTTP status code: ", ioArgs.xhr);
                }
            }
		};

		// send request
        if (params.method == 'post') {
			dojo.xhrPost(args);
		} else if (params.method == 'get') {
			args.contentType = ' '; // otherwise dojo sets it to something retarded
			dojo.xhrGet(args);
		} else {
			throw('invalid method: ' + params.method);
		}
    },
	// to use this method you must have dojo.require( 'dojo.io.iframe' ) somewhere
    iframeRequest: function ( params ) {
        if ( !params ) params = {};
        if ( !params.args ) params.args = {};

        var self = this;
        var deferred = dojo.io.iframe.send({
            url: params.url,
            handleAs: "json",
            timeout: 50000, //Time in milliseconds
            preventCache: true,
            content: params.args,
            form: params.form,
            handle: function(response, ioArgs){
                if(response instanceof Error){
                    if(response.dojoType == "cancel"){
                        //The request was canceled by some other JavaScript code.
                        console.debug("Request canceled.");
                        params.error({ reason: 'cancelled' });
                    }else if(response.dojoType == "timeout"){
                        //The request took over 5 seconds to complete.
                        console.debug("Request timed out.");
                        params.error({ reason: 'timed_out' });
                    }else{
                        // it sucks that i have to do this
                        // params.error( dojo.fromJson(ioArgs.xhr.responseText) );
                        params.error({ reason: 'unknown' });
                    }
                }else{
                    params.load(response);
                    console.debug("HTTP status code: ", ioArgs.xhr);
                }
            }
        });
    }
});

/*
=back

=cut
*/
