var TauriAPI = {}; //TauriAPI

Tauri.TimeoutRequest = Class.create(Ajax.Request,{

    initialize : function($super,url,options) {
        $super(url,options);
    },
    request: function(url) {
        this.url = url;
        this.method = this.options.method;
        var params = Object.clone(this.options.parameters);

        if (!['get', 'post'].include(this.method)) {
            // simulate other verbs over post
            params['_method'] = this.method;
            this.method = 'post';
        }

        this.parameters = params;

        if (params = Object.toQueryString(params)) {
            // when GET, append parameters to URL
            if (this.method == 'get')
                this.url += (this.url.include('?') ? '&' : '?') + params;
            else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
                params += '&_=';
        }

        try {
            var response = new Ajax.Response(this);
            if (this.options.onCreate) this.options.onCreate(response);
            Ajax.Responders.dispatch('onCreate', this, response);

            this.transport.open(this.method.toUpperCase(), this.url,
                this.options.asynchronous);

            if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);

            this.transport.onreadystatechange = this.onStateChange.bind(this);
            this.setRequestHeaders();

            this.body = this.method == 'post' ? (this.options.postBody || params) : null;
            this.transport.send(this.body);

            /* Force Firefox to handle ready state 4 for synchronous requests */
            if (!this.options.asynchronous && this.transport.overrideMimeType)
                this.onStateChange();

            if(this.options.timeoutDelay && this.options.timeout)
                this.startTimer(this.options.timeoutDelay);
        }
        catch (e) {
            this.dispatchException(e);
        }
    },
    cancel : function() {
        this.handleTimeout();
    },
    clearTimeout : function(){
        clearTimeout(this.timer);
    },
    startTimer : function(sec){
        this.timer = setTimeout(this.handleTimeout.bind(this), (sec*1000));
    },
    handleTimeout : function(){
        try{
            this.timeout = true;
            this.transport.abort();
            this.options.onTimeout(new Ajax.Response(this));
        }
        catch (e) {
            this.dispatchException(e);
        }
    },
    respondToReadyState: function(readyState) {
        var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
        if (state == 'Complete') {
            try {
                this._complete = true;
                (this.options['on' + response.status]
                    || this.options['on' + (this.success() ? 'Success' : 'Failure')]
                    || Prototype.emptyFunction)(response, response.headerJSON);
            } catch (e) {
                this.dispatchException(e);
            }

            var contentType = response.getHeader('Content-type');
            if (this.options.evalJS == 'force'
                || (this.options.evalJS && this.isSameOrigin() && contentType
                    && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
                this.evalResponse();
        }

        try {
            (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
            Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
        } catch (e) {
            this.dispatchException(e);
        }

        if (state == 'Complete') {
            this.clearTimeout();
            // avoid memory leak in MSIE: clean up
            this.transport.onreadystatechange = Prototype.emptyFunction;
        }
    }
});


TauriAPI = new (Class.create({
    URL : null,
    busy : false,
    msgBox : null,
    timeout : null,
    SERVER_NOT_RESPONDING_CODE : 4,

    request : function (command, post, callBacks) {
        var url, t = this;
        var msgBox = null;
        url = window.location.pathname + '';//href + '';
        post = $H(post).toObject();
        post.page = 'apiCall';
        post.command = command;

        var req = new Tauri.TimeoutRequest(url, {
            postBody: Object.toQueryString(post),
            method: 'post',
            timeout: true,
            timeoutDelay : 60,

            onComplete : function(e) {
                var callback = false;
                if(callBacks.get('onComplete')) {
                    callback = callBacks.get('onComplete');
                }
                if(e.responseJSON && callback) {
                    callback(e.responseJSON,post);
                } else if(e.responseJSON){
                    return e.responseJSON;
                } else if(callback) {
                    callback(false, post);
                } 
                return false;
            }
        });

        return req;
    }
}))();



