var HttpRequest = function () {
    var http_request = null;
	this.method = 'GET';
	this.tries = 0;
	this.timeout = 10000;
    /**
     * if asynchron is false, no handler is needed.
     * the load method will return the response
     */
	this.asynchron = true;
	this.send = null; // set send to key-value pairs (urlencoded and escaped? QUERY_STRING)
	this.loading = false;

	/**
	 * Initialize HTTpRequest
	 *
	 * @param reference		Function that handles the response
	 */
	this.init = function (handler) {
		//this.leak_test_data = new Array( 20 * 0xCCCC ).join( 0xFFFFFFFF ); // create an Array of 20 MB Size
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				//http_request.overrideMimeType('text/xml');
			} // fuer IE 7
		} else if (window.ActiveXObject) { // IE
			http_request = new ActiveXObject('Microsoft.XMLHTTP');
		}
        if (handler) {
            http_request.onreadystatechange = function () {
                if (http_request.readyState == 4) {
					if (this.timeout) {
						window.clearTimeout(this.timeout);
					}
                    try {
                        if (http_request.status) {
                            throw true;
                        } else {
                            throw false;
                        }
                    } catch (e) {
                        if (e === true) {
                            if (http_request.status == 200) {
								handler(http_request.responseText, true);
							} else {
								handler(http_request.responseText);
							}
                        } else {
							/* Aborted? */
                            handler("{status:"+http_request.status+"}");
                        }
						this.loading = false;
                    }
                }
            }.bindTo(this);
        }
	};
    this.toggleSync = function () {
        this.asynchron = !this.asynchron;
    };
	/**
	 * Fire the Request
	 *
	 * @param string	url to load with parameters
	 */
	this.load = function(url) {
		//console.trace();
		this.loading = true;
		this.url = url;
		http_request.open(this.method, url, this.asynchron);
		if (this.method == 'POST') {
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", this.send.length);
			http_request.setRequestHeader("Connection", "close");
		}
		http_request.send(this.send);
		
			this.timeout = window.setTimeout(function () {
				if (this.tries < 3) {
					this.abort();
					this.load(this.url);
					this.tries++;
				}
			}.bindTo(this), 60000);
			
        if (this.asynchron === false) {
			this.loading = false;
            return http_request.responseText;
        }
        return true;
	};
    /**
     * Maybe you want to abort the request
     */
    this.abort = function () {
        http_request.abort();
		this.loading = false;
    };
};
eventHandler.addLoadEvent(function () {
    window.httpRequest = new HttpRequest();
});