//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	} 
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/* ===============================================
 * ================ COMMON =======================
 * Some common functions for the basic JavaScript objects
 *
 * ===============================================
 */

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, "");
}

// Define methods for the Array data structure.
Array.prototype.indexOf = function(item, start) 
{ 
	for (var i = (start || 0); i < this.length; i++) 
	{ 
		if (this[i] == item) 
		{ 
			return i; 
		} 
	} 
	return -1; 
}

/* ===============================================
 * ============= GET ELEMENTS ====================
 * Methods to help retrieve elements from the DOM.
 *
 * ===============================================
 */

/*
 * Gets all the Elements by given Class Name. 
 * 
 * @param string searchClass - Name of the css class to look for.
 * @param object node - (optional) The node you want to start from. Defaults to 'document' if none is specified. 
 * @param string tagName - (optional) Limit  results by adding a tagName. Defaults to '*' if none is specified.
 * 
 * @returns array - Returns an array containing all the nodes given by the specified className.
 */ 
function getElementsByClass(searchClass, node, tagName)
{
	var	classElements =	new	Array();
	if (node == null)
		node = document;
	if (tagName ==	null)
		tagName	= '*';
	
	var	els	= node.getElementsByTagName(tagName);
	var	elsLen = els.length;
	
	for	(i = 0,	j =	0; i < elsLen; i++)
	{
		if (hasClass(els[i], searchClass))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* ===============================================
 * ================ STYLING ======================
 * Methods for manipulating an element's styles (CSS).
 *
 * ===============================================
 */

function getClasses(element)
{
	return element.className.trim().split(/\s+/);
}

function hasClass(element, className)
{
	return getClasses(element).indexOf(className) != -1;
}

function addClass(element, className)
{
    var classes = getClasses(element);
    
    if (classes.indexOf(className) == -1)
    {
        classes.push(className);
        element.className = classes.join(' ');
    }
}

function removeClass(element, className)
{
    var classes = getClasses(element);
    var index = classes.indexOf(className);
    
    if (index != -1)
    {
        classes.splice(index, 1);
        element.className = classes.join(' ');
    }
}

/* ===============================================
 * ================ The EFFECT ======================
 * Methods for adding the Row Highlight Effect.
 *
 * ===============================================
 */

// The methods from my Redux Example
function initRowHighlighting()
{
	if (!document.getElementsByTagName)
		{ return; }

	var tables = getElementsByClass('highlightTable', document, 'table');
	
	for(var i = 0; i < tables.length; i++)
	{
		var table = tables[i];
		//Make sure to use th tags for header row.
		attachRowMouseEvents(table.getElementsByTagName('tr'));
	}
}
function attachRowMouseEvents(rows)
{
	for(var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		
			row.onmouseover = function() { addClass(this, 'highlight'); }
			row.onmouseout = function() { removeClass(this, 'highlight'); }
	
	}
}

addLoadEvent(initRowHighlighting);
