try {
AjaxQueue = {
	version: "0.0.2",
	initialisationIsQueued: false,
	isInitialised: false,
	
	xmlHttp: null,
	host: "",
	url: "",
	queue: [],
	callback: "",
	isBusy: false,
	timer: null,
	
	responseXml: null,
	responseText: "",
	
	init: function() {
		AjaxQueue.host = document.location.protocol + "//" + document.location.host + "/";
		
		pathParts = document.location.pathname.split("/");
		for (i = 1; i < (pathParts.length - 1); i++) {
			AjaxQueue.url += pathParts[i] + "/";
		}
		
		AjaxQueue.createXmlHttpRequestObject();
		
		AjaxQueue.isInitialised = true;
		AjaxQueue.processQueue();
	},
	
	createXmlHttpRequestObject: function() {
		// speichert Referenz auf das XMLHttpRequest-Object
		var xmlHttp;
		
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest(); //Mozilla, Safari,Opera
		} else if (window.ActiveXObject) { 
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE 5
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE 6
				} catch (e) {
					alert(e.toString());
				}
			}
		}
		
		if (!xmlHttp) {
			alert("Error creating the XMLHttpRequest object.");
		} else {
			AjaxQueue.xmlHttp = xmlHttp;
		}
	},
	
	foo: function() {},
	
	addToQueue: function(itemPath, itemCallback, itemMethod, itemIsAsynchronous) {
		if (typeof itemMethod == "undefined" || itemMethod == null) {
			itemMethod = "GET";
		}
		
		if (typeof itemCallback == "undefined" || itemCallback== null) {
			itemCallback = "AjaxQueue.foo";
		}
		
		if (typeof itemIsAsynchronous == "undefined" || itemIsAsynchronous == null) {
			itemIsAsynchronous = true;
		}
		
		var item = {
			path: itemPath,
			method: itemMethod.toUpperCase(),
			callback: itemCallback,
			asynchronous: itemIsAsynchronous
			};
		
		AjaxQueue.queue.push(item);
		
		AjaxQueue.processQueue();
		
		return true;
	},
	
	processQueue: function() {
		if (AjaxQueue.isInitialised && !AjaxQueue.isBusy && AjaxQueue.queue.length > 0) {
			AjaxQueue.isBusy = true;
			var item = AjaxQueue.queue[0];
			AjaxQueue.queue.shift();
			AjaxQueue.processItem(item);
		}
	},
	
	processItem: function(item) {
		if (AjaxQueue.xmlHttp) {
			var url = AjaxQueue.host + AjaxQueue.url + item.path;
			
			var timestamp = new Date().getTime();
			if (url.indexOf('?') == -1) {
				url += "?_aqTimestamp=" + timestamp;
			} else {
				url += "&_aqTimestamp=" + timestamp;
			}
			
			AjaxQueue.callback = item.callback;
			
			AjaxQueue.xmlHttp.open(item.method, url, item.asynchronous);
			AjaxQueue.xmlHttp.onreadystatechange = AjaxQueue.processCallback;
			AjaxQueue.xmlHttp.send(null);
		} else {
			alert("No XMLHttpRequest-Object found");
			return false;
		}
	},
	
	processCallback: function() {
		if (AjaxQueue.xmlHttp.readyState == 4) {
			if (AjaxQueue.xmlHttp.status == 200) {
				try {
					AjaxQueue.responseXML = AjaxQueue.xmlHttp.responseXml;
				} catch (e) {
					AjaxQueue.responseXML = null;
				}
				
				try {
					AjaxQueue.responseText = AjaxQueue.xmlHttp.responseText;
				} catch (e) {
					AjaxQueue.responseText = "";
				}
				
				setTimeout(AjaxQueue.callback + "(AjaxQueue.responseText, AjaxQueue.responseXml)", 10);
				// setTimeout(AjaxQueue.callback + "()", 10);
				AjaxQueue.timer = setTimeout("AjaxQueue.processQueue()", 100);
			} else {
				alert("Error while polling data:\n" + AjaxQueue.xmlHttp.status);
			}
			
			AjaxQueue.isBusy = false;
		}
	},
	
	showQueue: function() {
		for (i = 0; i < AjaxQueue.queue.length; i++) {
			alert(AjaxQueue.queue[i].path);
		}
	},
	
	showVersion: function() {
		alert("Ajax Queue v" + AjaxQueue.version + " (c) 2009 Joern Maske <Stereoide@gmail.com>");
	}
}
} catch (e) {
	alert("error instantiating AjaxQueue:\n" + e.toString);
}

var stIsIE = /*@cc_on!@*/false;

/* for Mozilla/Opera9 */
if (document.addEventListener && !AjaxQueue.initialisationIsQueued) {
    document.addEventListener("DOMContentLoaded", AjaxQueue.init, false);
	AjaxQueue.initialisationIsQueued = true;
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	if (!AjaxQueue.initialisationIsQueued) {
		document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				AjaxQueue.init(); // call the onload handler
			}
		};
		AjaxQueue.initialisationIsQueued = true;
	}
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent) && !AjaxQueue.initialisationIsQueued) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            AjaxQueue.init(); // call the onload handler
        }
    }, 10);
	AjaxQueue.initialisationIsQueued = true;
}

/* for other browsers */
if (!AjaxQueue.initialisationIsQueued) {
	window.onload = AjaxQueue.init;
	AjaxQueue.initialisationIsQueued = true;
}
