
/**
 * --------------------------------------------------------------------------------------
 * Copyright (c) 2008 by hansadigit - <info@hansadigit.de>
 * Hamburg, Germany
 * --------------------------------------------------------------------------------------
 * Author: Oliver Oestrup <oestrup@hansadigit.de>
 **/



dhtml = new Object();

dhtml.config = {
	tooltipX: -33,
	tooltipY: -332,
	dragDropEasyRemove: true
}

dhtml.init = function() {	
	var config = this.config;
	var event;
	
	
	tooltipInit();
	//dragDropInit();
	//editInit();
	//ajaxInit();
	
	
	function tooltipInit() {
		
		var Trigger = function(node) {
			this.node = node;
			this.target = document.getElementById(getNodeAttribute(node, "dh_tooltip_target"));
			this.tooltipX = Number(getNodeAttribute(node, "dh_tooltip_x"));
			this.tooltipY = Number(getNodeAttribute(node, "dh_tooltip_y"));
			
			var trigger = this;
			node.onmouseover = function(event) {
				setEvent(event);
				trigger.show();
			}
			node.onmouseout = function(event) {
				setEvent(event);
				trigger.hide();
			}
			node.onmousemove = function(event) {
				setEvent(event);
				trigger.move();
			}
		}
		
		Trigger.prototype.show = function() {
			this.target.style.display = "block";
			this.target.style.position = "absolute";
		}
		
		Trigger.prototype.hide = function() {
			this.target.style.display = "none";
		}
		
		Trigger.prototype.move = function() {
			this.target.style.left = (event.documentX + this.tooltipX) + "px";
			this.target.style.top = (event.documentY + this.tooltipY) + "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 = "absolute";
			}
		}
		
		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.margin = 0;
			this.clone.style.position = "absolute";
			this.clone.onmousedown = function(event) { return false; }
			this.clone.onmousemove = function(event) { return false; }
			
			this.sourceNode.appendChild(this.clone);
			
			this.offsetPos = getElementPos(this.node);
			this.offsetPos.x = event.documentX - this.offsetPos.x;
			this.offsetPos.y = event.documentY - this.offsetPos.y;
			
			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() {
			this.clone.style.left = (event.documentX - this.offsetPos.x) + "px";
			this.clone.style.top = (event.documentY - this.offsetPos.y) + "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() {}
	
	
	function editInit() {
		
		var Container = function(editButton) {
			this.init(editButton);
		}
		
		Container.prototype.init = function(editButton) {
			this.editNode = editButton.parentNode;
			this.node = this.editNode.parentNode;
			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;
				}
			}
			
			this.node.insertBefore(editButton, this.node.firstChild);
			
			this.setEditShown(false);
			
			var container = this;
			
			var onshow = new Function("", getNodeAttribute(this.editNode, "dh_edit_onshow"));
			
			editButton.onclick = function(event) {
				container.setEditShown(!container.editShown);
				onshow();
				return false;
			}
			
			var onsave = new Function("", getNodeAttribute(this.editNode, "dh_edit_onsave"));
			
			this.form.onsubmit = function(event) {
				onsave();
				container.setEditShown(false);
				editButton.innerHTML = 'load...';
				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);
		}
		
		Container.prototype.reInit = function(data) {
			var newNode = false;
			var tmpNode = document.createElement("div");
			tmpNode.innerHTML = data;
			for(var x = 0; x < tmpNode.childNodes.length; x++) {
				if(tmpNode.childNodes[x].nodeType == 1) {
					var editButton = searchEditButton(tmpNode.childNodes[x]);
					if(editButton) {
						newNode = tmpNode.childNodes[x];
						break;
					}
				}
			}
			if(!newNode) {
				location.reload();
				return;
			}
			this.node.parentNode.insertBefore(newNode, this.node);
			this.node.parentNode.removeChild(this.node);
			this.init(editButton);
			
			var onsaved = new Function("", getNodeAttribute(this.editNode, "dh_edit_onsaved"));
			onsaved();
		}
		
		Container.prototype.setEditShown = function(status) {
			this.editShown = status;
			this.editNode.style.display = status? "block" : "none";
		}
		
		
		var containers = [];
		
		initContainers(document.body);
		
		function initContainers(node) {
			var editButton = searchEditButton(node);
			if(editButton) {
				containers.push(new Container(editButton));
			}
			else {
				for(var x = 0; x < node.childNodes.length; x++) {
					if(node.childNodes[x].nodeType == 1) {
						initContainers(node.childNodes[x]);
					}
				}
			}
		}
		
		function searchEditButton(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], "dh_edit_button")) {
							return node.childNodes[x].childNodes[y];
						}
					}
				}
			}
		}
	}
	
	
	
	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 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;
	}
}









