/*

Copyright (c) 2005, Daniel Juliano
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.
    * The names of contributors to this source code may not be used to endorse 
      or promote products derived from this software without specific prior 
      written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*

Script Summary:

Row highlighting is used in the results tables for List, Sort and Search.  As the 
user moves the mouse across a results table, the css classes for all cells in the
table row are switched out.

*/


var highlightSelected = -1;
var highlightPosition = -1;
var highlightClasses = Array();
var highlightID = null;
var highlightIDs = Array();

// Apply highlighting to a particular row, defined by the array cssClassNames:
//  [0] = none (normal / mouseout)
//  [1] = row moused over
//  [2] = row selected

function highlightInit(cssClassNames, selectedIndex) {
	
	highlightPosition = -1;
	highlightSelected = -1;
	highlightID = null;
	
	highlightClasses = cssClassNames;
	if (selectedIndex) highlightPosigion = selectedIndex;
}

function highlightRow(element, highlight) {
	
	// Element id's follow the pattern name_row_column.
	// Example: td_01_03
	highlightPosition = parseInt(element.id.split("_")[1]);
	if (highlightPosition == highlightSelected) return;
	
	// Apply highlight to each cell in the row.
	var td = element.parentNode.childNodes;
	for (var i = 0; i < td.length; i++) {
		if (td[i].nodeName == "TD") {
			td[i].className = highlightClasses[highlight];
		}
	}
}

function highlightSelect(id) {
	
	if (highlightPosition == -1) return;
	
	if (id == null) {
		// On 'Sort' page move, restore the previously selected element.
		if (highlightID == null || highlightID == "") return;
		id = highlightID;
		
	} else if(id.indexOf("_") == -1) {
		// Allow retrieval of previously selected element when switching 
		// between utilities (Sort, Search, List).
		id = highlightRetrieve(id);
		
	} else {
		// If there's an existing selection, remove all highlighting from that row.
		if (highlightID != null && highlightID != "") {
			var td = document.getElementById(highlightID).parentNode.childNodes;
			for (var i = 0; i < td.length; i++) td[i].className = highlightClasses[0];
		}
		highlightSelected = highlightPosition;
		highlightStore(id);
	}
	
	// Switch formatting for the highlighted row.
	var td = document.getElementById(id).parentNode.childNodes;
	for (var i = 0; i < td.length; i++) {
		if (td[i].nodeName == "TD") {
			td[i].className = highlightClasses[2];
		}
	}
}

function highlightStore(id) {
	var prefix = id.split("_")[0];
	
	// If the prefix already has a storage spot,
	// store the new full id at that location.
	for (var i = 0; i < highlightIDs.length; i++) {
		if (prefix == highlightIDs[i][0]) {
			highlightIDs[i][1] = id;
			highlightID = id;
			return;
		}
	}
	
	// Otherwise create a new storage element.
	var index = highlightIDs.length;
	highlightIDs[index] = Array();
	highlightIDs[index][0] = prefix;
	highlightIDs[index][1] = id;
	highlightID = id;
}
function highlightRetrieve(id) {
	var prefix = id.split("_")[0];
	
	// Find the storage for matching prefix.
	for (var i = 0; i < highlightIDs.length; i++) {
		if (prefix == highlightIDs[i][0]) {
			return document.getElementById(highlightIDs[i][1]);
		}
	}
	return null;
}


