/**
 *
 * @file		MyAjaxObject.class.js
 * 
 * @version		1.0.0 Beta
 * @author		Sebastian Nette
 *
 * @copyright	Nette Medien 2009
 *
 *
 */

 var AjaxActiveObjectName = "";

/** creating object **/
function MyAjaxObject(ObjectName) {
	this.name = ObjectName;
	this.callback = "";
	this.response = "";
	this.http = null;
	this.stack = [];
	this.xml = 1;
	this.run = 0;
}

/** process the next request **/
MyAjaxObject.prototype.next = function() {
	if(this.stack.length > 0) {
		var item = this.stack[0];
		this.stack.shift();
		if(this.xmlhttp()) {
			this.run = 1;
			this.xml = item['xml'];
			this.callback = item['callback'];
			this.request(item['type'], item['url'], item['data']);
		}
	} else {
		this.run = 0;
	}
}

/** try to get the xmlhttprequest object **/
MyAjaxObject.prototype.xmlhttp = function () {
	if(this.http) {
		if(this.http.readyState != 0 && this.http.readyState != 4) { 
			return false; 
		}
		this.http.abort();
	} else {
		this.http = false; 
	}
	try {
		if(window.ActiveXObject) {
			for(var i = 5; i; i--) {
				try {
					if (i == 2) { 
						this.http = new ActiveXObject( "Microsoft.XMLHTTP" ); 
					} else {
						this.http = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" ); 
					}
					break;
				} catch (excNotLoadable) {
					this.http = false; 
				}
			}
		} else if(window.XMLHttpRequest) {
			this.http = new XMLHttpRequest();
		}
	} catch(excNotLoadable) {
		this.http = false;
	}
	if(this.http) {
		AjaxActiveObjectName = this.name;
		this.http.onreadystatechange = this.call_response_handler;
		return this.http;
	} else {
		return false;
	}
}

/** handle ajax response, what else? :) **/
MyAjaxObject.prototype.call_response_handler = function () {
	eval(AjaxActiveObjectName + ".handle_response()");
}

/** handle ajax response, what else? :) **/
MyAjaxObject.prototype.handle_response = function () {
	if(this.http.readyState == 4) {
		if(this.http.status && this.http.status != 200) {
			alert("Ajax error:\n Http status = " + this.http.status + "\n" + this.http.statusText); 
		} else {
			if(this.callback) {
				this.response = "";
				if(this.http.responseXML && this.xml == 1) {
					this.response = this.http.responseXML; 
				} else if(this.http.responseText && this.xml == 0) {
					this.response = this.http.responseText; 
				}
				eval(this.callback);
				this.run = 0;
			}
		}
		if(this.stack.length > 0) {
			this.next();
		}
	}
}

/** perform request **/
MyAjaxObject.prototype.request = function (type, url, data) {
	if(type == "GET") {
		this.http.open("GET", url + "&nocache=" + Math.random(), true);
		this.http.send(null);
		return true;
	} else if(type == "POST") {
		this.http.open("POST", url + (url.indexOf('?') == -1 ? '?' : '') + "&nocache=" + Math.random(), true);
		this.http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.http.send(data + "&nocache=" + Math.random());
		return true;
	} else {
		return false;
	}
}

/** open GET Request **/
MyAjaxObject.prototype.GET = function (url, callback, xml) {
	this.add("GET", url, callback, "", (typeof xml == "undefined" ? 1 : xml));
}

/** open POST Request **/
MyAjaxObject.prototype.POST = function (url, data, callback, xml) {
	this.add("POST", url, callback, data, (typeof xml == "undefined" ? 1 : xml));
}

/** add one Request to the lib **/
MyAjaxObject.prototype.add = function (type, url, callback, data, xml) {
	var item = new Object();
	item['callback'] = callback;
	item['type'] = type;
	item['data'] = data;
	item['xml'] = xml;
	item['url'] = url;
	this.stack[this.stack.length] = item;
	if(this.run == 0) {
		this.run = 1;
		this.next();
	}
}

var MyAjax = new MyAjaxObject("MyAjax");