var isIE = navigator.userAgent.indexOf("MSIE") > 0,
	isGecko = navigator.userAgent.indexOf("rv:1.") > 0,
	isWebKit = navigator.userAgent.indexOf("WebKit") > 0;

function attachEventHandler(obj, name, handler, capture) {
	if (name == "dragstart" || name == "drag" || name == "dragend") {
		if (!obj.dragdrop) {
			new DragDropHandler(obj);
		}
		obj["_on" + name] = handler;
		return;
	}
	if (obj.attachEvent) {
		obj.attachEvent("on"+name, handler);
	} else {
		obj.addEventListener(name, handler, capture);
	}
}

function detachEventHandler(obj, name, handler, capture) {
	if (name == "dragstart" || name == "drag" || name == "dragend") {
		obj["_" + name] = null;
		return;
	}
	if (obj.detachEvent) {
		obj.detachEvent("on"+name, handler);
	} else {
		obj.removeEventListener(name, handler, capture);
	}
}

function getScrollTop() {
	return document.documentElement.scrollTop || document.body.scrollTop;
}	

function getScrollLeft() {
	return document.documentElement.scrollLeft || document.body.scrollLeft;
}

function getScrollHeight() {
	return document.body.scrollHeight;
}

function getScrollWidth() {
	return document.body.scrollWidth;
}

function getWindowHeight() {
	return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}

function getWindowWidth() {
	return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}

function layout(element) {
	var left = 0,
		right = 0,
		top = 0,
		bottom = 0,
		width = 0,
		height = 0,
		nodes,
		node,
		className;
	
	if (!element) {
		element = document.body;
		width = getWindowWidth();
		height = getWindowHeight();
	} else {
		width = element.offsetWidth;
		height = element.offsetHeight;
	}

	var nodes = element.childNodes;
	
	for (var i = 0; i < nodes.length; i++) {
		node = nodes[i];
		if (node.nodeType == 1 && node.style.display != "none") {
			className = node.className.split(" ")[0];
			switch(className) {
				case "left":
					node.style.left = left + "px";
					node.style.top = top + "px";
					node.style.bottom = "auto";
					node.style.right = "auto";
					node.style.height = height + "px";
					left += node.offsetWidth;
					width -= node.offsetWidth;
					layout(node);
					break;
				case "right":
					node.style.left = "auto";
					node.style.right = right + "px";
					node.style.top = top + "px";
					node.style.bottom = "auto";
					node.style.height = height + "px";
					right += node.offsetWidth;
					width -= node.offsetWidth;
					layout(node);
					break;
				case "top":
					node.style.top = top + "px";
					node.style.left = left + "px";
					node.style.right = "auto";
					node.style.bottom = "auto";
					node.style.width = width + "px";
					top += node.offsetHeight;
					height -= node.offsetHeight;
					layout(node);
					break;
				case "bottom":
					node.style.bottom = bottom + "px";
					node.style.left = left + "px";
					node.style.right = "auto";
					node.style.top = "auto";
					node.style.width = width + "px";
					bottom += node.offsetHeight;
					height -= node.offsetHeight;
					layout(node);
					break;
				case "rest":
					node.style.left = left + "px";
					node.style.top = top + "px";
					node.style.width = width + "px";
					node.style.height = height + "px";
					node.style.right = "auto";
					node.style.bottom = "auto";
					break;
				case "float":
					break;
				default: break; // the loop
			}
			if (width < 0) width = 0;
			if (height < 0) height = 0;
		}
	}
}

var xhrs = [];
function getXHR() {
	for (var i = 0; i < xhrs.length; i++) {
		if (!xhrs[i].used) {
			xhrs[i].used = true;
			return xhrs[i];
		}
	}
	var xhr = {
		xhr: createXHR(),
		used: true
	};
	xhrs.push(xhr);
	return xhr;
}	

function releaseXHR(xhr) {
	xhr.used = false;
}	

function createXHR() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP")
	} else {
		alert("Browser is not supported");
	}
}

