YAHOO.namespace ("aplia.buttons");

/*
 * Various DOM utilities and other sundries used for YUI based buttons.
 */ 
YAHOO.aplia.buttons.DOMutil = {
	/*
	 * Find nearest non-text sibling element from a node. Use direction "1" for next sibling, "-1" for previous sibling.
	 */
	closestSibling : function (node, direction) {
		var tmpObj;
		if (direction == -1&& node.previousSibling != null) {
			tmpObj = node.previousSibling;
			while (tmpObj.nodeType != 1 && tmpObj.previousSibling != null) {
				tmpObj = tmpObj.previousSibling;
			}
		}
		else if (direction == 1 && node.nextSibling != null) {
			tmpObj = node.nextSibling;
			while (tmpObj.nodeType != 1 && tmpObj.nextSibling != null) {
				tmpObj = tmpObj.nextSibling;
			}
		}
		return (tmpObj.nodeType == 1 ? tmpObj : false);
	},
	/*
	 * Find the first non-text child element.
	 */
	veryFirstChild : function (node) {
		if (node.nodeType == 1 && node.hasChildNodes()) {
			var ch = node.childNodes;
			for (var i=0; i < ch.length; i++) {
				if (ch[i].nodeType == 1) {
					return ch[i];
				}
			}
		}
		return null;
	},
	/*
	 * Get the event target.
	 */
	getTarget : function (e) {
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target) { return false; }
		// The following line is a fix for when the border is mouse clicked.
		if (target.nodeName != "BUTTON") {
			if (target.className == "first-child") 
				return target.firstChild;
			else if (target.firstChild.nodeName == "SPAN" && target.firstChild.className == "first-child") 
				return target.firstChild.firstChild;
		}
		
		return target;
	},
	/*
	 * Scan the args to the button span container.
	 */
	getSpanArgs : function (node) {
		var argsArray = new Array(); 
		if (node.nodeType == 1)
			for (var i = 0; i < node.attributes.length; i++) {
				argsArray[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
			}
		return argsArray;
	},
	
   	dummy : function (eventObj) {
		var spanNode = YAHOO.aplia.buttons.DOMutil.getTarget(eventObj).parentNode.parentNode.parentNode;
		alert(spanNode.id);
   	}

};





