
/**************************************************
* Name: hovercell.js
* Author: Whod'ya think?
* Modified: 20071112
* Overview: Highlights matching hoz and vert cells
* Copyright: Whoever
* Notes: The name is sexy, the code is sexy and so
* is the author.
**************************************************/

// Whether Hijax can operate in this client
var sanityCheck = false;

// Add the sanity test as the first Hijax call
winAddLoadEvent (testSanity);

// Add the hovercell Hijaxing
winAddLoadEvent (hovercellHijax);

// Adds window load events safely
function winAddLoadEvent (func) {
	var oldonload = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload ();
			func ();
		}
	}
}

// Checks the DOM for basic functionality
function testSanity () {
	if (document.getElementById) {
		sanityCheck = true;
	} else {
		sanityCheck = false;
	}
}

// This does the actual Hijaxing of the table
function hovercellHijax () {
	if (!sanityCheck) return false;
	if (!document.getElementById ("hovercell")) return false;

	var theTable = document.getElementById ("hovercell");

	// Process the table headers, but assign no events
	var theCells = theTable.getElementsByTagName ("th");
	for (var i = 0; i < theCells.length; i++) {
		// Check if the cell has an axis specified
		if (theCells[i].getAttribute ("axis").length > 2) {
			// Extract the column and row from the axis information
			var theAxis = theCells[i].getAttribute ("axis").split ("#");

			// Store the column and row data in the cell object for ultra-fast
			// identification later, at the price of slightly slower loading
			theCells[i].col = theAxis[0];
			theCells[i].row = theAxis[1];
		}
	}

	// Process the table dimensions and assign events
	var theCells = theTable.getElementsByTagName ("td");
	for (var i = 0; i < theCells.length; i++) {
		// Check if the cell has an axis specified
		if (theCells[i].getAttribute ("axis").length > 2) {
			// Extract the column and row from the axis information
			var theAxis = theCells[i].getAttribute ("axis").split ("#");

			// Store the column and row data in the cell object
			theCells[i].col = theAxis[0];
			theCells[i].row = theAxis[1];

			// Add event hooks in to the cell to operate the relevant axis' using the stored data
			theCells[i].onmouseover = function () { hovercellGlow (this); }
			theCells[i].onmouseout = function () { hovercellUnglow (this); }
		}
	}
}

// This function glows all matching cells
function hovercellGlow (what) {
	var theTable = document.getElementById ("hovercell");
	var theCells = theTable.getElementsByTagName ("th");

	for (var i = 0; i < theCells.length; i++) {
		// Check if the cell matches the row or column, highlight it
		if (theCells[i].row == what.row || theCells[i].col == what.col)
			theCells[i].className = "hovercell_highlight"; // = theCells[i].className + " hovercell_highlight";
	}
}

// This function unglows all cells
function hovercellUnglow (what) {
	var theTable = document.getElementById ("hovercell");
	var theCells = theTable.getElementsByTagName ("th");

	for (var i = 0; i < theCells.length; i++) {
		// Check if the cell matches the row or column, then unhighlight it
		if (theCells[i].row == what.row || theCells[i].col == what.col)
			theCells[i].className = "pme-header"; // = theCells[i].className.replace(/hovercell_highlight/, "");
	}
}
