MOOsem = {
	version: "1.1.0",
	copyright: "(c) 2011 by Osem Websystems",

	imgFolder: "img",

	init: function() {
		MOOsem.readScreen();
		ulTree.init();
	},

	about: function() {
		Msg.show("MOOsem version " + this.version + " " + this.copyright);
	},

	readScreen: function() {
		Screen = {
			left: screen.left ? screen.left : window.screenLeft,
			top: screen.top ? screen.top : window.screenTop,
			width: screen.width ? screen.width : screen.availWidth,
			height: screen.height ? screen.height : screen.availHeight
		};
		if (document.body) {
			Frame.width = window.innerWidth ? window.innerWidth : document.body.clientWidth;
			Frame.height = window.innerHeight ? window.innerHeight : document.body.clientHeight;
			document.body.centreX = parseInt(document.body.offsetWidth / 2, 10);
			document.body.centreY = parseInt(document.body.offsetHeight / 2, 10);
		}
	},

	readMouse: function(e) {
		var e = e ? e : window.event || window.Event;
		Mouse = { x: e.page.x, y: e.page.y, button: e.button };
	}
}

if (typeof MooTools == "undefined") {
	alert("ERROR: mootools.js must be included.");
}

Browser.ie = Browser.Engine.trident;
Browser.mozilla = Browser.Engine.gecko;
Browser.opera = Browser.Engine.presto;
Browser.webkit = Browser.Engine.webkit;

Frame = {};

window.addEvent("resize", MOOsem.readScreen);
MOOsem.readScreen();

Mouse = { x: 0, y: 0, button: 0 };
document.addEvent("load", MOOsem.readMouse);
document.addEvent("mousemove", MOOsem.readMouse);

/* BASICS */

Element.implement({
	applyStyle: function(o, backupOnly) {
		if (typeof o == "string") o = o.css2o();
		if (typeof backupOnly == "undefined") var backupOnly = false;
		var backup = "";
		for (var x in o) if (typeof o[x] != "function") {
			backup += x + ": " + (this.style[x] ? this.style[x] : "") + ";";
			if (!backupOnly) this.style[x] = o[x];
		}
		return backup;
	},

	adjustSize: function(minW, minH, maxW, maxH) {
		if (this.offsetWidth > maxW || this.offsetHeight > maxH) {
			if (this.offsetWidth > this.offsetHeight) {
				var h = Math.round((maxW / this.offsetWidth) * this.offsetHeight);
				if (h > maxH) h = maxH;
				this.style.height = h + "px";
			} else {
				var w = Math.round((maxH / this.offsetHeight) * this.offsetWidth);
				if (w > maxW) w = maxW;
				this.style.width = w + "px";
			}
		} else if (this.offsetWidth < minW || this.offsetHeight < minH) {
			if (this.offsetWidth < this.offsetHeight) {
				var h = Math.round((minW / this.offsetWidth) * this.offsetHeight);
				if (h < minH) h = minH;
				this.style.height = h + "px";
			} else {
				var w = Math.round((minH / this.offsetHeight) * this.offsetWidth);
				if (w < minW) w = minW;
				this.style.width = w + "px";
			}
		}
		return true;
	},

	attrib: function(prop, def) {
		if (!def) var def = null;
		if (!this[prop]) this[prop] = this.getAttribute(prop) ? this.getAttribute(prop) : def;
	},

	attribFunction: function(prop) {
		if (this.getAttribute(prop)) {
			var f = this.getAttribute(prop);
			if (typeof f == "string") f = new Function(f);
			this.prop = f;
		}
	},

	getValue: function() {
		if (this.value) return this.value;
		if (this.selectedIndex) return this[this.selectedIndex].value;
		return null;
	}
});

/* ------------------------------------------------ */

function $id(prefix) {
	var id;
	do {
		id = prefix + Math.floor(Math.random() * 99999);
	} while ($(id) != null);
	return id;
}

function getProp(o, elID, failVal) {
	for (var i = 0; i <= 2; i++) {
		var id = String(elID);
		id = (i == 1) ? id.toLowerCase() : (i == 2) ? id.toUpperCase() : id;
		if (typeof o[id] == "undefined" || o[id] == null) {
			if (i == 2) return (typeof failVal == "undefined") ? null : failVal;
		} else return o[id];
	}
}

function jump(q) {
	location.replace(new URI().setData(q, true).toString());
}

/* ------------------------------------------------ */

