// Limit the HoverCells to a single table by specifying the ID here
var tableID = "";

// Set the class name for the HoverCells
var HoverCellClass = "HoverCell HoverCell"; // Append "HoverCell" to the existing class name (1 class on hover)
//var HoverCellClass = " HoverCell"; // Add the class HoverCell (2 classes on hover)

// Choose Which Coordinating Cells Are Hovered
var coordinating = 1; // By URL, other cells with a link to the same URL are highlighted
//var coordinating = 2; // By any class, other cells sharing any class name are highlighted
//var coordinating = 3; // By all classes, other cells sharing all class names are highlighted
// Comment all three of the above to only highlight the cell the mouse is in and not any coordinating cells

/*
	Initialize the hover cell script.  Sets the appropriate event handlers on links inside table cells.
*/
function initHoverCells()
{
	var cells;
	if (tableID == "")
		cells = document.getElementsByTagName("td");
	else
		cells = document.getElementById(tableID).getElementsByTagName("td");

	for (var i = 0; i < cells.length; i++)
	{
		var hyperlinks = cells[i].getElementsByTagName("a");
		if (hyperlinks.length > 0)
		{
			cells[i].onmouseover = hovercellhandlerCell;
			cells[i].onmouseout = hovercellhandlerCell;
			cells[i].onclick = hovercellclick;
			for (var j = 0; j < hyperlinks.length; j++)
			{
				hyperlinks[j].onmouseover = hovercellhandlerLink;
				hyperlinks[j].onmouseout = hovercellhandlerLink;
			}
		}
	}
}
// Call the initialized onload
window.onload = initHoverCells;

/*
	Event Handler for OnMouseOver and OnMouseOut of HoverCell hyperlinks
*/
function hovercellhandlerLink(e)
{
	if (!e) 
		var e = window.event;

	if (e.target) 
		elem = e.target;
	else if (e.srcElement) 
		elem = e.srcElement;

	if (elem.nodeType == 3) // silly Safari
		elem = elem.parentNode;

	if (coordinating == 1)
		HighlightCellsByURL(elem.href, e.type);
	else if (coordinating == 2)
		HighlightCellsByAnyClass(elem.className, e.type);
	else if (coordinating == 3)
		HighlightCellsByAllClasses(elem.className, e.type);
	else
	{
		var cell = elem.parentNode;
		if (cell.tagName.toLowerCase() == "td")
		{
			if (e.type == "mouseover")
				cell.className += HoverCellClass;
			else
				cell.className = cell.className.replace(HoverCellClass, "");
		}		
	}
}

/*
	Event Handler for OnMouseOver and OnMouseOut of HoverCell table cells
*/
function hovercellhandlerCell(e)
{
	if (!e) 
		var e = window.event;

	if (e.target) 
		elem = e.target;
	else if (e.srcElement) 
		elem = e.srcElement;

	if (elem.nodeType == 3) // silly Safari
		elem = elem.parentNode;

	//alert (elem.tagName);
	if (elem.tagName.toLowerCase() == "td")
	{
		var hyperlinks = elem.getElementsByTagName("a");
		if (hyperlinks.length > 0)
		{
			var hyperlink = hyperlinks[0];
	
			if (coordinating == 1)
				HighlightCellsByURL(hyperlink.href, e.type);
			else if (coordinating == 2)
				HighlightCellsByAnyClass(elem.className, e.type);
			else if (coordinating == 3)
				HighlightCellsByAllClasses(elem.className, e.type);
			else
			{
				if (e.type == "mouseover")
					cell.className += HoverCellClass;
				else
					cell.className = cell.className.replace(HoverCellClass, "");
			}
		}
	}
}

/*
	Event Handler for OnClick of HoverCell table cells
*/
function hovercellclick(e)
{
	if (!e) 
		var e = window.event;

	if (e.target) 
		elem = e.target;
	else if (e.srcElement) 
		elem = e.srcElement;

	if (elem.nodeType == 3) // silly Safari
		elem = elem.parentNode;

	if (elem.tagName.toLowerCase() == "td")
	{
		var hyperlinks = elem.getElementsByTagName("a");
		window.location = hyperlinks[0].href;
	}
}

/*
	Highlight and Unhighlight cells that have a link to the given url

	Note: second parameter indicates highlight on vs. highlight off
*/
function HighlightCellsByURL(url, highlight)
{
	var hyperlinks = document.getElementsByTagName("a");
	for (var j = 0; j < hyperlinks.length; j++)
	{
		if (hyperlinks[j].href == url)
		{
			var cell = hyperlinks[j].parentNode;
			if (cell.tagName.toLowerCase() == "td")
			{
				if (highlight == "mouseover")
					cell.className += HoverCellClass;
				else
					cell.className = cell.className.replace(HoverCellClass, "");
			}
		}
	}
}

/*
	Highlight and Unhighlight cells that share at least one class with the class(es)

	Note: second parameter indicates highlight on vs. highlight off
*/
function HighlightCellsByAnyClass(srcClasses, highlight)
{
	var classes = srcClasses.split(" ");
	
	var hyperlinks = document.getElementsByTagName("a");
	for (var j = 0; j < hyperlinks.length; j++)
	{
		var cell = hyperlinks[j].parentNode;
		if (cell.tagName.toLowerCase() == "td")
		{
			var flag = false;
			for (var i = 0; i < classes.length; i++)
			{
				if (cell.className.indexOf(classes[i]) > -1)
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				if (highlight == "mouseover")
					cell.className += HoverCellClass;
				else
					cell.className = cell.className.replace(HoverCellClass, "");
			}
		}
	}
}

/*
	Highlight and Unhighlight cells that share at least one class with the class(es)

	Note: second parameter indicates highlight on vs. highlight off
*/
function HighlightCellsByAllClasses(srcClasses, highlight)
{
	var classes = srcClasses.split(" ");
	
	var hyperlinks = document.getElementsByTagName("a");
	for (var j = 0; j < hyperlinks.length; j++)
	{
		var cell = hyperlinks[j].parentNode;
		if (cell.tagName.toLowerCase() == "td")
		{
			var flag = true;
			for (var i = 0; i < classes.length; i++)
			{
				if (cell.className.indexOf(classes[i]) == -1)
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				if (highlight == "mouseover")
					cell.className += HoverCellClass;
				else
					cell.className = cell.className.replace(HoverCellClass, "");
			}
		}
	}
}
