(function() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("firefox/3.6b") > -1) { /* 3.6b1 */
        Ext.toArray = function(a, i, j, res) {
            res = [];
            Ext.each(a, function(v) { res.push(v); });
            return res.slice(i || 0, j || res.length);
        }
    }

    var createComplete = function(cb){
         return function(xhr, status){
            if((status == 'error' || status == 'timeout') && cb.failure){
                cb.failure.call(cb.scope||window, {
                    responseText: xhr.responseText,
                    responseXML : xhr.responseXML,
                    argument: cb.argument
                });
            }else if(cb.success){
                cb.success.call(cb.scope||window, {
                    responseText: xhr.responseText,
                    responseXML : xhr.responseXML,
                    argument: cb.argument
                });
            }
         };
    };

    Ext.lib.Ajax.request = function(method, uri, cb, data, options) {
        var o = {
            type: method,
            url: uri,
            data: data,
            timeout: cb.timeout,
            complete: createComplete(cb)
        };

        if(options){
            /* bugfix start */
            if (typeof options.async === 'boolean') o.async = options.async;
            /* bugfix end */
            var hs = options.headers;
            if(options.xmlData){
                o.data = options.xmlData;
                o.processData = false;
                o.type = (method ? method : (options.method ? options.method : 'POST'));
                if (!hs || !hs['Content-Type']){
                    o.contentType = 'text/xml';
                }
            }else if(options.jsonData){
                o.data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
                o.processData = false;
                o.type = (method ? method : (options.method ? options.method : 'POST'));
                if (!hs || !hs['Content-Type']){
                    o.contentType = 'application/json';
                }
            }
            if(hs){
                o.beforeSend = function(xhr){
                    for(var h in hs){
                        if(hs.hasOwnProperty(h)){
                            xhr.setRequestHeader(h, hs[h]);
                        }
                    }
                }
            }
        }
        jQuery.ajax(o);
    };

})();