Sort = {
	field: new URI().getData("sort"),
	direction: new URI().getData("sort_dir"),

	go: function(field) {

		var dir = (field == Sort.field) ? (Sort.direction == "ASC" ? "DESC" : "ASC") : "ASC";
		jump({ "sort": field, "sort_dir": dir });
	},

	init: function() {
		$$("*").each(function(el) {
			var field = el.getAttribute("sort");
			if (field != null) {
				if (field == Sort.field) {
					var marker = new Element("img", {
						src: "img/ico_sort_" + (Sort.direction == "ASC" ? "asc" : "desc") + ".gif"
					});
					marker.inject(el, "after");
				}

				el.setStyle("cursor", "pointer");

				el.addEvent("click", function() {
					Sort.go(this.getAttribute("sort"));
				});
			}
		});
	}
};

window.addEvent("domready", Sort.init);

/* ------------------------------------------------ */

Search = {
	txtSearch: "Search...",
	txtTooShort: "Enter at least ~Search.minimumLength~ characters.",
	minimumLength: 3,
	targetFrame: "_self",

	keyHandler: function(e) {
		e = e ? e : window.event || window.Event;
		if (e.code == 13) Search.go(e.target ? e.target : e.srcElement ? e.srcElement : null);
	},

	go: function(el) {
		if (el == null) el.value = "";
		if (el.value == "" || el.value.length >= Search.minimumLength) window.open(new URI(el.getAttribute("url") || location.href).setData({ search: el.value }, true).toString(), this.targetFrame); else alert(Search.txtTooShort.parseScriptTags());
	},

	init: function() {
		$$(".search").each(function(field) {
			if (field.nodeName.toUpperCase() == "INPUT") {
				field.value = Search.txtSearch.parseScriptTags();
				field.addEvent("focus", function() {
					this.select();
				});
				field.addEvent("keypress", Search.keyHandler);
			}
		});

		$$("button").each(function(button) {
			var field = button.getAttribute("field");
			if (field != null) {
				button.addEvent("click", function() {
					Search.go($(this.getAttribute("field")));
				});
			}
		});
	}
};

window.addEvent("domready", Search.init);

/* ------------------------------------------------ */

window.pop = function(p) {
	if (!p.url) return null;
	if (!p.target) p.target = "_blank";
	if (!p.width) p.width = 300;
	if (!p.height) p.height = 200;
	if (!p.position) p.position = "centre";
	if (typeof p.position == "object") {
		var c = p.position.getCoordinates();
		p.x = c.x;
		p.y = c.y;
	} else {
		p.position = p.position.toLowerCase();
		if (p.position == "centre" || p.position == "center") {
			p.x = parseInt(Screen.width / 2, 10) - parseInt(p.width / 2, 10);
			p.y = parseInt(Screen.height / 2, 10) - parseInt(p.height / 2, 10);
		} else {
			if (!p.x) p.x = 0;
			if (!p.y) p.y = 0;
		}
	}
	if (p.passCoordinates) p.url += (p.url.indexOf("?") == -1 ? "?" : "&") + "x=" + p.x + "&y=" + p.y + "&width=" + p.width + "&height=" + p.height;
	var par = "";
	for (var x in p) if (typeof p[x] != "function") par += (par == "" ? "" : ", ") + (x == "x" ? "left" : x == "y" ? "top" : x) + "=" + (typeof p[x] == "boolean" ? (p[x] ? "yes" : "no") : p[x]);
	return window.open(p.url, p.target, par);
}

/* ------------------------------------------------ */

/* STRING */

String.implement({
	css2o: function() {
		var o = {};
		var a = this.split(";");
		for (var i = 0; i < a.length; i++) {
			var b = a[i].split(":");
			if (b.length == 2) o[String(b[0]).trim()] = String(b[1]).trim();
		}
		return o;
	},

	escQuotes: function() {
		return this.replace(/"/g, "\\\"");
	},

	enquote: function() {
		var s = String(this);
		if (s.indexOf("'") == -1) return "'" + s + "'";
		if (s.indexOf("\"") == -1) return "\"" + s + "\"";
		return "\"" + s.escQuotes() + "\"";
	},

	parseScriptTags: function() {
		return this.replace(/~(.+?)~/g, function(p0, p1) {
			return eval(p1);
		});
	}
});

/* NUMBER */

byteMeasures = ["b", "kb", "MB", "GB", "TB"];

Number.implement({
	formatByteSize: function() {
		var i = 0;
		var x = this;
		do {
			x = Math.round((100 * x / 1024) / 100);
			i++;
		} while (x >= 1024 && i < byteMeasures.length);
		if (x == 0) x = 1;
		return x + " " + byteMeasures[i];
	}
});

/* SEND POST DATA */

function post(p) {
	if (typeof p == "undefined") return null;
	var data = p.data ? p.data : null;

	var form = document.createElement("form");
	form.method = "post";
	form.action = p.url ? p.url : "";
	form.target = p.target ? p.target : "_self";
	for (var x in data) {
		var field = document.createElement("input");
		field.setAttribute("name", x);
		field.setAttribute("value", data[x]);
		form.appendChild(field);
	}
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form);
}