function parseDate(datestr) {
	var date, year, month, day, hours, minutes, seconds;
	year = parseInt(datestr.substr(0,4), 10);
	month = parseInt(datestr.substr(4,2), 10) - 1;
	day = parseInt(datestr.substr(6,2), 10);
	hours = parseInt(datestr.substr(8,2), 10);
	minutes = parseInt(datestr.substr(10,2), 10);
	seconds = parseInt(datestr.substr(12,2), 10);
	date = new Date(year, month, day, hours, minutes, seconds);
	return date;
}

function formatDate(datestr) {
	var date = parseDate(datestr),
		daysOfWeek = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
		year, month, day, dow;
		year = parseInt(datestr.substr(0,4), 10);
		month = parseInt(datestr.substr(4,2), 10);
		day = parseInt(datestr.substr(6,2), 10);
		dow = daysOfWeek[date.getDay()];
	return dow + ", " + day + "." + month + "." + year;
}

function formatDateStr(date, short) {
	var res = "";
	res += date.getUTCFullYear();
	if (date.getUTCMonth() + 1 < 10) res += "0";
	res += date.getUTCMonth() + 1;
	if (date.getUTCDate() < 10) res += "0";
	res += date.getUTCDate();
	if (!short) {
		if (date.getUTCHours() < 10) res += "0";
		res += date.getUTCHours();
		if (date.getUTCMinutes() < 10) res += "0";
		res += date.getUTCMinutes();
		if (date.getUTCSeconds() < 10) res += "0";
		res += date.getUTCSeconds();
	}
	return res;
}

function DragDropHandler(obj) {
	this.obj = obj;
	if (window != parent) {
		this.addWindow = parent;
	} else {
		this.addWindow = overviewRef.contentWindow;
	}
	
	obj.dragdrop = true;
	this.dragging = false;
	this.startX = 0;
	this.startY = 0;
	
	var handler = this;
	this.downForwarder = function(event) {
		handler.onMouseDown(event)
	};
	this.moveForwarder = function(event) {
		handler.onMouseMove(event)
	};
	this.upForwarder = function(event) {
		handler.onMouseUp(event)
	};

	attachEventHandler(obj, "mousedown", this.downForwarder, true);
}

DragDropHandler.prototype.onMouseDown = function(event) {
	this.startX = event.screenX;
	this.startY = event.screenY;
	if (document.body.setCapture) {
		attachEventHandler(document.body, "mousemove", this.moveForwarder, true);
		attachEventHandler(document.body, "mouseup", this.upForwarder, true);
		document.body.setCapture();
	} else {
		attachEventHandler(document, "mousemove", this.moveForwarder, true);
		attachEventHandler(document, "mouseup", this.upForwarder, true);
		if (this.addWindow) {	
			attachEventHandler(this.addWindow, "mousemove", this.moveForwarder, true);
			attachEventHandler(this.addWindow, "mouseup", this.upForwarder, true);
		}
	}
	if (event.preventDefault) event.preventDefault();
}

DragDropHandler.prototype.onMouseMove = function(event) {
	var deltaX = this.startX - event.screenX,
		deltaY = this.startY - event.screenY;

	if (!this.dragging && (Math.abs(deltaX) > 3 || Math.abs(deltaY) > 3)) {
		this.dragging = true;
		if (this.obj._ondragstart) this.obj._ondragstart();
	}
	
	if (this.dragging) {
		if (this.obj._ondrag) this.obj._ondrag({dx: deltaX, dy: deltaY});
	}
}

DragDropHandler.prototype.onMouseUp = function(event) {
	if (this.dragging) {
		this.dragging = false;
		if (this.obj._ondragend) this.obj._ondragend();
	} 
	if (document.body.releaseCapture) {
		detachEventHandler(document.body, "mousemove", this.moveForwarder, true);
		detachEventHandler(document.body, "mouseup", this.upForwarder, true);
		document.body.releaseCapture();
	} else {
		detachEventHandler(document, "mousemove", this.moveForwarder, true);
		detachEventHandler(document, "mouseup", this.upForwarder, true);
		if (this.addWindow) {	
			detachEventHandler(this.addWindow, "mousemove", this.moveForwarder, true);
			detachEventHandler(this.addWindow, "mouseup", this.upForwarder, true);
		}
	}
}
