
// JavaScript methods for use with navigation menus (R3CMHtmlNavMenuView)

function r3navGetAbsoluteLeft(element) {
	var result;
	var curNode;
	result = 0;
	curNode = element;
	while ( curNode != null ) {
		if ( !isNaN(curNode.offsetLeft) ) result += curNode.offsetLeft;
		curNode = curNode.offsetParent;
	}
	return( result );
}

function r3navGetAbsoluteTop(element) {
	var result;
	var curNode;
	result = 0;
	curNode = element;
	while ( curNode != null ) {
		if ( !isNaN(curNode.offsetTop) ) result += curNode.offsetTop;
		curNode = curNode.offsetParent;
	}
	return( result );
}

function R3CMNavMenu_addSubMenu(parentMenuId, hoverId, subMenuId) {
	var menuToAttachTo, newSubMenu;
	var eventBody, curEvent;
	// Determine which menu to attach to
	menuToAttachTo = this;
	if ( parentMenuId != this.menuElement.id ) {
		menuToAttachTo = this.getSubMenu(parentMenuId);
	}
	// Create the sub-menu
	newSubMenu = new R3CMNavMenu(hoverId, subMenuId);
	menuToAttachTo.subMenus[menuToAttachTo.subMenus.length] = newSubMenu;
	newSubMenu.parentMenu = menuToAttachTo;
	// Add it to the main menu's list of sub-menus
	this.getMainMenu().menuList[newSubMenu.menuElement.id] = newSubMenu;
	// Create the mouse events
	eventBody = "('" + this.getMainMenu().menuElement.id + "', '" + subMenuId + "');";
	newSubMenu.hoverElement.onmouseover = new Function("R3CMMainMenus.hoverOver" + eventBody);
	newSubMenu.hoverElement.onmouseout = new Function("R3CMMainMenus.hoverOut" + eventBody);
	newSubMenu.menuElement.onmouseover = new Function("R3CMMainMenus.menuOver" + eventBody);
	newSubMenu.menuElement.onmouseout = new Function("R3CMMainMenus.menuOut" + eventBody);
	return( newSubMenu );
}

function R3CMNavMenu_getMainMenu() {
	if ( this.parentMenu == null ) return( this );
	return( this.parentMenu.getMainMenu() );
}

function R3CMNavMenu_getSubMenu(menuId) {
	return( this.getMainMenu().menuList[menuId] );
}

function R3CMNavMenu_resolveLeft() {
	var result;
	result = r3navGetAbsoluteLeft(this.hoverElement);
	result += this.parentMenu.openOffsetLeft;
	switch ( this.parentMenu.openDirection ) {
		case "above":
		case "below":
			if ( (result + this.menuElement.offsetWidth) >= document.body.offsetWidth ) {
				result = document.body.offsetWidth - this.menuElement.offsetWidth;
			}
			break;
		case "right":
			result += this.hoverElement.offsetWidth;
			break;
		case "left":
			result -= this.hoverElement.offsetWidth;
			break;
	}
	return( result );
}

function R3CMNavMenu_resolveTop() {
	var result;
	result = r3navGetAbsoluteTop(this.hoverElement);
	result += this.parentMenu.openOffsetTop;
	switch ( this.parentMenu.openDirection ) {
		case "right":
		case "left":
			if ( (result + this.menuElement.offsetHeight) >= document.body.offsetHeight ) {
				result = document.body.offsetHeight - this.menuElement.offsetHeight;
			}
			break;
		case "above":
			result -= this.hoverElement.offsetHeight;
			break;
		case "below":
			result += this.hoverElement.offsetHeight;
			break;
	}
	return( result );
}

function R3CMNavMenu_show() {
	var menuLoop;
	var curSiblingMenu;
	for ( menuLoop = 0; menuLoop < this.parentMenu.subMenus.length; menuLoop++ ) {
		curSiblingMenu = this.parentMenu.subMenus[menuLoop];
		if ( curSiblingMenu == this ) {
			curSiblingMenu.menuElement.style.left = this.resolveLeft();
			curSiblingMenu.menuElement.style.top = this.resolveTop();
			curSiblingMenu.menuElement.style.visibility = "visible";
		} else {
			curSiblingMenu.hide();
		}
	}
}

function R3CMNavMenu_hide() {
	var menuLoop;
	var curSubMenu;
	for ( menuLoop = 0; this.subMenus.length; menuLoop++ ) {
		curSubMenu = this.subMenus[menuLoop];
		curSubMenu.hide();
	}
	this.menuElement.style.visibility = "hidden";
}

function R3CMNavMenu_hoverOver() {
	this.onHover = true;
	this.timeUntilHide = 0;
	if ( !this.getIsVisible() ) this.show();
}

function R3CMNavMenu_hoverOut() {
	this.onHover = false;
}

function R3CMNavMenu_menuOver() {
	this.timeUntilHide = 0;
	this.onMenu = true;
}

function R3CMNavMenu_menuOut() {
	this.onMenu = false;
}

function R3CMNavMenu_getIsVisible() {
	return( this.menuElement.style.visibility == "visible" );
}