/* HTTP REQUEST */

Req = {
	reqs: [],
	useDefaultSpinner: true,
	spinnerHTML: "",
	spinnerTarget: null,
	appendLoadingHTML: false,
	debug: false,

	getInstance: function(method) {
		if (typeof method == "undefined") var method = "";
		var req;
		if (method == "IFRAME") {
			req = new IFrame({
				src: "about:blank",
				styles: {
					width: 0,
					height: 0,
					visibility: "hidden",
					border: 0
				}
			}).inject(document.body);
			return req;
		} else if (window.XMLHttpRequest) {
			try {
				req = new XMLHttpRequest();
				if (req.overrideMimeType) req.overrideMimeType("text/html");
				return req;
			} catch(err) {
				return null;
			}
		} else if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(err) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(err) {
					return null;
				}
			}
		}
		return null;
	},

	spinner: function(p) {
		if (typeof p == "undefined") return null;
		if (typeof p != "object") p = { container: p };
		if (typeof Spinners == "undefined") Spinners = {};
		if (p.id) p = Spinners[p.id]; else {
			p.id = $id("spinner");
			if (!p.size) p.size = 3;
			if (!p.interval) p.interval = 100;
			if (!p.style) p.style = "font-size: 13px; color: #777777; padding-left: 2px";
			if (!p.highlightStyle) p.highlightStyle = "color: #FC060E";
			if (!p.symbol) p.symbol = "&#8226;";
			if (!p.container) p.container = document.body;
			p.position = -1;
			p.stop = function() {
				clearInterval(Spinners[p.id].timer);
				Spinners[p.id] = null;
			}
			Spinners[p.id] = p;
			if (false) p.container.empty().grab(
				new Element("span", { id: p.id })
			);
			p.container.set("html", "<span id='" + p.id + "' style='" + p.style + "'></span>");
			//p.container.innerHTML = (p.append ? p.container.innerHTML : "") + "<span id='" + p.id + "' style='" + p.style + "'></span>";
			p.el = $(p.id);
			p.timer = setInterval("Req.spinner({ id: '" + p.id + "' });", p.interval);
			return Spinners[p.id];
		}
		if (p == null || !p.id) return null;
		if (p.el == null) p.stop();
		if (p.position == p.size) p.position = 0; else p.position++;
		var s = "";
		for (var i = 0; i < p.size; i++) {
			s += "<span" + (i == p.position ? " style='" + p.highlightStyle + "'" : "") + ">" + p.symbol + "</span>";
		}
		p.el.innerHTML = s;
	},

	request: function(p) {
		if (typeof p != "object") return {};
		var method = String(p.method).toUpperCase();
		var req = this.getInstance(method);
		if (req == null) return {};
		p.isReq = true;
		p.req = req;
		if (!p.spinnerHTML) p.spinnerHTML = this.spinnerHTML;
		if (typeof p.useDefaultSpinner == "undefined") p.useDefaultSpinner = (this.useDefaultSpinner && p.spinnerHTML == "");
		if (!p.spinnerTarget) p.spinnerTarget = this.spinnerTarget ? this.spinnerTarget : p.target ? p.target : null;
		if (typeof p.appendSpinnerHTML == "undefined") p.appendSpinnerHTML = this.appendSpinnerHTML;
		if (!p.url) return false;
		var url = String(p.url);
		p.error = { code: 0, description: "" };

		var el = $(p.spinnerTarget);
		if (el != null) {
			if (p.useDefaultSpinner && typeof el.value == "undefined") p.spinner = this.spinner({ container: el, append: p.appendSpinnerHTML }); else {
				var x = p.spinnerHTML;
				if (el.value) x = x.replace(/<[^>]*>/g, "");
				if (x == "") x = "LOADING...";
				if (typeof el.value != "undefined") {
					el.origHTML = el.value;
					el.value = x;
				} else {
					el.origHTML = el.innerHTML;
					el.set("html", (p.appendSpinnerHTML ? el.origHTML : "") + x);
				}
				p.loadEl = el;
			}
		}

		var reqID = Req.reqs.length;
		Req.reqs[reqID] = p;

		var f = new Function("Req.done(" + reqID + ");");

		if (method == "IFRAME") {
			req.addEvent("load", f);
		} else {
			req.onreadystatechange = f;
			try {
				req.open(method, url, true);
			} catch(err) {
				this.throwError(reqID, "<b>AHAH error:</b> invalid URL (" + url + ")");
			}
		}
		switch(method) {
			case "GET":
				req.send("");
				break;
			case "POST":
				var data = String(p.data);
				req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				req.setRequestHeader("Content-length", data.length);
				req.setRequestHeader("Connection", "close");
				req.send(data);
				break;
			case "IFRAME":
				p.form.method = "post";
				p.form.target = req.id;
				p.form.submit();
				break;
			default: return {};
		}
		this.func(p, "onStart");
		return p;
	},

	get: function(p) {
		p.method = "GET";
		return this.request(p);
	},

	post: function(p) {
		var data = p.data ? p.data : p.form ? p.form : null;
		if (typeof data == "object") {
			var s = "";
			if ($type(data) == "element" && data.get("tag") == "form" && data.elements) {
				p.form = data;
				if (!p.url && p.form.action) p.url = p.form.action;
				if (p.form.enctype.toLowerCase() == "multipart/form-data") {
					p.method = "IFRAME";
					return this.request(p);
				} else for (var i = 0; i < p.form.elements.length; i++) if (p.form.elements[i].name) {
					var el = p.form.elements[i];
					var type = el.type;
					if (type == "submit" || type == "reset" || type == "button") continue;
					if ((type == "radio" || type == "checkbox") && !p.form.elements[i].checked) continue;
					var val = (type == "select") ? el[el.selectedIndex].value : el.value;
					s += (s == "" ? "" : "&") + el.name + "=" + encodeURIComponent(String(val));
				}
			} else for (var x in data) if (typeof data[x] != "function") {
				s += (s == "" ? "" : "&") + x + "=" + encodeURIComponent(String(data[x]));
			}
			data = s;
		}
		p.method = "POST";
		p.data = data;
		return this.request(p);
	},

	done: function(reqID) {
		var o = Req.reqs[reqID];
		if (this.debug) Msg.show("url: " + o.url + "<br>state: " + o.req.readyState);
		if (o.req.readyState == 4 || o.req.readyState == "loaded" || o.req.readyState == "complete" || o.req.contentDocument) {
			if (o.spinner) o.spinner.stop();
			if (o.loadEl && (o.loadingTarget != o.target || !o.target)) {
				if (o.loadEl.value) o.loadEl.value = o.loadEl.origHTML; else o.loadEl.innerHTML = o.loadEl.origHTML;
			}
			o.output = (o.method == "IFRAME") ? (o.req.contentDocument ? o.req.contentDocument.body.innerHTML : o.req.contentWindow.document.body.innerHTML) : o.req.responseText;
			if (!this.catchErrorTag(reqID) && (o.method == "IFRAME" || o.req.status == 200 || o.req.status == 0)) {
				var doScript = (typeof o.ignoreScript == "undefined" || o.ignoreScript == false);
				if (doScript) {
					var a = o.output.replace(/\n/g, "~n~").replace(/\r/g, "~r~").match(/<script .*?>(.*?)<\/script>/gi);
					var script = "";
					if (a != null) {
						for (var i = 0; i < a.length; i++) script += a[i].replace(/<\/?script.*?>/gi, "\n");
						script = script.replace(/~n~/g, "\n").replace(/~r~/g, "\r").replace(/function ([^\(]+)/gi, ";$1 = function");
					}
				}
				var s = getProp(o, "onLoadMsg", o.output);
				var el = $(o.target);
				if (el != null) {
					if (typeof el.value == "undefined") el.innerHTML = s; else el.value = s;
				}
				if (doScript) window.eval(script);
				this.func(o, "onLoad");
				var redir = getProp(o, "onLoadRedir", getProp(o, "redir", ""));
				if (redir != "") window.open(redir, "_self");
			} else this.throwError(reqID);
		}
	},

	catchErrorTag: function(reqID) {
		var o = Req.reqs[reqID];
		if (!o.req) return false;
		if (o.output == "") return false;
		var a = o.output.match(/<error(.*?)?>(.+?)<\/error>/i);
		if (a != null && typeof a.length != "undefined" && a.length >= 3) {
			o.error.code = parseFloat(String(a[1]).replace(/ /g, ""));
			if (isNaN(o.error.code)) o.error.code = -1;
			o.error.description = a[2];
			return true;
		}
		return false;
	},

	throwError: function(reqID, msg) {
		var o = Req.reqs[reqID];
		if (!this.catchErrorTag(reqID)) {
			o.error.code = o.req.status;
			try {
				o.error.description = getProp(o.req, "statusText", "");
			} catch(err) {}
		}
		if (o.error.code == 500) o.error.description += "<br>" + o.req.responseText;
		o.error.url = o.url;
		if (typeof msg == "undefined") var msg = "<b>AHAH error " + o.error.code + ":</b> " + o.error.description + "<br>URL: " + o.error.url;
		if (o.onErrorMsg) msg = o.onErrorMsg.replace(/{code}/gi, code).replace(/{description}/gi, desc).replace(/{msg}/gi, msg);
		msg = msg.replace(/\n/g, "<br>");
		var el = $(o.target);
		if (el == null || el.value) {
			if (el != null && el.value) el.value = el.origHTML;
			Msg.show(msg);
		} else el.innerHTML = msg;
		this.func(o, "onError");
		var redir = getProp(o, "onErrorRedir", "");
		if (redir != "") window.open(redir, "_self");
	},

	func: function(o, name) {
		if (o[name]) {
			if (typeof o[name] != "function") o[name] = new Function(o[name]);
			o[name](o);
		}
	}
};

