/***********************************************/
/* Script: CSSMenu                             */
/* Version: 1.5                                */
/* File Name: css_menu.js                      */
/***********************************************/

// stack variables to store current menus, items and iframes
var css_menuStack = new Array();
var css_itemStack = new Array();
var css_frameStack = new Array();

// timing variables for hiding child menus
var css_hideTimer = null;
var css_hideDelay = 500;

// browser ID variables
var css_ua = navigator.userAgent.toLowerCase();
var css_isIE = (document.all && !window.opera);
var css_isIE5 = (parseFloat(css_ua.substr(css_ua.indexOf("msie") + 5, 3)) < 5.5);
var css_isW3C = (!css_isIE && !window.opera);

// allow for stack functions on older browsers
if (!css_menuStack.push)
{
	Array.prototype.push = function()
	{
		for (var i=0; i<arguments.length; i++)
			this[this.length] = arguments[i];
		return this.length;
	};
	
	Array.prototype.pop = function()
	{
		var value = this[this.length - 1];
		this.length--;
		return value;
	};
}

function css_getLeft(elem)
{
	// return the absolute left coordinate of a given element
	var left = elem.offsetLeft;
	while ((elem = elem.offsetParent)) left += elem.offsetLeft;
	return left;
}

function css_getTop(elem)
{
	// return the absolute top coordinate of a given element
	var top = elem.offsetTop;
	while ((elem = elem.offsetParent)) top += elem.offsetTop;
	return top;
}

function css_addClass(elem, className)
{
	// check for and initialize the class property
	if (!elem.className) elem.className = "";
	if (elem.className.indexOf(className) != -1) return;
	
	// append the new class to the element's class property
	if (elem.className.length > 0) elem.className += " ";
	elem.className += className;
}

function css_removeClass(elem, className)
{
	// check for having to remove the given class
	if (!elem.className || elem.className == "") return;
	if (elem.className.indexOf(className) == -1) return;
	
	// remove the given class from the element's class property
	var re = new RegExp("\s?" + className, "g");
	elem.className = elem.className.replace(re, "");
}

function css_replaceClass(elem, oldClassName, newClassName)
{
	// replace a given class with another using the available class functions
	css_removeClass(elem, oldClassName);
	css_addClass(elem, newClassName);
}

function css_createFrame()
{
	// create an iframe to show menus over form elements
	var elem = document.createElement("iframe");
	document.body.appendChild(elem);
	elem.src = "about:blank";
	elem.border = 0;
	elem.frameBorder = 0;
	elem.style.visibility = "hidden";
	elem.style.position = "absolute";
	elem.style.height = "0px";
	elem.style.width = "0px";
	elem.style.zIndex = 500;
	
	// make the iframe invisible and add it to the reusable iframes stack
	if (css_isIE5)
		elem.style.filter = "alpha(opacity=0)";
	else
		elem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
	css_frameStack.push(elem);
}

function css_hideAfter(level, flag)
{
	if (!document.getElementById) return; // only run on compatible browsers
	
	// make sure the menus don't close on their own
	css_stopTimer();
	var numOfMenus = css_menuStack.length;
	
	for (var i=numOfMenus-1; i>=level; i--)
	{
		// hide the menu and optionally play a transition in Internet Explorer
		var filter = (!flag || i != level) ? css_findTransition(css_menuStack[i]) : null;
		if (filter) css_menuStack[i].filters[filter].apply();
		if (css_isIE && css_frameStack[i]) css_frameStack[i].style.visibility = "hidden";
		css_menuStack[i].style.visibility = "hidden";
		if (filter) css_menuStack[i].filters[filter].play();
		css_menuStack.pop();
			
		// unhighlight the item, perform any mouseout functions and remove the active item
		css_replaceClass(css_itemStack[i], "onState", "offState");
		if (css_itemStack[i].getAttribute("mouseOut")) eval(css_itemStack[i].getAttribute("mouseOut"));
		css_itemStack.pop();
	}
}

function css_showMenu(level, item, id, menuX, menuY)
{
	if (!document.getElementById) return; // only run on compatible browsers
	
	// reset the menus up to the given level
	var flag =  (item == css_itemStack[level]);
	css_hideAfter(level, flag);
	var menu = document.getElementById(id);
	css_replaceClass(item, "offState", "onState");
	
	// get the element that the menu's position will be relative to
	var posItem = item;
	if (item.tagName.toLowerCase() == "area")
	{
		var ref = item.id.substr(0, item.id.indexOf("Area"));
		posItem = document.images[ref];
	}
	
	// position the child menu
	menu.style.left = (css_getLeft(posItem) + menuX) + "px";
	menu.style.top = (css_getTop(posItem) + menuY) + "px";
	menu.style.zIndex = level == 0 ? 1000 : parseInt(css_menuStack[level - 1].style.zIndex) + 1;
	
	// show the child menu while optionally playing a transition in Internet Explorer
	var filter = flag ? null : css_findTransition(menu);
	if (filter) menu.filters[filter].apply();
	menu.style.visibility = "visible";
	if (filter) menu.filters[filter].play();
	
	// get the document body's size and scrolling offsets
	var rightEdge, bottomEdge;
	if (window.innerHeight)
	{
		// for Mozilla, Netscape, Opera, etc.
		rightEdge = window.innerWidth + window.pageXOffset;
		bottomEdge = window.innerHeight + window.pageYOffset;
	}
	else
	{
		// for Internet Explorer
		var doc = (document.documentElement && document.documentElement.clientHeight) ? document.documentElement : document.body;
		rightEdge = doc.clientWidth + doc.scrollLeft;
		bottomEdge = doc.clientHeight + doc.scrollTop;
	}
	
	// adjust the menu's position if it's off the viewport's visible area
	if (menu.offsetLeft + menu.offsetWidth > rightEdge)
		menu.style.left = (menu.offsetLeft - (menu.offsetLeft + menu.offsetWidth - rightEdge)) + "px";
	if (menu.offsetTop + menu.offsetHeight > bottomEdge)
		menu.style.top = (menu.offsetTop - (menu.offsetTop + menu.offsetHeight - bottomEdge)) + "px";
			
	if (css_isIE && css_ua.indexOf("mac_powerpc") == -1)
	{
		// make sure that the menu will appear over form elements with an iframe
		if (!css_frameStack[level]) css_createFrame();
		css_frameStack[level].style.left = menu.style.left;
		css_frameStack[level].style.top = menu.style.top;
		css_frameStack[level].style.height = menu.offsetHeight + "px";
		css_frameStack[level].style.width = menu.offsetWidth + "px";
		css_frameStack[level].style.visibility = "visible";
	}
	
	// save the active menu and item
	css_menuStack.push(menu);
	css_itemStack.push(item);
}

function css_findTransition(elem)
{
	// only continue if the browser supports filters
	if (!elem.filters) return null;
	var transitions = "barn|blendtrans|blinds|checkerboard|fade|gradientwipe|inset|iris|pixelate|radialwipe|randombars|randomdissolve|revealtrans|slide|spiral|stretch|strips|wheel|zigzag";
	var filter = null;
	var name;
	
	// check the elements filters for a transition
	for (var i in elem.filters)
	{
		name = i.split(".")[i.split(".").length - 1].toLowerCase();
		if (transitions.indexOf(name) != -1)
		{
			filter = i;
			break;
		}
	}
	return filter;
}

function css_startTimer()
{
	// start timer to hide all sub menus
	css_stopTimer();
	css_hideTimer = setTimeout("css_hideAfter(0)", css_hideDelay);
}

function css_stopTimer()
{
	// stop timer so menus will stay open
	clearTimeout(css_hideTimer);
}