function homeOn(img){
  hiliteImage(img, "on");
}
function homeOff(img){
  unhiliteImage(img);
}


// ********************************************************
// Functions for handling thread icon rollovers
// ********************************************************

function threadIconOver(icon) {
	hiliteImage(icon, 'over');
	icon.reEntered = true;
	// Kludge: hide the previous popup just in case it gets stuck
	if (window.lastThreadPopupNum) {
		setThreadPopupVisible(window.lastThreadPopupNum, false);
	}
}

function threadIconOut(icon) {
	unhiliteImage(icon);
}

function threadIconClick(icon) {
	if (icon) {
		num = extractLastToken('_',icon.id);
		setThreadPopupVisible(num, true);
		icon.reEntered = false;
		// Kludge: setup to hide the popup just in case it gets stuck
		window.lastThreadPopupNum = num;
	}
}

function threadPopupOver(popup) {
	hiliteImage(popup, 'on');
	num = extractLastToken('_',popup.id);
	hiliteImage(getThreadIconElement(num), 'over'); // Re-hilite the icon
	// Need to store globally so we can turn it off, 
	// because IE doesn't always behave (see getEventNextTarget)
	window.lastThreadPopupOver = popup;
}

function threadPopupOut(popup) {
	unhiliteImage(popup);
	// Need to store globally so we can turn it off, 
	// because IE doesn't always behave (see getEventNextTarget)
	window.lastThreadPopupOut = popup;
	setTimeout("checkHidePopup()",100);
}

function checkHidePopup() {
	if (window.lastThreadPopupOver.id == window.lastThreadPopupOut.id) {
		num = extractLastToken('_',window.lastThreadPopupOver.id);
		setThreadPopupVisible(num, false);
		icon = getThreadIconElement(num);
		if (!icon.reEntered) {
			unhiliteImage(icon);
		}
	}
}

// Helper functions for thread icon rollovers

function setThreadPopupVisible(num, visible) {
	popup = getThreadPopupElementContainer(num);
	if (popup) {
		popup.style.display = visible ? 'inline' : 'none';
	}
}

function getThreadIconElement(num) {
	return document.getElementById('thread_icon_'+num);
}

function getThreadPopupElement(num, pos) {
	return document.getElementById('thread_popup_'+pos+'_'+num);
}

function getThreadPopupElementContainer(num) {
	return document.getElementById('thread_popup_'+num);
}

function hiliteImage(imgElem, suffix) {
	if (imgElem && !imgElem.hilited) {
		imgElem.offSrc = imgElem.src
		imgElem.src = imgElem.offSrc.substring(0, imgElem.offSrc.indexOf('off.gif')) + suffix + '.gif';
		imgElem.hilited = true;
	}
}

function unhiliteImage(imgElem) {
	if (imgElem && imgElem.offSrc) {
		imgElem.src = imgElem.offSrc;
		imgElem.hilited = false;
	}
}

function trace(msg) {
	document.getElementById('debug').innerHTML = msg;
}


// ********************************************************
// Functions for handling essay row rollover
// ********************************************************

/*
function rowOn(theRow) {
	highlightRow(theRow, true);
	showDescription(theRow);
	theRow.defaultCursor = theRow.style.cursor; // Remember the default cursor
	theRow.style.cursor = 'pointer';
}

function rowOff(theRow) {
	highlightRow(theRow, false);
	hideDescription(theRow);
	if (theRow.defaultCursor) {
		theRow.style.cursor = theRow.defaultCursor;
	}
}
*/
function threadOver(theThreadIcon) {
	hiliteImage(theThreadIcon, 'over');
}
function threadOut(theThreadIcon) {
	unhiliteImage(theThreadIcon);
}
function rowOver(theRow) {
	theRow.style.cursor = 'pointer';
	highlightRow(theRow, true);
}
function rowOut(theRow) {
	highlightRow(theRow, false);
}

function highlightRow(theRow, mode) {
	var child = null;
	divs = theRow.getElementsByTagName('TD');
	for (i=0;i<divs.length;i++) {
		child = divs[i];
		if (child.className.indexOf('text_cell') != -1) {
			highlightTextCell(child, mode);
		} else if (child.className.indexOf('empty_cell') != -1) {
			highlightEmptyCell(child, mode);
		} else if (child.className.indexOf('gathering_cell') != -1) {
			highlightGatheringCell(child, mode);
		}
	}
}

function highlightTextCell(cell, turnOn) {
	if (turnOn) {
		cell.tmpBackgroundColor = cell.style.backgroundColor;
		cell.style.backgroundColor = darkenColor(cell.tmpBackgroundColor);
		cell.tmpColor = cell.style.color;
		cell.style.color = "#ffffff";
	} else {
		if (cell.tmpBackgroundColor != null) cell.style.backgroundColor = cell.tmpBackgroundColor;
		if (cell.tmpColor != null) cell.style.color = cell.tmpColor;
	}
}

function highlightEmptyCell(cell, turnOn) {
	if (turnOn) {
		cell.tmpBackgroundColor = cell.style.backgroundColor;
		cell.style.backgroundColor = fadeColor(cell.tmpBackgroundColor);
	} else {
		if (cell.tmpBackgroundColor != null) cell.style.backgroundColor = cell.tmpBackgroundColor;
	}
}

function highlightGatheringCell(cell, turnOn) {
	if (turnOn) {
		cell.tmpBackgroundColor = cell.style.backgroundColor;
		cell.style.backgroundColor = fadeColor(cell.tmpBackgroundColor);
	} else {
		if (cell.tmpBackgroundColor != null) cell.style.backgroundColor = cell.tmpBackgroundColor;
	}
}

function fadeColor(rgbColor) {
	return rgbToString(lightenRGB(parseRGB(rgbColor), 0.6));
}

function darkenColor(rgbColor) {
	return rgbToString(darkenRGB(parseRGB(rgbColor), 0.6));
}

function showDescription(theRow) {
	num = extractLastToken('_',theRow.id);
	window.essayDescription = document.getElementById('essay_description_'+num);
	//window.essayDescription.style.display = 'inline';
}

function hideDescription(theRow) {
	if (window.essayDescription) {
		//window.essayDescription.style.display = 'none';
	}
}


// ********************************************************
// Utility functions
// ******************************************************** 

function extractLastToken(delimiter,str) {
	if (str.indexOf(delimiter) == -1) {
		return str;
	}
	return str.substring(str.lastIndexOf(delimiter)+1);
}

function getEventTarget(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	return (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
}

// For mouseOut events, returns the new element INTO which the mouse has rolled
// IE is inconsistent in implementation, so don't use this
function getEventNextTarget(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	return (evt.relatedTarget) ? evt.relatedTarget : ((evt.toElement) ? evt.toElement : null);
}