/* DRAG & DROP */

DragDrop = {
	opacity: 50,
	revertStep: 100,
	dragOverStyle: "background: #C5E1F3",

	init: function() {
		if (!this.initialised) {
			this.div = new Element("div", {
				styles: {
					position: "absolute",
					left: 0,
					top: 0,
					zIndex: 999,
					fontSize: "12px",
					visibility: "hidden"
				}
			}).inject(document.body);

			this.idCounter = 0;
			this.drag = null;
			this.dragging = false;

			this.oDragOverStyle = this.dragOverStyle.css2o();

			document.addEvent("mousemove", this.updateDrag);
			document.addEvent("mouseup", this.drop);
			document.addEvent("selectstart", new Function("return !DragDrop.dragging"));	// -- Prevent select when dragging in IE

			this.initialised = true;
		}
		this.initItems();
	},

	initItems: function() {
		DragDrop.draggables = $$(".draggable");
		for (var i = 0; i < DragDrop.draggables.length; i++) if (!DragDrop.draggables[i].dragInitialised) {
			var el = DragDrop.draggables[i];
			el.dragInitialised = true;

			if (!el.getAttribute("ddID")) {
				el.ddID = "ddItem" + DragDrop.idCounter;
				DragDrop.idCounter++
			}

			el.attrib("dragHTML");
			if (el.dragHTML == null) switch(el.get("tag")) {
				case "img": el.dragHTML = "<img src='" + el.src + "'><br>" + el.title.replace(/\n(.)+/g, ""); break;
				case "input": el.dragHTML = "<input type='" + el.type + "' value='" + el.value + "'>"; break;
				case "table": case "div": case "span": el.dragHTML = "<" + el.nodeName + ">" + el.innerHTML + "</" + el.nodeName + ">"; break;
				default: el.dragHTML = el.innerHTML;
			}

			el.attribFunction("onDrag");
			el.attribFunction("onDragStart");
			el.attribFunction("onDragEnd");
			el.attribFunction("onDrop");

			el.addEvent("mousedown", DragDrop.startDrag);
			el.addEvent("dragstart", DragDrop.startDrag);
		}

		DragDrop.dropzones = $$(".dropzone");
		for (var i = 0; i < DragDrop.dropzones.length; i++) if (!DragDrop.dropzones[i].dzInitialised) {
			var el = DragDrop.dropzones[i];
			el.dzInitialised = true;

			if (!el.getAttribute("ddID")) {
				el.ddID = "ddItem" + DragDrop.idCounter;
				DragDrop.idCounter++
			}

			el.dropzoneNr = i;

			el.attrib("dragOverStyle");
			el.attribFunction("onDragOver");
			el.attribFunction("onDragOut");

			el.savedStyle = el.applyStyle(el.dragOverStyle ? el.dragOverStyle : DragDrop.oDragOverStyle, true);

			el.addEvent("mouseover", DragDrop.enterDropZone);
			el.addEvent("mouseout", DragDrop.leaveDropZone)
		}

		if (DragDrop.onLoad) DragDrop.onLoad();
	},

	refresh: function() {
		DragDrop.initItems();
	},

	startDrag: function(e) {
		if (DragDrop.dragging) return false;
		var e = e ? e : window.event || window.Event;
		var el = e.srcElement ? e.srcElement : e.target;
		while (!el.dragHTML && (el.parentNode || el.parentElement)) el = el.parentNode ? el.parentNode : el.parentElement;

		var pos = el.getPosition();
		el.origX = pos.x;
		el.origY = pos.y;
		DragDrop.drag = el;

		var bodyScroll = getScroll();
		DragDrop.startX = e.pageX ? e.pageX : e.clientX + bodyScroll.x;
		DragDrop.startY = e.pageY ? e.pageY : e.clientY + bodyScroll.y;

		e.stop();
		return true;
	},

	updateDrag: function(e) {
		if (DragDrop.drag == null) return false;
		if (Mouse.x == DragDrop.startX && Mouse.y == DragDrop.startY) {
			DragDrop.drag = null;
			return false;
		}

		if (!DragDrop.dragging && DragDrop.drag != null) {
			DragDrop.setTransparency(true);
			DragDrop.drag.dropped = false;
			DragDrop.x = -1;
			DragDrop.y = -1;
			DragDrop.width = DragDrop.drag.offsetWidth;
			DragDrop.height = DragDrop.drag.offsetHeight;
			DragDrop.div.innerHTML = DragDrop.drag.dragHTML;
			document.body.style.cursor = "move";
			DragDrop.dragging = true;

			if (DragDrop.onDragStart) DragDrop.onDragStart(DragDrop.drag);
			if (DragDrop.drag.onDragStart) DragDrop.drag.onDragStart(DragDrop.drag);
		}

		if (!DragDrop.drag.dropped) {
			DragDrop.div.style.left = Mouse.x + 14;
			DragDrop.div.style.top = Mouse.y;
			DragDrop.div.style.visibility = "visible";

			var pos = DragDrop.div.getPosition();
			DragDrop.x = pos.x;
			DragDrop.y = pos.y;

			if (DragDrop.onDrag) DragDrop.onDrag(DragDrop.drag);
			if (DragDrop.drag.onDrag) DragDrop.drag.onDrag(DragDrop.drag);
		}
	},

	endDrag: function() {
		try {
			clearTimeout(DragDrop.revertTimer);
		} catch(err) {}
		DragDrop.div.style.visibility = "hidden";
		DragDrop.setTransparency(false);
		
		if (DragDrop.onDragEnd) DragDrop.onDragEnd(DragDrop.drag);
		if (DragDrop.drag.onDragEnd) DragDrop.drag.onDragEnd(DragDrop.drag);

		DragDrop.x = -1;
		DragDrop.y = -1;
		DragDrop.width = 0;
		DragDrop.height = 0;
		DragDrop.drag = null;
		DragDrop.inDropZone = null;

		document.body.style.cursor = "";

		DragDrop.dragging = false;
	},

	enterDropZone: function(e) {
		if (!DragDrop.dragging) return false;
		var e = e ? e : window.event || window.Event;
		var el = e.srcElement ? e.srcElement : e.target;
		while (!el.ddID && (el.parentNode || el.parentElement)) el = el.parentNode ? el.parentNode : el.parentElement;
		if (el.ddID == DragDrop.drag.ddID) return false;
		DragDrop.inDropZone = el.dropzoneNr;
		el.applyStyle(el.dragOverStyle ? el.dragOverStyle : DragDrop.oDragOverStyle);
		if (DragDrop.onDragOver) DragDrop.onDragOver(DragDrop.drag, el);
		if (el.onDragOver) el.onDragOver(DragDrop.drag, el);
	},

	leaveDropZone: function(e) {
		if (!DragDrop.dragging) return false;
		var e = e ? e : window.event || window.Event;
		var el = e.srcElement ? e.srcElement : e.target;
		while (!el.ddID && (el.parentNode || el.parentElement)) el = el.parentNode ? el.parentNode : el.parentElement;
		if (el.ddID == DragDrop.drag.ddID) return false;
		DragDrop.inDropZone = null;
		el.applyStyle(el.savedStyle);
		if (DragDrop.onDragOut) DragDrop.onDragOut(DragDrop.drag, el);
		if (el.onDragOut) el.onDragOut(DragDrop.drag, el);
	},

	drop: function(e) {
		var e = e ? e : window.event || window.Event;

		if (DragDrop.dragging) {
			DragDrop.drag.dropped = true;

			if (DragDrop.inDropZone == null) DragDrop.revert(); else {
				var el = e.srcElement ? e.srcElement : e.target;
				while (!el.dzInitialised && (el.parentNode || el.parentElement)) el = el.parentNode ? el.parentNode : el.parentElement;
				if (el.dzInitialised) {
					el.applyStyle(el.savedStyle);
					DragDrop.dropzone = el;
				}
				if (DragDrop.onDrop || DragDrop.drag.onDrop) {
					if (DragDrop.onDrop || DragDrop.drag.onDrop) DragDrop.div.style.visibility = "hidden";
					if (DragDrop.onDrop) DragDrop.onDrop(DragDrop.drag, DragDrop.dropzone);
					if (DragDrop.drag.onDrop) DragDrop.drag.onDrop(DragDrop.drag, DragDrop.dropzone);
				}
				DragDrop.endDrag();
			}
		} else if (DragDrop.drag != null) DragDrop.endDrag();
	
		if (e.preventDefault) e.preventDefault();
		if (e.stopPropagation) e.stopPropagation();
		e.returnValue = false;
		e.cancelBubble = true;
		return false;
	},

	revert: function(initDone) {
		if (typeof initDone == "undefined") var init = true;
		var dragScroll = DragDrop.drag.getScroll();
		var origX = DragDrop.drag.origX - dragScroll.x;
		var origY = DragDrop.drag.origY - dragScroll.y;
		if (init) {
			var distX = Math.abs(DragDrop.x - origX);
			var distY = Math.abs(DragDrop.y - origY);
			var xIsLonger = distX > distY;
			var longestDist = xIsLonger ? distX : distY;
			DragDrop.revertInterval = 20 - parseInt((longestDist / Screen.width) * 15, 10);
			if (xIsLonger) {
				DragDrop.revertStepX = (DragDrop.revertStep > distX) ? distX : DragDrop.revertStep;
				DragDrop.revertStepY = parseInt((distY / distX) * DragDrop.revertStepX, 10);
			} else {
				DragDrop.revertStepY = (DragDrop.revertStep > distY) ? distY : DragDrop.revertStep;
				DragDrop.revertStepX = parseInt((distX / distY) * DragDrop.revertStepY, 10);
			}
		}
		var stepX = DragDrop.revertStepX;
		var stepY = DragDrop.revertStepY;

		if (DragDrop.x < origX) {
			DragDrop.x += stepX;
			if (DragDrop.x > origX) DragDrop.x = origX;
		} else if (DragDrop.x > origX) {
			DragDrop.x -= stepX
			if (DragDrop.x < origX) DragDrop.x = origX;
		}
		DragDrop.div.style.left = DragDrop.x;

		if (DragDrop.y < origY) {
			DragDrop.y += stepY;
			if (DragDrop.y > origY) DragDrop.y = origY;
		} else if (DragDrop.y > origY) {
			DragDrop.y -= stepY;
			if (DragDrop.y < origY) DragDrop.y = origY;
		}
		DragDrop.div.style.top = DragDrop.y;

		if (DragDrop.x == origX || DragDrop.y == origY) DragDrop.endDrag(); else DragDrop.revertTimer = setTimeout("DragDrop.revert(true)", DragDrop.revertInterval);
	},

	setTransparency: function(on) {
		if (this.drag) this.drag.setOpacity(on ? this.opacity : 100);
	}
}

