/* Unobtrusive table row highlighting script
 * Copyright 2004 Alexis Shirtliff - http://www.sliff.co.uk
 * (with the exception of addEvent function by Scott Andrew)
 * This script may not be re-used, modified or sold without 
 * written permission
 */
function addEvent(elm, evType, fn, useCapture)
		// addEvent and removeEvent
		// cross-browser event handling for IE5+,  NS6 and Mozilla
		// By Scott Andrew
	{
	  if (elm.addEventListener){
    	elm.addEventListener(evType, fn, useCapture);
	    return true;
	  } else if (elm.attachEvent){
    	var r = elm.attachEvent("on"+evType, fn);
	    return r;
	  } else {
    	alert("Handler could not be removed");
	  }
	} 
	
addEvent(window, "load", cellhighlight_init);

function cellhighlight_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("highlighter") != -1) && (thisTbl.id)) {
            ts_makeHi(thisTbl);
        }
    }
}

function doHi(e) {
	if (window.event && window.event.srcElement){
		row = window.event.srcElement
	} else if (e && e.target) {
		row = e.target
	}
    if (!row) return;
	if (!document.getElementsByTagName) return;
	// As we're probably going to actually mouseover a td we need to walk back up the 
	// DOM tree to fetch 
	var strNode = "Flibble";
	//strNode = row.nodeName.toString;
	strNode = row.nodeName.toLowerCase();
	fetchID = '';
	if (strNode != 'tr') {
		// Walk up
		gotTR = false;
		curNode = row;
		while (!gotTR) {
			curNode = curNode.parentNode;
			if (curNode.nodeName.toLowerCase() == 'tr') {
				gotTR = true;
				fetchID = curNode.id;
			}
			if (curNode == document) {
				// Exit loop accordingly
				gotTR = true;
			}
		}
	} else {
		// Get this nodes id
		fetchID = row.id;
	}
	if (fetchID != '') {
		//clearHighRows();
		document.getElementById(fetchID).className = 'row_over';
	}
}

function doLo(e) {
	if (window.event && window.event.srcElement){
		row = window.event.srcElement
	} else if (e && e.target) {
		row = e.target
	}
    if (!row) return;
	if (!document.getElementsByTagName) return;
	// As we're probably going to actually mouseover a td we need to walk back up the 
	// DOM tree to fetch 
	var strNode = "Flibble";
	//strNode = row.nodeName.toString;
	strNode = row.nodeName.toLowerCase();
	fetchID = '';
	if (strNode != 'tr') {
		// Walk up
		gotTR = false;
		curNode = row;
		while (!gotTR) {
			curNode = curNode.parentNode;
			if (curNode.nodeName.toLowerCase() == 'tr') {
				gotTR = true;
				fetchID = curNode.id;
			}
			if (curNode == document) {
				// Exit loop accordingly
				gotTR = true;
			}
		}
	} else {
		// Get this nodes id
		fetchID = row.id;
	}
	if (fetchID != '') {
		//clearHighRows();
		document.getElementById(fetchID).className = 'row_out';
	}
}

function ts_makeHi(table) {
	for (var i=0;i<table.rows.length;i++) {
		var thisRow = table.rows[i];
		if (thisRow.id) {
			addEvent(thisRow, "mouseover", doHi);
			addEvent(thisRow, "mouseout", doLo);
		}
	}
}
