/*
Summary:  Manipulate the class attribute of an element.
Version:  1.0
Author:   John Bentley (www.softmake.com.au)     
Depends on:

Usage Requirments:

Usage Example: 
*/

/*
Summary:  Adds the supplied class to the element.
Remarks:  We are careful to add a class when the element already has 
          multiple classes.
          We are also careful NOT to add the same class multiple times 
Example:  addClassToElement("coolness", ele)          */
function addClassToElement(classString, element) {
  // If classString is not already in class then add it.
  if (element.className.indexOf(classString) == -1) {
    element.className = element.className + " " + classString;
  }
}

/*
Summary: Removes the class name from the element
Remarks:  We are careful to remove a class when the element already has 
          multiple classes */
function removeClassFromElement(classString, element) {
  var re = new RegExp(classString + "\s*");
  element.className = element.className.replace(re,"");
}  

/*
Returns: an array of elements that contain the searchClass.
Params: nodeToStartSearch & tag optional
Source: Dustin Diaz, Top 10 Javascript functions of all time,
        http://www.dustindiaz.com/top-ten-javascript/ */
function getElementsByClass(searchClass, nodeToStartSearch, tag) {
	var classElements = new Array();
	if ( nodeToStartSearch == null )
		nodeToStartSearch = document;
	if ( tag == null )
		tag = '*';
	var els = nodeToStartSearch.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