/* PROGRESS BAR */

function ProgressBar(id) {
	if (typeof id == "undefined") this.id = $id("progbar"); else this.id = id;
	this.width = 0;
	this.height = 0;
	this.x = -1;
	this.y = -1;
	this.colour = "";
	this.fillColour = "blue";
	this.text = "";
	this.textSize = 0;
	this.textColour = "white";
	this.activeText = "";
	this.opacity = 100;
	this.hideWhenFinished = true;
	this.hideByFading = true;
	this.fadeDelay = 20; // milliseconds
	this.fadeStep = 5;

	eval(this.id + "_globVar = this;");

	this.init = function() {
		if (this.x == -1) this.x = document.body.centreX - parseInt(this.width / 2, 10);
		if (this.y == -1) this.y = document.body.centreY - parseInt(this.height / 2, 10);
		if (this.textSize == 0) this.textSize = this.height;
		this.origOpacity = this.opacity;
		document.write("<div id='" + this.id + "' style='position: absolute; left: " + this.x + "; top: " + this.y + "; width: " + this.width + "px; height: " + this.height + "px; background-color: " + this.colour + "; font-size: " + this.textSize + "; color: " + this.fillColour + "'><span id='" + this.id + "_text' style='width: " + this.width + "px; overflow: hidden'>" + this.text + "</span>" + (this.text == "" ? "" : "<br>") + "<div style='border: 1px solid " + this.fillColour + "; padding: 1px; margin: 2px'><div id='" + this.id + "_filler' style='background-color: " + this.fillColour + "; width: 0; height: " + this.height + "px; font-size: " + this.textSize + "px; color: " + this.textColour + "; overflow-x: hidden'></div></div></div>");
		this.divShell = $(this.id);
		this.divFiller = $(this.id + "_filler");
		this.divText = $(this.id + "_text");
		this.setOpacity();
		this.activeText = this.text;
	}

	this.setOpacity = function(value) {
		if (typeof value == "undefined") var value = this.opacity;
		if (value < 0) value = 0;
		if (value == 0) this.divShell.style.display = "none"; else this.divShell.setOpacity(value);
		this.opacity = value;
	}

	this.set = function(newNr, finNr) {
		if (typeof finNr == "undefined") var finNr = 100;
		var newPerc = (finNr == 0) ? 100 : parseInt((newNr / finNr) * 100, 10);
		this.divFiller.style.width = newPerc + "%";
		this.divFiller.innerHTML = newPerc + "%";
		if (this.text != this.activeText) {
			this.divText.innerHTML = this.text;
			this.activeText = this.text;
		}
		if (this.hideWhenFinished && newNr >= finNr) this.hide();
	}

	this.show = function() {
		this.divShell.style.display = "";
		this.setOpacity(this.origOpacity);
	}

	this.hide = function() {
		if (this.hideByFading) {
			if (this.opacity > 0) {
				this.opacity -= this.fadeStep;
				this.setOpacity();
				if (this.opacity > 0) setTimeout(this.id + "_globVar.hide()", this.fadeDelay);
			}
		} else this.divShell.style.display = "none";
	}
}

