//<!--
//# Written by Russell Moffitt, NOAA
//# Modified Oct 2005
//# License: GPL
//-->

// DOM TREE TRAVERSAL & ELEMENT SEARCHING FUNCTIONS
// Modified from cross-browser.com x lib (Mike Foster).

/* x_dom.js
   additional functions can be found in
       x_core.js:	Cross-browser DOM & CSS compatibility functions
       x_event.js:	W3C DOM and Microsoft event model compatibility functions
       x_slide.js:	Animation functions for a smooth sin slide (slow>>fast>>slow)
*/

function xFirstChild(e, t) {
	var c = e ? e.firstChild : null;
	if (t) while (c && c.nodeName != t) { c = c.nextSibling; }
	else while (c && c.nodeType != 1) { c = c.nextSibling; }
	return c;
}

function xNextSib(e,t) {
	var s = e ? e.nextSibling : null;
	if (t) while (s && s.nodeName != t) { s = s.nextSibling; }
	else while (s && s.nodeType != 1) { s = s.nextSibling; }
	return s;
}

function xPrevSib(e,t) {
	var s = e ? e.previousSibling : null;
	if (t) while(s && s.nodeName != t) {s=s.previousSibling;}
	else while(s && s.nodeType != 1) {s=s.previousSibling;}
	return s;
}

/* xWalkTree()
   Perform a preorder traversal on the subtree starting at oNode
   and pass each Element node to function f.
*/
function xWalkTree(n, f) {
	f(n);
	for (var c = n.firstChild; c; c = c.nextSibling) {
		if (c.nodeType == 1) xWalkTree(c, f);
	}
}

/* xGetElementsByClassName()
   Returns an array of elements which are
   descendants of parentEle and have tagName and clsName.
   If parentEle is null or not present, document will be used.
   if tagName is null or not present, "*" will be used.
   fn is an optional callback function, iterates thru the returned list
*/
function xGetElementsByClassName(clsName,parentEle,tagName,fn) {
	var found = new Array();
	var re = new RegExp('\\b'+clsName+'\\b', 'i');
	var list = xGetElementsByTagName(tagName, parentEle);
	for (var i = 0; i < list.length; ++i) {
		if (list[i].className && list[i].className.search(re) != -1) {
			found[found.length] = list[i];
			if (fn) fn(list[i]);
		}
	}
	return found;
}

/* xGetElementsByTagName()
   Returns an array of elements which are
   descendants of parentEle and have tagName.
   If parentEle is null or not present, document will be used.
   if tagName is null or not present, "*" will be used.
*/
function xGetElementsByTagName(tagName,parentEle) {
	var list = null;
	tagName = tagName || '*';
	parentEle = parentEle || document;
	if (parentEle.getElementsByTagName) list = parentEle.getElementsByTagName(tagName);
	return list || new Array();
}

/* xGetElementsByAttribute
   Return an array of all sTag elements whose sAtt attribute matches sRE.
   sAtt can also be a property name but the property must be of type string.
   fn is an optional callback function, iterates thru the returned list
*/
function xGetElementsByAttribute(sTag, sAtt, sRE, fn) {
	var a, list, found = new Array(), re = new RegExp(sRE, 'i');
	list = xGetElementsByTagName(sTag);
	for (var i = 0; i < list.length; ++i) {
		a = list[i].getAttribute(sAtt);
		if (!a) {a = list[i][sAtt];}
		if (typeof(a)=='string' && a.search(re) != -1) {
			found[found.length] = list[i];
			if (fn) fn(list[i]);
		}
	}
	return found;
}
