var MooTreeIcon = [ "I", "L", "Lminus", "Lplus", "Rminus", "Rplus", "T",
		"Tminus", "Tplus", "_closed", "_doc", "_open", "minus", "plus" ];
var MooTreeControl = new Class( {
	initialize : function(b, a) {
		a.control = this;
		a.div = b.div;
		this.root = new MooTreeNode(a);
		this.index = new Object();
		this.enabled = true;
		this.theme = b.theme;
		this.loader = b.loader;
		this.selected = null;
		this.mode = b.mode;
		this.grid = b.grid;
		this.onExpand = b.onExpand || new Function();
		this.onSelect = b.onSelect || new Function();
		this.onClick = b.onClick || new Function();
		this.root.update(true)
	},
	insert : function(a) {
		a.control = this;
		return this.root.insert(a)
	},
	select : function(a) {
		this.onClick(a);
		a.onClick();
		if (this.selected === a) {
			return
		}
		if (this.selected) {
			this.selected.select(false);
			this.onSelect(this.selected, false)
		}
		this.selected = a;
		a.select(true);
		this.onSelect(a, true)
	},
	expand : function() {
		this.root.toggle(true, true)
	},
	collapse : function() {
		this.root.toggle(true, false)
	},
	get : function(a) {
		return this.index[a] || null
	},
	adopt : function(b, a) {
		if (a === undefined) {
			a = this.root
		}
		this.disable();
		this._adopt(b, a);
		a.update(true);
		$(b).destroy();
		this.enable()
	},
	_adopt : function(a, j) {
		e = $(a);
		var h = 0, o = e.getChildren();
		for (h = 0; h < o.length; h++) {
			if (o[h].nodeName == "LI") {
				var b = {
					text : ""
				}, k = "", f = null, g = null;
				var d = 0, m = 0, l = null, r = o[h].getChildren();
				for (d = 0; d < r.length; d++) {
					switch (r[d].nodeName) {
					case "A":
						for (m = 0; m < r[d].childNodes.length; m++) {
							l = r[d].childNodes[m];
							switch (l.nodeName) {
							case "#text":
								b.text += l.nodeValue;
								break;
							case "#comment":
								k += l.nodeValue;
								break
							}
						}
						b.data = r[d].getProperties("href", "target", "title",
								"name");
						break;
					case "UL":
						g = r[d];
						break
					}
				}
				if (b.label != "") {
					b.data.url = b.data.href;
					if (k != "") {
						var q = k.split(";");
						for (m = 0; m < q.length; m++) {
							var p = q[m].trim().split(":");
							if (p.length == 2) {
								b[p[0].trim()] = p[1].trim()
							}
						}
					}
					f = j.insert(b);
					if (g) {
						this._adopt(g, f)
					}
				}
			}
		}
	},
	disable : function() {
		this.enabled = false
	},
	enable : function() {
		this.enabled = true;
		this.root.update(true, true)
	}
});
var MooTreeNode = new Class(
		{
			initialize : function(a) {
				this.text = a.text;
				this.id = a.id || null;
				this.nodes = new Array();
				this.parent = null;
				this.target = a.target;
				this.href = a.href;
				this.last = true;
				this.control = a.control;
				this.selected = false;
				this.color = a.color || null;
				this.data = a.data || {};
				this.onExpand = a.onExpand || new Function();
				this.onSelect = a.onSelect || new Function();
				this.onClick = a.onClick || new Function();
				this.open = a.open ? true : false;
				this.icon = a.icon;
				this.openicon = a.openicon || this.icon;
				if (this.id) {
					this.control.index[this.id] = this
				}
				this.div = {
					main : new Element("div").addClass("mooTree_node"),
					indent : new Element("div"),
					gadget : new Element("div"),
					icon : new Element("div"),
					text : new Element("div").addClass("mooTree_text"),
					sub : new Element("div")
				};
				this.div.main.adopt(this.div.indent);
				this.div.main.adopt(this.div.gadget);
				this.div.main.adopt(this.div.icon);
				this.div.main.adopt(this.div.text);
				$(a.div).adopt(this.div.main);
				$(a.div).adopt(this.div.sub);
				this.div.gadget._node = this;
				this.div.gadget.onclick = this.div.gadget.ondblclick = function() {
					this._node.toggle()
				};
				this.div.icon._node = this.div.text._node = this;
				this.div.icon.onclick = this.div.icon.ondblclick = this.div.text.onclick = this.div.text.ondblclick = function() {
					this._node.control.select(this._node)
				}
			},
			insert : function(a) {
				a.div = this.div.sub;
				a.control = this.control;
				var b = new MooTreeNode(a);
				b.parent = this;
				var c = this.nodes;
				if (c.length) {
					c[c.length - 1].last = false
				}
				c.push(b);
				b.update();
				if (c.length == 1) {
					this.update()
				}
				if (c.length > 1) {
					c[c.length - 2].update(true)
				}
				return b
			},
			remove : function() {
				var a = this.parent;
				this._remove();
				a.update(true)
			},
			_remove : function() {
				var b = this.nodes;
				while (b.length) {
					b[b.length - 1]._remove()
				}
				delete this.control.index[this.id];
				this.div.main.destroy();
				this.div.sub.destroy();
				if (this.parent) {
					var a = this.parent.nodes;
					a.erase(this);
					if (a.length) {
						a[a.length - 1].last = true
					}
				}
			},
			clear : function() {
				this.control.disable();
				while (this.nodes.length) {
					this.nodes[this.nodes.length - 1].remove()
				}
				this.control.enable()
			},
			update : function(c, g) {
				var b = true;
				if (!this.control.enabled) {
					this.invalidated = true;
					b = false
				}
				if (g) {
					if (!this.invalidated) {
						b = false
					} else {
						this.invalidated = false
					}
				}
				if (b) {
					var a;
					this.div.main.className = "mooTree_node"
							+ (this.selected ? " mooTree_selected" : "");
					var f = this, d = "";
					while (f.parent) {
						f = f.parent;
						d = this.getImg(f.last || !this.control.grid ? "" : "I")+ d
					}
					if (d!="") {
						this.div.indent.innerHTML = d;
					}
					a = this.div.text;
					a.empty();
					//a.appendText(this.text);
					link = new Element('a',{html:this.text, target: this.target, href:this.href});
					link.addClass("mooTree_text");
					link.inject(a);
					
					if (this.color) {
						a.style.color = this.color
					}
					this.div.icon.innerHTML = this
							.getImg(this.nodes.length ? (this.open ? (this.openicon
									|| this.icon || "_open")
									: (this.icon || "_closed"))
									: (this.icon || (this.control.mode == "folders" ? "_closed"
											: "_doc")));
					this.div.gadget.innerHTML = this
							.getImg((this.control.grid ? (this.control.root == this ? (this.nodes.length ? "R"
									: "")
									: (this.last ? "L" : "T"))
									: "")
									+ (this.nodes.length ? (this.open ? "minus"
											: "plus") : ""));
					this.div.sub.style.display = this.open ? "block" : "none"
				}
				if (c) {
					this.nodes.forEach( function(h) {
						h.update(true, g)
					})
				}
			},
			getImg : function(c) {
				var f = '<div class="mooTree_img"';
				if (c != "") {
					var b = this.control.theme;
					var d = MooTreeIcon.indexOf(c);
					if (d == -1) {
						var a = c.split("#");
						b = a[0];
						d = (a.length == 2 ? parseInt(a[1]) - 1 : 0)
					}
					f += ' style="background-image:url(' + b
							+ "); background-position:-" + (d * 18)
							+ 'px 0px;"'
				}
				f += "></div>";
				return f
			},
			toggle : function(a, b) {
				this.open = (b === undefined ? !this.open : b);
				this.update();
				this.onExpand(this.open);
				this.control.onExpand(this, this.open);
				if (a) {
					this.nodes.forEach( function(c) {
						c.toggle(true, this.open)
					}, this)
				}
			},
			select : function(a) {
				this.selected = a;
				this.update();
				this.onSelect(a)
			},
			load : function(a, c) {
				if (this.loading) {
					return
				}
				this.loading = true;
				this.toggle(false, true);
				this.clear();
				this.insert(this.control.loader);
				var b = function() {
					new Request( {
						method : "GET",
						url : a,
						onSuccess : this._loaded.bind(this),
						onFailure : this._load_err.bind(this)
					}).send(a, c || "")
				}.bind(this).delay(20)
			},
			_loaded : function(b, a) {
				this.control.disable();
				this.clear();
				this._import(a.documentElement);
				this.control.enable();
				this.loading = false
			},
			_import : function(h) {
				var j = h.childNodes;
				for ( var f = 0; f < j.length; f++) {
					if (j[f].tagName == "node") {
						var d = {
							data : {}
						};
						var b = j[f].attributes;
						for ( var c = 0; c < b.length; c++) {
							switch (b[c].name) {
							case "text":
							case "id":
							case "icon":
							case "openicon":
							case "color":
							case "open":
							case "target":
							case "href":
								d[b[c].name] = b[c].value;
								break;
							default:
								d.data[b[c].name] = b[c].value
							}
						}
						var g = this.insert(d);
						if (g.data.load) {
							g.open = false;
							g.insert(this.control.loader);
							g.onExpand = function(a) {
								this.load(this.data.load);
								this.onExpand = new Function()
							}
						}
						if (j[f].childNodes.length) {
							g._import(j[f])
						}
					}
				}
			},
			_load_err : function(a) {
				window.alert("Error loading: " + this.text)
			}
		});