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) {
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			http_request.overrideMimeType ? http_request.overrideMimeType('text/xml') : false; // 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) {
                            http_request.status == 200 ? handler(http_request.responseText, true) : handler(http_request.responseText);
                        } else {
                            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;
		if (this.method == 'POST') {
			http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		http_request.open(this.method, url, this.asynchron);
		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;
});