dhtml = new Object();

dhtml.config = {
	tooltipX: 20,
	tooltipY: 10,
	dragDropEasyRemove: true
}

dhtml.init = function() {
	var config = this.config;
	var event;

	tooltipInit();
	dragDropInit();
	editInit();
	ajaxInit();
	moInit();
	
	function tooltipInit() {
		var Trigger = function(node) {
			this.node = node;
			this.target = document.getElementById(getNodeAttribute(node, "dh_tooltip_target"));
			
			var trigger = this;
			node.onmouseover = function(event) {
				var cursor = getPosition(event);
				setEvent(event);
				trigger.show(cursor.x, cursor.y);
			}
			node.onmouseout = function(event) {
				setEvent(event);
				trigger.hide();
			}
			node.onmousemove = function(event) {
				var cursor = getPosition(event);
				setEvent(event);
				trigger.move(cursor.x, cursor.y);
			}
		}
		
		Trigger.prototype.show = function(posX, posY) {
			if(window.pageYOffset) {
			   // browsers other than internet explorer
			   the_x = window.pageXOffset;
			   the_y = window.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
			   // internet explorer 6
			   the_x = document.documentElement.scrollLeft;
			   the_y = document.documentElement.scrollTop;
			} else if (document.body){
			   // all other internet explorer versions
			   the_x = document.body.scrollLeft;
			   the_y = document.body.scrollTop;
			}
			posX = posX - the_x + 10;
			posY = posY - the_y + 10;
			this.target.style.display = "block";
			this.target.style.position = "fixed";
			this.target.style.left = posX + "px";
			this.target.style.top = posY + "px";
		}
		
		Trigger.prototype.hide = function() {
			this.target.style.display = "none";
		}
		
		Trigger.prototype.move = function(posX, posY) {
			if(window.pageYOffset) {
			   // browsers other than internet explorer
			   the_x = window.pageXOffset;
			   the_y = window.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
			   // internet explorer 6      
			   the_x = document.documentElement.scrollLeft;
			   the_y = document.documentElement.scrollTop;
			} else if (document.body){
			   // all other internet explorer versions
			   the_x = document.body.scrollLeft;
			   the_y = document.body.scrollTop;
			}
			posX = posX - the_x + 10;
			posY = posY - the_y + 10;
			this.target.style.left = posX + "px";
			this.target.style.top = posY + "px";
		}
		
		
		initTriggers(document.body);
		
		
		function initTriggers(node) {
			if(getNodeAttribute(node, "dh_tooltip_target")) {
				new Trigger(node);
			}
			for(var x = 0; x < node.childNodes.length; x++) {
				if(node.childNodes[x].nodeType == 1) {
					initTriggers(node.childNodes[x]);
				}
			}
		}
	}


	function dragDropInit() {
		var Source = function(node) {
			this.node = node;
			this.items = [];
			this.active = false;
			this.stdClass = node.className;
			this.activeClass = getNodeAttribute(node, "dh_drag_class_active");
			this.firstItemClass = getNodeAttribute(node, "dh_drag_class_item_first");
			this.itemIndropableClass = getNodeAttribute(node, "dh_drag_class_item_nodrag");
			this.activeItemClass = getNodeAttribute(node, "dh_drag_class_item_active");
			this.dragItemClass = getNodeAttribute(node, "dh_drag_class_item_drag");
		}
		
		Source.prototype.init = function() {
			this.render();
		}
		
		Source.prototype.addItem = function(node) {
			var item = new SourceItem(this, node);
			if(item.id.length) {
				this.items.push(item);
				items[item.id] = item;
			}
		}
		
		Source.prototype.setActive = function(active) {
			if(!this.active && active) {
				this.node.className = this.stdClass + " " + this.activeClass;
			}
			if(this.active && !active) {
				this.node.className = this.stdClass
			}
			this.active = active;
		}
		
		Source.prototype.render = function() {
			removeAllChildNodes(this.node);
			
			var first = true;
			for(var x in this.items) {
				this.items[x].node.className = this.items[x].stdClass + (this.items[x].isDropable()? "" : " " + this.itemIndropableClass) + (first? " " + this.firstItemClass : "");
				this.node.appendChild(this.items[x].node);
				first = false;
			}
			
			this.node.style.display = "block";
		}

		var Target = function(node) {
			this.node = node;
			this.active = false;
			this.dropPos = -1;
			this.stdClass = node.className;
			this.dropedItems = [];
			this.dummyItem = false;
			this.item = false;
			this.itemPos = false;
			this.name = getNodeAttribute(node, "dh_drag_name");
			this.firstItemClass = getNodeAttribute(node, "dh_drag_class_item_first");
			this.activeClass = getNodeAttribute(node, "dh_drag_class_active");
			this.replaceItem = getNodeAttribute(node, "dh_drag_replace") == "true";
			this.site = 1;
			
			var target = this;
			
			var maxShow = getNodeAttribute(node, "dh_drag_max_show");
			this.maxShow = maxShow.length? Number(maxShow) : -1;
			
			if(this.maxShow > -1) {
				var prevLink = getNodeAttribute(node, "dh_drag_link_prev");
				if(prevLink.length) {
					this.prevLink = document.getElementById(prevLink);
					this.prevLink.onclick = function() {
						target.site--;
						target.render();
						return false;
					}
				}
				var nextLink = getNodeAttribute(node, "dh_drag_link_next");
				if(nextLink.length) {
					this.nextLink = document.getElementById(nextLink);
					this.nextLink.onclick = function() {
						target.site++;
						target.render();
						return false;
					}
				}
			}
			
			this.mode = getNodeAttribute(node, "dh_drag_mode");
			if(!this.mode.length) {
				this.mode = "list";
			}
			
			this.dragState = getNodeAttribute(node, "dh_drag_state");
			if(!this.dragState.length) {
				this.dragState = "auto";
			}
			
			var maxCount = getNodeAttribute(node, "dh_drag_count");
			this.maxCount = maxCount.length? Number(maxCount) : -1;
			
			if(this.mode == "list") {
				this.hr = document.createElement("hr");
				this.hr.className = getNodeAttribute(node, "dh_drag_class_hr");
				this.hr.style.position = "fixed";
			}
		}
		
		Target.prototype.init = function() {
			for(var x = 0; x < this.node.childNodes.length; x++) {
				if(this.node.childNodes[x].nodeType == 1) {
					var id = getNodeAttribute(this.node.childNodes[x], "dh_drag_id");
					var item = false;
					if(id.length) {
						item = (typeof items[id] != "undefined")? items[id] : this.dummyItem;
					}
					var droped = new DropedItem(this, item, this.node.childNodes[x]);
					droped.setPos(this.dropedItems.length);
					this.dropedItems[this.dropedItems.length] = droped;
				}
			}
			this.render();
		}
		
		Target.prototype.detectPos = function() {
			this.itemPos = -1;
			if(!this.item) {
				return -1;
			}
			
			var pos = -1;
			if(this.mode == "list") {
				for(var x = 0; x < this.dropedItems.length; x++) {
					var itemPos = getElementPos(this.dropedItems[x].node);
					if(itemPos.y + itemPos.h / 2 > event.documentY) {
						pos = x;
						break;
					}
				}
			} else {
				for(var x = 0; x < this.dropedItems.length; x++) {
					var itemPos = getElementPos(this.dropedItems[x].node);
					if(itemPos.y + itemPos.h > event.documentY && itemPos.x + itemPos.w / 2 > event.documentX) {
						pos = x;
						break;
					}
				}
			}
			
			pos = this.getAbsPos(pos);
			
			var diff = 0;
			while(diff <= this.dropedItems.length) {
				var tPos = pos + diff;
				if(tPos <= this.dropedItems.length && this.checkPos(tPos)) {
					pos = tPos;
					break;
				}
				var tPos = pos - diff;
				if(tPos >= 0 && diff && this.checkPos(tPos)) {
					pos = tPos;
					break;
				}
				diff++;
			}
			if(this.dropedItems.length && diff >= this.dropedItems.length) {
				this.item = false;
				return -1;
			}
			
			this.itemPos = (this.item.target == this && pos > this.item.pos)? pos -1 : pos;
			
			return pos;
		}
		
		Target.prototype.getAbsPos = function(pos) {
			return (pos >= 0)? pos : this.dropedItems.length + pos + 1;
		}
		
		Target.prototype.checkPos = function(pos) {
			
			// ......
			
			for(var x = pos; x < this.dropedItems.length; x++) {
				var tPos = this.getAbsPos(this.dropedItems[x].maxPos);
				if(x + 1 > tPos && (this.item.target != this || pos <= tPos && this.item.pos > tPos)) {
					return false;
				}
			}
			
			// ......
			
			return true;
		}
		
		Target.prototype.setItem = function(item) {
			var active = !!this.item;
			this.item = item;
			var pos = this.detectPos();
			if(!active && this.item) {
				this.node.className = this.stdClass + " " + this.activeClass;
				if(this.mode == "list") {
					this.node.appendChild(this.hr);
				}
			}
			if(active && !this.item) {
				this.node.className = this.stdClass
				if(this.mode == "list") {
					this.node.removeChild(this.hr);
				}
			}
			this.showPos(pos);
		}
		
		Target.prototype.showPos = function(pos) {
			if(this.mode != "list" || !this.item) {
				return;
			}
			if(this.dropedItems.length) {
				this.hr.style.display = "block";
				var posHr = getElementPos(this.hr);
				if(pos == 0) {
					var pos1 = getElementPos(this.node);
					var pos2 = getElementPos(this.dropedItems[0].node);
					this.hr.style.top = (Math.round((pos2.y - pos1.y - posHr.h) / 2) + pos1.y) + "px";
				}
				else if(pos == this.dropedItems.length) {
					var pos1 = getElementPos(this.node);
					var pos2 = getElementPos(this.dropedItems[0].node);
					var pos3 = getElementPos(this.dropedItems[this.dropedItems.length - 1].node);
					this.hr.style.top = (Math.round((pos2.y - pos1.y - posHr.h) / 2) + pos3.y + pos3.h) + "px";
				}
				else {
					var pos1 = getElementPos(this.dropedItems[pos - 1].node);
					var pos2 = getElementPos(this.dropedItems[pos].node);
					this.hr.style.top = (Math.round((pos2.y - pos1.y - pos1.h - posHr.h) / 2) + pos1.y + pos1.h) + "px";
				}
			}
			else {
				this.hr.style.display = "none";
			}
		}
		
		Target.prototype.countDropedItems = function(item) {
			var count = 0;
			for(var x in this.dropedItems) {
				if(this.dropedItems[x].item == item) {
					count++;
				}
			}
			return count;
		}
		
		Target.prototype.dropItem = function() {
			if(!this.item) {
				return false;
			}
			if(!this.item.item) {
				var dropedItem = new DropedItem(this, this.item);
				this.pushDropedItem(dropedItem, this.itemPos);
			}
			else {
				this.item.target.pullDropedItem(this.item);
				this.pushDropedItem(this.item, this.itemPos);
			}
			this.setItem(false);
			return true;
		}
		
		Target.prototype.removeDropedItem = function(dropedItem) {
			this.pullDropedItem(dropedItem);
		}
		
		Target.prototype.pushDropedItem = function(dropedItem, pos) {
			dropedItem.setTarget(this);
			dropedItem.node.className = dropedItem.item.stdClass;
			
			if(this.replaceItem && this.dropedItems.length >= this.maxCount) {
				this.dropedItems.pop();
			}
			
			if(pos == -1 || !this.dropedItems.length || pos >= this.dropedItems.length) {
				dropedItem.setPos(this.dropedItems.length);
				this.dropedItems[this.dropedItems.length] = dropedItem;
			}
			else {
				var dropedItems = [];
				for(var x in this.dropedItems) {
					if(x == pos) {
						dropedItem.setPos(dropedItems.length);
						dropedItems[dropedItems.length] = dropedItem;
					}
					this.dropedItems[x].setPos(dropedItems.length);
					dropedItems[dropedItems.length] = this.dropedItems[x];
				}
				this.dropedItems = dropedItems;
			}
		}

		Target.prototype.pullDropedItem = function(dropedItem) {
			this.node.removeChild(dropedItem.node);
			
			var dropedItems = [];
			for(var x in this.dropedItems) {
				if(this.dropedItems[x] != dropedItem) {
					this.dropedItems[x].setPos(dropedItems.length);
					dropedItems[dropedItems.length] = this.dropedItems[x];
				}
			}
			this.dropedItems = dropedItems;
			
		}

		Target.prototype.render = function() {
			removeAllChildNodes(this.node);
			
			if(this.maxShow > -1) {
				var showFrom = (this.site - 1) * this.maxShow;
				var showTo = showFrom + this.maxShow;
				
				this.prevLink.style.display = (this.site > 1)? "" : "none";
				this.nextLink.style.display = (this.site < this.dropedItems.length / this.maxShow)? "" : "none";
			}
			
			var first = true;
			for(var x in this.dropedItems) {
				this.dropedItems[x].node.style.display = (this.maxShow > -1 && (x < showFrom || x >= showTo))? "none" : "";
				this.node.appendChild(this.dropedItems[x].node);
				first = false;
			}
		}

		var Item = function() {}
		
		Item.prototype.initPos = function() {
			if(this.item) {
				this.minPos = this.item.minPos;
				this.maxPos = this.item.maxPos;
			}
			else {
				var pos = getNodeAttribute(this.node, "dh_drag_pos_min");
				this.minPos = (pos.length)? Number(pos) : 0;
				
				var pos = getNodeAttribute(this.node, "dh_drag_pos_max");
				this.maxPos = (pos.length)? Number(pos) : -1;
			}
		}
		
		Item.prototype.initDragDrop = function() {
			var item = this;
			
			this.node.onmousedown = function(event) {
				setEvent(event);
				
				item.drag();
				item.move();
				return false;
			}
		}
		
		Item.prototype.drag = function() {
			if(!this.isDropable()) {
				return;
			}

			this.clone = this.node.cloneNode(true);
			this.clone.className = this.stdClass + " " + this.source.dragItemClass;
			this.clone.style.position = "absolute";
			this.clone.style.margin = 0;
			this.clone.onmousedown = function(event) { return false; }
			this.clone.onmousemove = function(event) { return false; }

			this.sourceNode.appendChild(this.clone);

			this.offsetPos = getElementPos(this.node);
			// alert(event.documentX + " - " + this.offsetPos.x + " | " + event.documentY + " - " + this.offsetPos.y);
			this.offsetPos.x = event.documentX - this.offsetPos.x;
			this.offsetPos.y = event.documentY - this.offsetPos.y;
			// alert(this.offsetPos.x + " x " + this.offsetPos.y + " | " + (event.documentX - this.offsetPos.x) + "px");

			var item = this;
			this.bodyOnmousemove = document.body.onmousemove;
			this.bodyOnmouseup = document.body.onmouseup;
			document.body.onmousemove = function(event) {
				setEvent(event);
				item.move();
				return false;
			}
			document.body.onmouseup = function(event) {
				setEvent(event);
				item.drop();
				return false;
			}
		}
		
		Item.prototype.move = function() {
			operaName = 'opera';
			ie8 = 'msie 8.0';
			var userAgent = navigator.userAgent.toLowerCase();
			if(userAgent.indexOf(operaName.toLowerCase()) >- 1) {
				xo = 277;
				yo = 38;
			} else if(userAgent.indexOf(ie8.toLowerCase()) >- 1) {
                                xo = 275;
                                yo = 38;
			} else {
				xo = 275;
				yo = 35;
			}
			styleHeaderHigh = document.getElementById('header').style.height;

			styleHeaderHigh = styleHeaderHigh.toString();
			styleHeaderHigh = styleHeaderHigh.replace(/px/g, "");
			yo = yo + parseInt(styleHeaderHigh);
			this.clone.style.left = (event.documentX - this.offsetPos.x - xo) + "px";
			this.clone.style.top = (event.documentY - this.offsetPos.y - yo) + "px";
			var targetActive = false;
			
			for(var x in this.targets) {
				if(this.checkTarget(x)) {
					var active = checkMouseover(this.targets[x].target.node)? this : false;
					this.targets[x].target.setItem(active);
					if(active) {
						targetActive = true;
					}
				}
			}
			
			if(this.checkSource()) {
				this.source.setActive(!targetActive && (checkMouseover(this.source.node) || config.dragDropEasyRemove));
			}
		}
		
		Item.prototype.checkTarget = function(target) {
			return true;
		}
		
		Item.prototype.checkSource = function() {
			return true;
		}
		
		Item.prototype.drop = function() {
			document.body.onmousemove = this.bodyOnmousemove;
			document.body.onmouseup = this.bodyOnmouseup;
			
			this.sourceNode.removeChild(this.clone);
			for(var x in this.targets) {
				this.targets[x].target.dropItem();
			}
			if(this.source.active) {
				this.target.removeDropedItem(this);
			}
			this.source.setActive(false);
			this.source.render();
			for(var x in this.targets) {
				this.targets[x].target.render();
			}
		}
		
		Item.prototype.isDropable = function() {
			return true;
		}
		
		Item.prototype.targetDropable = function(target) {
			var target = this.targets[target];
			return (target.maxCount == -1 || target.target.countDropedItems(this.item? this.item : this) < target.maxCount) && (target.target.maxCount == -1 || target.target.dropedItems.length < target.target.maxCount) || target.target.replaceItem;
		}


		var SourceItem = function(source, node) {
			this.node = node;
			this.source = source;
			this.sourceNode = source.node;
			this.targets = [];
			this.id = getNodeAttribute(this.node, "dh_drag_id");

			var classes = this.node.className.split(" ");
			this.stdClass = "";
			for(var x in classes) {
				if(classes[x] != this.source.firstItemClass && classes[x] != this.source.itemIndropableClass) {
					this.stdClass += (this.stdClass.length? " " : "") + classes[x];
				}
			}
			
			var targetIds = getNodeAttribute(this.node, "dh_drag_targets").split(",");
			var targetCount = getNodeAttribute(this.node, "dh_drag_count").split(",");
			
			var maxCount = getNodeAttribute(this.node, "dh_drag_count_full");
			this.maxCount = maxCount.length? Number(maxCount) : -1;
			
			for(var x in targetIds) {
				var target = getTarget(targetIds[x]);
				if(!this.id.length) {
					target.dummyItem = this;
				}
				this.targets.push({
					target: target,
					maxCount: (typeof targetCount[x] != "undefined" && targetCount[x].length)? Number(targetCount[x]) : -1
				});
			}
			
			this.initPos();
			this.initDragDrop();
		}
		
		SourceItem.prototype = new Item();
		
		SourceItem.prototype.checkTarget = function(target) {
			return this.isDropable() && this.targetDropable(target);
		}
		
		SourceItem.prototype.checkSource = function() {
			return false;
		}
		
		SourceItem.prototype.isDropable = function() {
			var count = 0;
			var targetDropable = false;
			for(var x in this.targets) {
				count += this.targets[x].target.countDropedItems(this);
				if(this.targets[x].target.dragState == "auto" && this.targetDropable(x)) {
					targetDropable = true;
				}
			}
			return targetDropable && (this.maxCount == -1 || count < this.maxCount);
		}


		var DropedItem = function(target, item, node) {
			this.setTarget(target);
			this.item = item;
			this.node = (typeof node != "undefined")? node : false;
			this.fields = [];
			this.pos = -1;

			if(this.node) {
				for(var x = 0; x < this.node.childNodes.length; x++) {
					var child = this.node.childNodes[x];
					if(child.nodeType == 1 && getNodeAttribute(child, "name").search(eval("/^" + this.target.name + "\\[\\d*\\]\\[([^\\]]+)\\]/")) > -1) {
						this.fields[RegExp.$1] = child;
					}
				}
			}

			if(this.item) {
				this.source = this.item.source;
				this.targets = this.item.targets;
				this.stdClass = this.item.stdClass;
				
				if(!this.node) {
					this.node = this.item.node.cloneNode(true);
					this.node.removeAttribute("dh_drag_targets");
					
					for(var x = 0; x < this.node.childNodes.length; x++) {
						var child = this.node.childNodes[x];
						if(child.nodeType == 1) {
							var name = getNodeAttribute(child, "dh_drag_name");
							if(name.length) {
								this.fields[name] = child;
							}
						}
					}
				}
			} else {
			   var classes = this.node.className.split(" ");
			   this.stdClass = "";
			   for(var x in classes) {
			      if(classes[x] != this.target.firstItemClass) {
			         this.stdClass += (this.stdClass.length? " " : "") + classes[x];
			      }
		  	   }
			}
			
			this.initPos();
			if(this.item) {
				this.initDragDrop();
			}
		}
		
		DropedItem.prototype = new Item();
		
		DropedItem.prototype.setTarget = function(target) {
			this.target = target;
			this.sourceNode = target.node;
		}
		
		DropedItem.prototype.checkTarget = function(target) {
			return this.targetDropable(target) || this.target == this.targets[target].target;
		}
		
		DropedItem.prototype.setPos = function(pos) {
			this.pos = pos;
			for(var x in this.fields) {
				if(this.target.maxCount == 1) {
					this.fields[x].name = this.target.name + "[" + x + "]";
				}
				else {
					this.fields[x].name = this.target.name + "[" + pos + "][" + x + "]";
				}
			}
			this.node.className = this.stdClass + ((pos == 0)? " " + this.target.firstItemClass : "");
		}
		
		
		dhtml.dragDropReInit = function() {
			init();
		}
		
		
		var sources, items, targets;
		
		init();
		
		function init() {
			sources = [];
			items = [];
			targets = [];
			
			initSources(document.body);
			
			for(var x in targets) {
				targets[x].init();
			}
			
			for(var x in sources) {
				sources[x].init();
			}
		}
		
		function initSources(node) {
			var source = false;
			for(var x = 0; x < node.childNodes.length; x++) {
				if(getNodeAttribute(node.childNodes[x], "dh_drag_targets")) {
					if(!source) {
						source = new Source(node);
					}
					source.addItem(node.childNodes[x]);
				}
			}
			if(source) {
				sources.push(source);
			}
			else {
				for(var x = 0; x < node.childNodes.length; x++) {
					if(node.childNodes[x].nodeType == 1) {
						initSources(node.childNodes[x]);
					}
				}
			}
		}
		
		function getTarget(id) {
			if(typeof targets[id] != "undefined") {
				var target = targets[id];
			}
			else {
				var target = new Target(document.getElementById(id));
				targets[id] = target;
			}
			return target;
		}
	}
	
	
	function dragDropReInit() {}
	
   var button = false;
   function editInit() {
      var Container = function(gadgetNode) {
         this.init(gadgetNode);
      }

      Container.prototype.init = function(gadgetNode) {
         try {
            onGadgetShow = new Function("", getNodeAttribute(gadgetNode, "dh_gadget_onshow"));
            onGadgetShow();
         } catch(err) {}
         this.node = gadgetNode;
         this.editNode = searchEditNode(this.node);
         this.editButton = searchEditButton(searchHeaderNode(this.node));
         if(!this.node || !this.editNode || !this.editButton)
            return false;

         // Formular lesen um die Daten per Ajax zu Posten
         this.form = false;
         for(var x = 0; x < this.editNode.childNodes.length; x++) {
            if(this.editNode.childNodes[x].nodeType == 1 && this.editNode.childNodes[x].nodeName == "FORM") {
               this.form = this.editNode.childNodes[x];
               break;
            }
         }

         // Flag die den Status des Gadget Edit Blockes liefert.
         this.setEditShown(false);

         var container = this;
         this.editButton.onshow = new Function("", getNodeAttribute(this.editNode, "dh_edit_onshow"));
         this.editButton.onclick = function(event) {
            container.setEditShown(!container.editShown);
            if(!this.ed) {
               this.onshow();
               this.ed = true;
            }
         }

         this.onclick = function(event) {
            style.display = status ? "block" : "none";
         }

         this.form.onsave = new Function("", getNodeAttribute(this.editNode, "dh_edit_onsave"));
         this.form.onsubmit = function(event) {
            this.onsave();
            container.setEditShown(false);

            container.editButton.innerHTML = '<span style="position:relative; top:-5px; color:white; right:2px;">load..</span>';
            container.sendForm();

            return false;
         }
      }

      Container.prototype.sendForm = function() {
         var ajax = new Ajax();
         var container = this;

         ajax.response = function() {
            container.reInit(this.data);
         }
         ajax.sendForm(this.form);

         return false;
      }

      Container.prototype.reInit = function(data) {
    	  if(data == '')
            return false;

         var newNode = false;
         var newNode = document.createElement("div");
         newNode.innerHTML = data;
         if(!newNode)
            return false;

         this.node.parentNode.insertBefore(newNode, this.node);
         this.node.parentNode.removeChild(this.node);
         if(this.node == searchGadgetNode(newNode.parentNode)) {
            this.init(this.node);
         } else {
            this.init(newNode);
         }
         var onsaved = new Function("", getNodeAttribute(this.editNode, "dh_edit_onsaved"));
         onsaved();

         return false;
      }

      Container.prototype.setEditShown = function(status) {
         this.editShown = status;
         this.editNode.style.display = status? "block" : "none";
      }

      Container.prototype.setBoxShown = function(status) {
         this.boxShown = status;
         this.boxNode.style.display = status? "block" : "none";
      }

      function initContainers(node) {
         var gadgetNode = searchGadgetNode(node);
         if(gadgetNode) {
            containers.push(new Container(gadgetNode));
            var newAttr = document.createAttribute("module_box");
            newAttr.nodeValue = "";
            gadgetNode.setAttributeNode(newAttr);
            initContainers(node.parentNode);
         } else {
            for(var x = 0; x < node.childNodes.length; x++) {
               if(node.childNodes[x].nodeType == 1) {
                  initContainers(node.childNodes[x]);
               }
            }
         }
      }

      function searchGadgetNode(node) {
         var container = false;
         for(var x = 0; x < node.childNodes.length; x++) {
            if(node.childNodes[x].nodeType == 1) {
               for(var y = 0; y < node.childNodes[x].childNodes.length; y++) {
                  if(node.childNodes[x].childNodes[y].nodeType == 1 && getNodeAttribute(node.childNodes[x].childNodes[y], "module_box") != "") {
                     return node.childNodes[x].childNodes[y];
                  }
               }
            }
         }
      }

      function searchEditNode(node) {
         var container = false;

         for(var x = 0; x < node.childNodes.length; x++) {
            if(node.childNodes[x].nodeType == 1) {
               if(node.childNodes[x].className == "boxEdit") {
                  return node.childNodes[x];
               } else {
            	   
                  for(var y = 0; y < node.childNodes[x].childNodes.length; y++) {
                     if(node.childNodes[x].childNodes[y].nodeType == 1 && node.childNodes[x].childNodes[y].className == "boxEdit") {
                        return node.childNodes[x].childNodes[y];
                     }
                  }
               }
            }
         }
      }

      function searchHeaderNode(node) {
         var boxHeader = false;

         for(var x = 0; x < node.childNodes.length; x++) {
            if(node.childNodes[x].nodeType == 1) {
               if(node.childNodes[x].className == "boxHeader") {
            	   return node.childNodes[x];
               } else {
                  if(node.childNodes[x].childNodes.length > 0) {
                     for(var y = 0; y < node.childNodes[x].childNodes.length; y++) {
                        if(node.childNodes[x].childNodes[y].className == "boxHeader") {
                           return node.childNodes[x].childNodes[y];
                        }
                     }
                  }
               }
            }
         }
         return false;
      }

      function searchEditButton(node) {
         if(node.childNodes.length > 0) {
            for(var z = 0; z < node.childNodes.length; z++) {
               if(node.childNodes[z].className == "boxEditButton") {
                  return boxHeader.childNodes[z].firstChild;
               }
               if(node.childNodes[z].childNodes.length > 0) {
                  for(var i = 0; i < node.childNodes[z].childNodes.length; i++) {
                     if(node.childNodes[z].childNodes[i].className == "boxEditButton") {
                        return node.childNodes[z].childNodes[i];
                     }
                  }
               }
            }
         }

         return false;
      }

      function searchBodyNode(node) {
         if(node.childNodes.length > 0) {
            for(var z = 0; z < node.childNodes.length; z++) {
               if(node.childNodes[z].className == "boxBody") {
                  return boxHeader.childNodes[z].firstChild;
               }
               if(node.childNodes[z].childNodes.length > 0) {
                  for(var i = 0; i < node.childNodes[z].childNodes.length; i++) {
                     if(node.childNodes[z].childNodes[i].className == "boxBody") {
                        return node.childNodes[z].childNodes[i];
                     }
                  }
               }
            }
         }

         return false;
      }

      var containers = [];
      initContainers(document.body);
   }

	function ajaxInit() {
		initElements(document.body);
		
		function initElements(node) {
			var target = "";
			if(node.nodeName == "A" || node.nodeName == "FORM") {
				target = getNodeAttribute(node, "dh_ajax_target");
			}
			if(target.length) {
				var ajax = new Ajax();
				
				if(target == "_self") {
					target = node;
				}
				else if(target == "_parent") {
					target = node.parentNode;
				}
				else {
					target = document.getElementById(target);
				}
				
				if(target) {
					ajax.response = function() {
						target.innerHTML = this.data;
						initElements(target);
						dhtml.dragDropReInit();
					}
				}
				
				if(node.nodeName == "A") {
					node.onclick = function() {
						ajax.sendLink(node);
						return false;
					}
				}
				else {
					node.onsubmit = function() {
						ajax.sendForm(node);
						return false;
					}
				}
			}
			else {
				for(var x = 0; x < node.childNodes.length; x++) {
					if(node.childNodes[x].nodeType == 1) {
						initElements(node.childNodes[x]);
					}
				}
			}
		}
	}
	
	
	
	function moInit() {
		initImages(document.body, false);
		
		function initImages(node, link) {
			if(node.nodeName == "A") {
				link = node;
			}
			else if(link && node.nodeName == "IMG") {
				var moImageSrc = getNodeAttribute(node, "dh_mo_image");
				if(moImageSrc.length) {
					var moImage = new Image();
					moImage.src = moImageSrc;
					
					var image = new Image();
					image.src = getNodeAttribute(node, "src");
					
					var oldMouseover = link.onmouseover;
					
					link.onmouseover = function() {
						node.src = moImage.src;
						
						if(typeof oldMouseover == "function") {
							oldMouseover();
						}
					}
					
					var oldMouseout = link.onmouseout;
					
					link.onmouseout = function() {
						node.src = image.src;
						
						if(typeof oldMouseout == "function") {
							oldMouseout();
						}
					}
				}
			}
			
			for(var x = 0; x < node.childNodes.length; x++) {
				if(node.childNodes[x].nodeType == 1) {
					initImages(node.childNodes[x], link);
				}
			}
		}
	}
	
	
	function removeAllChildNodes(node) {
		var nodes = [];
		for(var x = 0; x < node.childNodes.length; x++) {
			if(node.childNodes[x].nodeType == 1) {
				nodes.push(node.childNodes[x]);
			}
		}
		for(var x in nodes) {
			node.removeChild(nodes[x]);
		}
	}
	
	function setEvent(e) {
		if(typeof e == "undefined") {
			e = window.event;
		}
		e.documentX = document.documentElement.scrollLeft + e.clientX;
		e.documentY = document.documentElement.scrollTop + e.clientY;
		event = e;
	}
	
	function getElementPos(element) {
		var pos = {
			x: 0,
			y: 0,
			h: element.offsetHeight,
			w: element.offsetWidth
		}
		while(element) {
			pos.x += element.offsetLeft;
			pos.y += element.offsetTop;
			element = element.offsetParent;
		}

		return pos;
	}
	
	function checkMouseover(element) {
		var pos = getElementPos(element);

		if(
			event.documentX >= pos.x &&
			event.documentX < pos.x + pos.w &&
			event.documentY >= pos.y &&
			event.documentY < pos.y + pos.h
		) {
			return true;
		}
		return false;
	}
	
	function getNodeAttribute(node, attribute) {
		if(!node.getAttribute) {
			return "";
		}
		var value = node.getAttribute(attribute);
		return (typeof value == "string" && value.length)? value : "";
	}
	
	function inArray(arr, elem) {
		for(var x in arr) {
			if(arr[x] == elem) {
				return true;
			}
		}
		return false;
	}
	
	function arrayKeys(arr) {
		var keys = [];
		for(var x in arr) {
			keys.push(x);
		}
		return keys;
	}
}
