// TODO: homeOn, homeOff


// ********************************************************
// 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;
}

// ********************************************************
// 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);
}

