// a library containing (HTML)node methods ======== // 
// (c) Leen Besslink 2004 ========================= // 
// testing & bugfixes by Jan Vermaat 2004 ========= //
// november 2004 ================================== //
// version 1.2.1 ================================== //

// constructor functie nodes ======================= //
var nodes = function () {}

nodes.prototype.childNodeBy = function (el, condition, deepChild) {
	if (this.is_node (el) == false)
		return false;

	if (condition == false)
		return false;

// oops , next part is not good at all.
// problems in Mozilla with typeof el==function
	if (typeof el == 'undefined' || typeof el == 'function')
		return false;

	if (!el.hasChildNodes ())
		return false;

	for (var el2 in el.childNodes)
		if (el2 != 'length') {
			var el3 = el.childNodes [el2];

			if (this.check_condition (el3, condition))
				return el3;

			var el4 = this.childNodeBy (el3, condition, deepChild);

			if (el4 !== false)
				if (deepChild === true)
					return el4;
				else
					return el3;
		}

	return false;
}


// parentNodeBy ==================================== //
// zoek een parent node die voldoet aan:
//  condition (a=b)
// voorbeeld
// (o,'nodeName=TD')
// o = het huidige object waarin men zich bevind
// zoek naar de eerste parent met de property nodeName=TD
// oftewel: zoek naar de eerste bovenliggende TD
// =================================================== //
nodes.prototype.parentNodeBy  = function (el, condition) {
	if (typeof condition != 'string')
		condition = false;

	while (true) {
		if (el == null)
			return false;
			
		if (typeof el.parentNode == 'undefined') 
			return false;

		if (condition == false) 
			return el.parentNode;

		if (this.check_condition (el.parentNode, condition)) 
			return el.parentNode;

		var el = el.parentNode;
	}
	return false;
}

// check_condition ============================================ //
/*
	zinnigeParentNode = parentNodeBy ('prop!=', thisNode);
*/
// hulpfunctie ================================================ //

nodes.prototype.check_condition = function (el, condition) {
	if (typeof condition != 'string')
		return false;

	var arr = condition.split ('=', 2);

	if (arr.length == 1)
		arr [1] = '';

	if (typeof arr.length != 'undefined' && arr.length == 2) {
		var negative = arr [0].replace (/\!$/, '');
		if (negative == arr [0]) {
			negative = false;
		} else {
			arr [0] = negative;
			negative = true;
		}
		try {
			if (negative) {
				if (nodes.make_string (el.getAttribute (arr [0])) != arr [1])
					return true;

				if (nodes.make_string (el [arr [0]]) != arr [1])
					return true;
			} else {
				if (nodes.make_string (el.getAttribute (arr [0])) == arr [1])
					return true;

				if (nodes.make_string (el [arr [0]]) == arr [1])
					return true;
			}
		} catch (e) {
		}
	}
	return false;
}

nodes.prototype.make_string = function (val) {
	switch (typeof val) {
	case 'undefined':
	case 'null':
		return '';
	case 'object':
		if (val === null)
			return '';
	}

	return val;
}

nodes.prototype.is_node = function (node) {
/*
	nodeType:
	3 == text-node (space, tab, newline)
	8 == comment
*/

	if (node != null && typeof node != 'undefined')
		if (typeof node.nodeType != 'undefined') {
			var nodeType = node.nodeType.toString ();
			if (nodeType !== '3' && nodeType !=='8')
				return true;
		}

	return false;
}

nodes.prototype.getPreviousSibling = function (p, cond) {
	var s = p.previousSibling;
	if (s === null)
		return false;

	if (typeof cond != 'undefined')
		if (this.check_condition (s, cond))
			return s;

	if (!this.is_node (s))
		return this.getPreviousSibling (s, cond);
	else
		return s;
}

nodes.prototype.getNextSibling = function (p, cond) {
	var s = p.nextSibling;
	if (s === null)
		return false;

	if (typeof cond != 'undefined')
		if (this.check_condition (s))
			return s;

	if (!this.is_node (s))
		return this.getNextSibling (s, cond);
	else
		return s;
}

var nodes = new nodes ();