/* MSG */

Msg = {
	x: 0,
	y: 0,

	init: function() {
		if (this.inner) return true;
		this.w = 200;

		this.outer = new Element("div", {
			styles: {
				position: "absolute",
				display: "block",
				left: this.x,
				top: 100,
				width: this.w + "px",
				background: "#666666",
				visibility: "hidden"
			}
		});

		this.inner = new Element("div", {
			styles: {
				position: "relative",
				left: "-3px",
				top: "-3px",
				padding: "3px",
				background: "#DFDFDF url(img/bg_msg.jpg) repeat-y",
				border: "1px outset",
				fontSize: "12px",
				textAlign: "center",
				overflow: "hidden"
			}
		});

		document.body.appendChild(this.outer);
		this.outer.appendChild(this.inner);

		this.testcontainer = new Element("div", {
			styles: {
				position: "relative",
				width: this.w + "px",
				height: 0,
				background: "#666666",
				padding: "3px",
				border: "1px outset",
				fontSize: "12px",
				visibility: "hidden"
			}
		});
		this.testtable = new Element("table");
		this.testtr = new Element("tr");
		this.tester = new Element("td");

		this.testtr.appendChild(this.tester);
		this.testtable.appendChild(this.testtr);
		this.testcontainer.appendChild(this.testtable);
		document.body.appendChild(this.testcontainer);

		this.set("x");
	},

	set: function(prop, val) {
		this.init();
		if (typeof this[prop] != "undefined") {
			if (!val) switch(prop) {
				case "x":
					var val = document.body.centreX - parseInt(this.w / 2, 10);
					break;
				case "w":
					var val = 200;
					break;
			}
			this[prop] = val;
			switch(prop) {
				case "x":
					try {
						this.outer.setStyle("left", val);
					}
					catch(err) { alert(err.message); }
					break;
				case "w":
					if (val < 60) val = 60;
					this.outer.setStyle("width", val + "px");
					this.set("x");
					break;
			}
		}
	},

	show: function(s) {
		this.init();
		this.inner.innerHTML = s + "<br><br><center><button type='button' onClick='Msg.hide()'>ok</button></center>";
		this.tester.innerHTML = this.inner.innerHTML;
		this.set("w", this.tester.offsetWidth);
		this.outer.style.visibility = "visible";
	},

	hide: function() {
		this.outer.style.visibility = "hidden";
	}
};