function R3CMNavMenu_getChildVisible() {
	var childLoop;
	var curSubMenu;
	for ( childLoop = 0; childLoop < this.subMenus.length; childLoop++ ) {
		curSubMenu = this.subMenus[childLoop];
		if ( curSubMenu.getIsVisible() ) return( true );
		if ( curSubMenu.getChildVisible() ) return( true );
	}
	return( false );
}

function R3CMNavMenu_getShouldBeVisible() {
	return( this.isMain || this.onHover || this.onMenu || this.getChildVisible() );
}

function R3CMNavMenu_pollSubMenus(milliseconds) {
	var childLoop;
	var curSubMenu;
	for ( childLoop = 0; childLoop < this.subMenus.length; childLoop++ ) {
		curSubMenu = this.subMenus[childLoop];
		curSubMenu.pollSubMenus(milliseconds);
		if ( curSubMenu.getShouldBeVisible() ) {
			curSubMenu.timeUntilHide = 0;
		} else {
			curSubMenu.timeUntilHide += milliseconds;
			if ( curSubMenu.timeUntilHide >= this.getMainMenu().timeBeforeHide ) {
				curSubMenu.timeUntilHide = 0;
				curSubMenu.hide();
			}
		}
	}
}

function R3CMNavMenu(hoverId, menuId) {
	if ( hoverId == null ) {
		this.isMain = true;
		this.menuList = new Object();
		this.hoverElement = null;
		this.openDirection = "below";
		this.timeBeforeHide = 500;
	} else {
		this.isMain = false;
		this.hoverElement = document.getElementById(hoverId);
		this.openDirection = "right";
		this.onHover = false;
		this.onMenu = false;
		this.timeUntilHide = 0;
	}
	this.parentMenu = null;
	this.subMenus = new Array();
	this.menuElement = document.getElementById(menuId);
	this.openOffsetLeft = 0;
	this.openOffsetTop = 0;
	this.addSubMenu = R3CMNavMenu_addSubMenu;
	this.getMainMenu = R3CMNavMenu_getMainMenu;
	this.getSubMenu = R3CMNavMenu_getSubMenu;
	this.resolveLeft = R3CMNavMenu_resolveLeft;
	this.resolveTop = R3CMNavMenu_resolveTop;
	this.show = R3CMNavMenu_show;
	this.hide = R3CMNavMenu_hide;
	this.hoverOver = R3CMNavMenu_hoverOver;
	this.hoverOut = R3CMNavMenu_hoverOut;
	this.menuOver = R3CMNavMenu_menuOver;
	this.menuOut = R3CMNavMenu_menuOut;
	this.getIsVisible = R3CMNavMenu_getIsVisible;
	this.getChildVisible = R3CMNavMenu_getChildVisible;
	this.getShouldBeVisible = R3CMNavMenu_getShouldBeVisible;
	this.pollSubMenus = R3CMNavMenu_pollSubMenus;
}

function R3CMNavHandler_addMainMenu(menuObject) {
	this.menuList[this.menuList.length] = menuObject;
	this.menuMap[menuObject.menuElement.id] = menuObject;
}

function R3CMNavHandler_getMainMenu(mainMenuId) {
	return( this.menuMap[mainMenuId] );
}

function R3CMNavHandler_getSubMenu(mainMenuId, subMenuId) {
	return( this.menuMap[mainMenuId].getSubMenu(subMenuId) );
}

function R3CMNavHandler_hoverOver(mainMenuId, subMenuId) {
	this.getSubMenu(mainMenuId, subMenuId).hoverOver();
}

function R3CMNavHandler_hoverOut(mainMenuId, subMenuId) {
	this.getSubMenu(mainMenuId, subMenuId).hoverOut();
}

function R3CMNavHandler_menuOver(mainMenuId, subMenuId) {
	this.getSubMenu(mainMenuId, subMenuId).menuOver();
}

function R3CMNavHandler_menuOut(mainMenuId, subMenuId) {
	this.getSubMenu(mainMenuId, subMenuId).menuOut();
}

function R3CMNavHandler() {
	this.menuList = new Array();
	this.menuMap = new Object();
	this.addMainMenu = R3CMNavHandler_addMainMenu;
	this.getMainMenu = R3CMNavHandler_getMainMenu;
	this.getSubMenu = R3CMNavHandler_getSubMenu;
	this.hoverOver = R3CMNavHandler_hoverOver;
	this.hoverOut = R3CMNavHandler_hoverOut;
	this.menuOver = R3CMNavHandler_menuOver;
	this.menuOut = R3CMNavHandler_menuOut;
}

function R3CMNavPoller(milliseconds) {
	var mainMenuLoop;
	var curMainMenu;
	for ( mainMenuLoop = 0; mainMenuLoop < R3CMMainMenus.menuList.length; mainMenuLoop++ ) {
		curMainMenu = R3CMMainMenus.menuList[mainMenuLoop];
		curMainMenu.pollSubMenus(milliseconds);
	}
}

var R3CMMainMenus;
R3CMMainMenus = new R3CMNavHandler();
setInterval("R3CMNavPoller(100);", 100);

