/* Dynamic menu code, by Mike Hall (tweaked by WebTOP) -- see http://www.brainjar.com/ */

//Finds the current subpage type (whereto one should go in a new module)
function subpage() {
	return top.menuLink;
}

//Sets subpage type for links to other modules; returns true
function setSubpage(page) {
	top.menuLink=page;
	return true;
}

function getPageObject(name) {
	if(browser.isIE) return eval('document.all.'+name);
	else return document.getElementById(name);
}

function setVisible(obj,show) {
	if(show) {
		obj.width=obj.width0;
		obj.height=obj.height0;
	}	else {
		obj.width0=obj.width;
		obj.height0=obj.height;
		obj.width=obj.height=1;
	}
}

//Hides or un-hides the module.
function setModuleVisible(show) {
	if(getPageObject('vrml'))
		setVisible(getPageObject('vrml'),show);
	document.styleSheets[document.styleSheets.length - 1].addRule("samp", "visibility:"+(show?"visible":"hidden"));
}

//Creates an object of type Browser, representing the brand and version of user agent
//Only checks for IE and Netscape 6+.
function Browser() {
	var agent, i, str;
	this.isIE    = false;  // Internet Explorer
	this.isNS    = false;  // Netscape
	this.version = null;
	agent = navigator.userAgent;
	if ((i = agent.indexOf(str="MSIE")) >= 0) {
		this.isIE = true;
		this.version = parseFloat(agent.substr(i + str.length));
	} else if ((i = agent.indexOf(str="Netscape6/")) >= 0) {
		this.isNS = true;
		this.version = parseFloat(agent.substr(i + str.length));
	}
}

var browser = new Browser();

// Global variable for tracking the currently active button.
var activeButton = null;

if (browser.isIE) {
	document.styleSheets[document.styleSheets.length - 1].addRule("#menuBar", "padding-top:4px");
	document.styleSheets[document.styleSheets.length - 1].addRule("#menuBar", "padding-bottom:3px");
}

//Listens to mouse-clicks, and destroys any active menu if click was not on a menu
if (browser.isIE) document.onmousedown = pageMousedown;
if (browser.isNS) document.addEventListener("mousedown", pageMousedown, true);
function pageMousedown(event) {
	var className;
	//Get CSS class of target
	if (browser.isIE) className = window.event.srcElement.className;
	if (browser.isNS)
		className = (event.target.className ? event.target.className : event.target.parentNode.className);
	//If it doesn't start with 'menu', kill any active menu
	if (className.indexOf("menu") && activeButton) resetButton(activeButton);
}

//Responds to user clicking a menu button.  Returns false if it has done all the handling necessary
function buttonClick(button, menuName,execute,menuLink) {
	if(execute=='true'){
		//We don't need to do anything; just return true and the hyperlink goes!
		//This implies we may not even need this function for non-menu buttons...x
		return true;
	} else {
		if(!menuName) return false;
		else {
			// Blur focus from the link to remove that annoying outline.
			button.blur();

			// Associate the named menu to this button if not already done.
			if (!button.menu) button.menu = document.getElementById(menuName);

			// Reset the currently active button, if any.
			if(activeButton && activeButton!=button) resetButton(activeButton);
			// Toggle the button's state.  This does the actual work.
			if (button.isDepressed) resetButton(button);
			else depressButton(button);

			return false;
		}
	}
}

function buttonMouseover(button, menuName) {
	// If any other button menu is active, deactivate it and activate this one.
	if (activeButton && activeButton != button) {
		resetButton(activeButton);
		//If we have a menu, assert it
		if (menuName) buttonClick(button, menuName,0,null);
	}
}

function depressButton(button) {
	var w, dw, x, y;

	//We have to hide the module (as opposed to just pulling the menu down)
	//because all windowed displays (like VRML browsers, applets) are
	//always in front of windowless items like HTML menus.
	setModuleVisible(false);
	// Change the button's style class to make it look like it's depressed.
	button.className = "menuButtonActive";

	// For IE, set an explicit width on the first menu item. This will
	// cause link hovers to work on all the menu's items even when the
	// cursor is not over the link's text.
	if (browser.isIE && !button.menu.firstChild.style.width) {
		w = button.menu.firstChild.offsetWidth;
		button.menu.firstChild.style.width = w + "px";
		dw = button.menu.firstChild.offsetWidth - w;
		w -= dw;
		button.menu.firstChild.style.width = w + "px";
	}

	// Position the associated drop down menu under the button and
	// show it. Note that the position must be adjusted according to
	// browser, styling and positioning.
	x = getPageOffsetLeft(button);
	y = getPageOffsetTop(button) + button.offsetHeight;
	if (browser.isIE || (browser.isNS && browser.version >= 6.1)) y += 2;
	else if (browser.isNS && browser.version < 6.1) {
		x--;
		y--;
	}

	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";

	//Set button state and let the world know which button is active.
	button.isDepressed = true;
	activeButton = button;
}

function resetButton(button) {
	// Restore the button's style class.
	button.className = "menuButton";
	// Hide the button's menu.
	if (button.menu) {
		button.menu.style.visibility = "hidden";
		setModuleVisible(true);
	}
	// Set button state and clear active menu global.
	button.isDepressed = false;
	activeButton = null;
}

//Returns the x-coordinate of an element relative to the page
function getPageOffsetLeft(el) {
	return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}

//Returns the y-coordinate of an element relative to the page
function getPageOffsetTop(el) {
	return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}

//Returns a path string placing one depth levels up in the directory tree
function dirPrefix(depth) {
	ret='';
	if(depth>0) while(depth--) ret+='../';
	return ret;
}

