/*
//	Adam's Common JavaScript Library :: Node Traversal-related Functions
//	Copyright (c) 2010-2011 - Adam Rehn
*/

function filterChildElementsByCallback(parent, filterCallback, callback, recursive)
{
	if (parent && typeof(parent) != 'undefined' && parent.hasChildNodes())
	{
		var children = parent.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (typeof(children[i].tagName) != 'undefined')
			{
				//If the tag filter is matched, run the callback
				if (filterCallback(children[i]))
					callback(children[i]);
				
				//If the element has children and we are in recursive mode, recurse now
				if (children[i].hasChildNodes() && recursive)
					filterChildElementsByCallback(children[i], filterCallback, callback, true);
			}
		}
	}
}

function filterChildElements(parent, tagFilter, callback, recursive)
{
	//Call filterChildElementsByCallback with a callback that checks for the specified tag
	filterChildElementsByCallback
	(
		parent,
		function(elem) { return (elem.tagName.toLowerCase() == tagFilter.toLowerCase()); },
		callback,
		recursive
	);
}

function elemHasClass(elem, className)
{
	if (valid(elem) && valid(className))
	{
		//Retrieve the list of classes that the element is
		var classList = (!isIE() ? elem.getAttribute('class') : elem.className);
		
		//If we have a class list, split it
		if (classList)
		{
			//Split the list of classes into an array
			var classes = classList.split(' ');
			if (classes && classes.indexOf(className) != -1)
			{
				//The element does have the specified class set!
				return true;
			}
		}
	}
	
	return false;
}

