function getActiveStyle(elem)
{
	var retval;
	
	if (window.getComputedStyle)
		retval = window.getComputedStyle(elem, null);
	else if (elem.currentStyle)
		retval = elem.currentStyle;
		
	return retval;
}

function findCSSStyle(styleSheet, selectorText)
{
	var rules = (styleSheet.cssRules) ? styleSheet.cssRules : styleSheet.rules;
	for (var i = 0; i < rules.length; i++)
	{
		if (rules[i].type && rules[i].type == 3)
		{
			var style = findCSSStyle(rules[i].styleSheet, selectorText);
			if (style)
				return style;
		}
		else if (rules[i].selectorText.toLowerCase().substr(0, selectorText.length) == selectorText)
		{
			return rules[i].style;
		}
	}
	
	if (styleSheet.imports)
	{
		for (var i = 0; i < styleSheet.imports.length; i++)
		{
			var style = findCSSStyle(styleSheet.imports[i], selectorText);
			if (style)
				return style;
		}
	}
}

function getCSSStyle(selectorText)
{
	for (var i = 0; i < document.styleSheets.length; i++)
	{
		var style = findCSSStyle(document.styleSheets[i], selectorText);
		if (style)
			return style;
	}
}

function isParent(elem, parent)
{
	var retval = false;
	
	if (elem.parentNode)
	{
		if (elem.parentNode == parent)
			retval = true;
		else
			retval = isParent(elem.parentNode, parent);
	}
	
	return retval;
}

function hiliteButton(elem)
{
	if (popupElem && !isParent(elem, popupElem))
		closePopup();
		
	elem.style.backgroundColor = getCSSStyle("td.navbutton_hilite").backgroundColor;
}

function restoreButton(elem)
{
	elem.style.backgroundColor = getCSSStyle("td.navbutton").backgroundColor;
}

function navigate(elem, url)
{
	restoreButton(elem);
	window.location = url;
}

var popupElem;

function popup(id, btnidx)
{
	popupElem = document.getElementById(id);
	var top = parseInt(getActiveStyle(document.getElementById("bodydiv")).top);
	var left = parseInt(getCSSStyle("td.navspace").width) + 1;
	var btnWidth = parseInt(getCSSStyle("td.navbutton").width);
	popupElem.style.top = top + "px";
	popupElem.style.left = (left + (btnWidth + 1) * btnidx) + "px";
	popupElem.style.visibility = "visible";
}

function closePopup()
{
	if (popupElem)
	{
		popupElem.style.visibility = "hidden";
		popupElem = 0;
	}
}
