/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 * Programmer:  Jean-Charles Miron (PWGSC)
 * Date:        9th of June 2009
 * Description: This javascript file contains general functions that are used by
 *              several javascript objects.
 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
 
 /* 
 * Setup our function that will enable us to look for one or more nodes with a specific class. 
 */
function addGetNodesByClassName(){		
	document.getNodesByClassName = function(className,node){
		if( node == null ) node = document;		
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = node.getElementsByTagName("*");
		var results = [];
		
		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}	
		
		return results;
	}
}

/* 
 * Thsi function detects if CSS is enabled or not. This is to skip the javascript modifications if the CSS is 
 * disabled. We add a div with the style position as absolute to the document. Then we check if the window has 
 * computed the div's style. If it did, we know CSS is enabled. 
 */
function isCSSEnabled(){
	var cssenabled = true;
	
	// Create a div, set the position to absolute and add it to the document
	var testcss = document.createElement('div');
	testcss.style.position = 'absolute';
	document.getElementsByTagName('body')[0].appendChild(testcss);
	
	if(testcss.currentStyle) var currstyle = testcss.currentStyle['position'];
	else if(window.getComputedStyle) var currstyle = document.defaultView.getComputedStyle(testcss, null).getPropertyValue('position');
	
	// Did the style render? 
	cssdisabled = ((currstyle == 'static') ? false : true);
	
	//Remove the div element from the document
	document.getElementsByTagName('body')[0].removeChild(testcss);
	
	return cssenabled;
}

/* 
 * This function fixes the firefox bug of having empty text nodes between list items.
 * The function looks at the first child node of the node given and returns it if its not
 * a text node. If it is a text node, it will return the second child node.
 */
function ffGetFirstChildNodeFix( node ){
	if(node.childNodes[0].nodeType == 3) return node.childNodes[1];
	else return node.childNodes[0];		
}