/* UL TREE */

ulTree = {
	leafImg: "leaf.gif",
	folderImg: "closed.gif",

	init: function(o) {
		if (typeof o == "undefined") {
			var uls = $$(".ultree");
			uls.each(function(ul) { ulTree.init({ ul: ul }); });
			return;
		}
		if (!o.nest) o.nest = 0;
		o.ul.style.fontSize = "11px";
		o.ul.style.paddingLeft = "16px";
		o.ul.style.marginLeft = 0;
		var n = o.ul.childNodes;
		var li = [];
		for (var i = 0; i < n.length; i++) if ($(n[i]).nodeName.toLowerCase() == "li") li.push(n[i]);
		for (var i = 0; i < li.length; i++) {
			li[i].style.listStyleType = "none";
			li[i].style.marginLeft = 0;
			li[i].style.marginBottom = 0;
			var n = li[i].childNodes;
			var ul = [];
			for (var j = 0; j < n.length; j++) if ($(n[j]).nodeName.toLowerCase() == "ul") ul.push(n[j]);
			var isLeaf = true;
			for (var j = 0; j < ul.length; j++) {
				ulTree.init({ ul: ul[j], nest: o.nest + 1 });
				isLeaf = false;
			}
			var img = new Element("img", {
				src: MOOsem.imgFolder + "/" + (isLeaf ? this.leafImg : this.folderImg),
				styles: { paddingRight: "4px" }
			});
			li[i].insertBefore(img, li[i].firstChild);

		}
	}
};

window.addEvent("domready", MOOsem.init);
