var timeOn = null;
var DEBUG = false;
var activeSubMenus = new Array(10);
var imageDirectory = '';

/******************************************************************************
 * FUNCTION: debugAlert
 * PURPOSE: This function will print out the alert message if the page is in
 *	debug mode.
 * ARGS:
 *	msg - the message to print out.
 *****************************************************************************/
function debugAlert(msg)
{
	if (DEBUG)
	{
		alert(msg);
	}
}

/******************************************************************************
 * FUNCTION: getObject
 * PURPOSE: This function will find the requested object in the current document.
 * ARGS:
 *	objectId - the ID of the object we are looking for
 *****************************************************************************/
function getObject(objectId)
{
	debugAlert("getObject");

	// IF we can use the getElementById function and the object exists
	//  return the object
	// ELSE IF we can use the all array and the object exists
	//  return the object
	// ELSE IF we can use the layers array and the object exists
	//  return the object
	// ELSE
	//  return false
	// ENDIF
	if(document.getElementById && document.getElementById(objectId))
	{
		return document.getElementById(objectId);
	}
	else if (document.all && document.all(objectId))
	{
		return document.all(objectId);
	}
	else if (document.layers && document.layers[objectId])
	{
		return document.layers[objectId];
	}
	else
	{
		return false;
	}
}

/******************************************************************************
 * FUNCTION: getStyleObject
 * PURPOSE: This function will find the requested style object in the given
 *		document.
 * ARGS:
 *	objectId - the ID of the object we are looking for
 *****************************************************************************/
function getStyleObject(objectId, doc)
{
	var myObj = getObject(objectId);
	
	debugAlert("getStyleObject");

	// IF we were able to get the object
	//  return the style object
	// ELSE
	//  return false
	// ENDIF
	if(myObj)
	{
		return myObj.style;
	}
	else
	{
		return false;
	}
}

/******************************************************************************
 * FUNCTION: menuOver
 * PURPOSE: This function will clear out the hide menu timeout.
 * ARGS: none
 *****************************************************************************/
function menuOver()
{
	debugAlert("menuOver");

	// clear the hide timeout
	clearTimeout(timeOn);
}

/******************************************************************************
 * FUNCTION: menuOut
 * PURPOSE: This function will set a 1 second timeout after which all menus
 *	will be hidden.
 * ARGS: none
 *****************************************************************************/
function menuOut()
{
	debugAlert("menuOut");

	// start the hide timeout
	timeOn = setTimeout("hideAllMenus()", 1000);
}

/******************************************************************************
 * FUNCTION: hideAllMenus
 * PURPOSE: This function will hide all menus on this page.
 * ARGS: none
 *****************************************************************************/
function hideAllMenus()
{
	var cnt=0;
	
	debugAlert("hideAllMenus");

	// LOOP through all of the passed menu items
	// ENDLOOP
	for(cnt=0; cnt<activeSubMenus.length; cnt++)
	{
		// set the class of the main menu item
		changeClass('menuLink' + activeSubMenus[cnt], 'MenuLabelLink');
		
		// change back the menus bullets to unselected
		changeImage('menuBullet' + activeSubMenus[cnt], imageDirectory + 'header_off.gif');

		// hide all of the menu's sub-items
		changeVisibility('subMenu' + activeSubMenus[cnt], 'hidden');
	}
}

/******************************************************************************
 * FUNCTION: showMenu
 * PURPOSE: This function will show the menu for the given menu header
 * ARGS:
 *	menuNum - the number of the menu to show
 *****************************************************************************/
function showMenu(menuNum)
{
	var borderSize = 1;
	var menuObject = getObject('subMenu' + menuNum);
	
	debugAlert("showMenu");

	// first want to hide all of the menus
	hideAllMenus();

	// IF we do not have a menu to show
	//  return true
	// ENDIF
	if(!menuObject)
	{
		return true;
	}

	debugAlert("showMenu[" + menuObject.id + "]");

	changeClass('menuLink' + menuNum, 'MenuLabelLinkOn');

	changeImage('menuBullet' + menuNum, imageDirectory + 'header_on.gif');

	labelObj = 'labelCell' + menuNum;

	// get the x,y coordinates for where we need to place the element
	x = getElementLeft(labelObj);
	y = getElementTop(labelObj) + getElementHeight(labelObj);

	// IF we are aligning on the center
	//  split the extra space so the sub menu is in the middle of the main menu item
	// ENDIF
	if (menuObject.align == 'center')
	{
		x = x + ((getElementWidth(labelObj) - getElementWidth('subMenu' + menuNum))/2);
	}

	// IF we are aligning to the right
	//  adjust the left starting point so the sub menu ends at the same vertical point as the menu item
	// ENDIF
	if (menuObject.align == 'right')
	{
		x = x + ((getElementWidth(labelObj) - getElementWidth('subMenu' + menuNum)));
	}

	// move the sub menu to the appropriate spot on the page
	moveXY('subMenu' + menuNum, x, y);

	return changeVisibility('subMenu' + menuNum, 'visible');
}

/******************************************************************************
 * FUNCTION: mainMenuItemMouseOver
 * PURPOSE: This function will change the style class and appropriate image
 *          for the main menu area that has been moused over
 * ARGS:
 *	classObjectName - the object whose class we need to change
 *	imageObjectName - the object whose image we need to change
 *	subMenuNum - the sub-menu number to show
 *****************************************************************************/
function mainMenuItemMouseOver(classObjectName, imageObjectName, subMenuNum)
{
	debugAlert("mainMenuItemMouseOver");
	
	// stop the hide menus timer
	menuOver();

	// hide all of the sub-menus
	hideAllMenus();

	// change the class of the object to the mouse in class
	changeClass(classObjectName, 'MenuLabelLinkOn');

	// IF the main menu item has a sub-menu
	//  show the mouse over image that indicates a sub-menu
	//  show the sub-menu
	// ELSE
	//  show the mouse over image that doesn't indicate a sub-menu
	// ENDIF
	if (subMenuNum > 0)
	{
		changeImage(imageObjectName, imageDirectory + 'header_on.gif');
		showMenu(subMenuNum);
	}
	else
	{
		changeImage(imageObjectName, imageDirectory + 'menu_on.gif');
	}
}

/******************************************************************************
 * FUNCTION: mainMenuItemMouseOut
 * PURPOSE: This function will change the style class and appropriate image
 *          for the main menu area that has been moused out of
 * ARGS:
 *	classObjectName - the object whose class we need to change
 *	imageObjectName - the object whose image we need to change
 *	subMenuNum - the sub-menu number to show
 *****************************************************************************/
function mainMenuItemMouseOut(classObjectName, imageObjectName, subMenuNum)
{
	debugAlert("mainMenuItemMouseOut");
	
	// change the class of the object to the mouse in class
	changeClass(classObjectName, 'MenuLabelLink');

	// IF the main menu item has a sub-menu
	//  show the mouse out image that indicates a sub-menu
	//  start the timer that will hide all menus
	// ELSE
	//  show the mouse out image that doesn't indicate a sub-menu
	// ENDIF
	if (subMenuNum > 0)
	{
		changeImage(imageObjectName, imageDirectory + 'header_off.gif');
		menuOut();

	}
	else
	{
		changeImage(imageObjectName, imageDirectory + 'menu_off.gif');
	}
}

/******************************************************************************
 * FUNCTION: subMenuItemMouseOver
 * PURPOSE: This function will change the style class and appropriate image
 *          for the sub-menu area that has been moused over
 * ARGS:
 *	classObjectName - the object whose class we need to change
 *	imageObjectName - the object whose image we need to change
 *****************************************************************************/
function subMenuItemMouseOver(classObjectName, imageObjectName)
{
	debugAlert("subMenuItemMouseOver");
	
	// stop the hide menus timer
	menuOver();

	// change the class of the object to the mouse in class
	changeClass(classObjectName, 'MenuItemLinkOn');

	// show the mouse over image
	changeImage(imageObjectName, imageDirectory + 'menu_on.gif');
}

/******************************************************************************
 * FUNCTION: subMenuItemMouseOut
 * PURPOSE: This function will change the style class and appropriate image
 *          for the sub-menu area that has been moused out of
 * ARGS:
 *	classObjectName - the object whose class we need to change
 *	imageObjectName - the object whose image we need to change
 *****************************************************************************/
function subMenuItemMouseOut(classObjectName, imageObjectName)
{
	debugAlert("subMenuItemMouseOut");
	
	// start the timer that will hide all menus
	menuOut();

	// change the class of the object to the mouse out of class
	changeClass(classObjectName, 'MenuItemLink');

	// show the mouse out of image
	changeImage(imageObjectName, imageDirectory + 'menu_off.gif');
}

/******************************************************************************
 * FUNCTION: changeClass
 * PURPOSE: This function will change the style class of the given object
 * ARGS:
 *	elemName - the element name whose class we need to change
 *	newClass - the class that we are switching to
 *****************************************************************************/
function changeClass(elemName, newClass)
{
	var elem;
	
	debugAlert("changeClass");

	// IF we can get the element by ID
	//  call the get element by ID method
	// ELSE IF we can get the element from the all array
	//  use the all array to get the element
	// ENDIF
	if(document.getElementById)
	{
		elem = document.getElementById(elemName);
	}
	else if (document.all)
	{
		elem = document.all[elemName];
	}

	// IF the element was found
	//  update the class
	// ENDIF
	if (elem)
	{
		elem.className = newClass;
	}
}

/******************************************************************************
 * FUNCTION: changeImage
 * PURPOSE: This function will change the source of the given image
 * ARGS:
 *	target - the image name whose source we need to change
 *	source - the new source of the image
 *****************************************************************************/
function changeImage(target, source)
{
	var image;
	
	debugAlert("changeImage");

	// get the image element
	image = eval('document.images.' + target);

	// IF we foung the image element
	//  update the source of the image
	// ENDIF
	if (image)
	{
		image.src = source;
	}
}

/******************************************************************************
 * FUNCTION: getElementHeight
 * PURPOSE: This function will get the height of the given element
 * ARGS:
 *	elem - the element whose height we are getting
 *****************************************************************************/
function getElementHeight(elem)
{
	var elem;
	var xPos;
	
	debugAlert("getElementHeight");

	// IF we can get the element from the getElementById function
	//  use that function to get the element
	// ELSE IF we can get the element from the all array
	//  use the all array to get the element
	// ENDIF
	if(document.getElementById)
	{
		elem = document.getElementById(elem);
	}
	else if (document.all)
	{
		elem = document.all[elem];
	}

	// IF we could find the element
	//  set the height
	// ENDIF
	if (elem)
	{
		xPos = elem.offsetHeight;
	}
	
	return xPos;
}

/******************************************************************************
 * FUNCTION: getElementHeight
 * PURPOSE: This function will get the height of the given element
 * ARGS:
 *	elem - the element whose height we are getting
 *****************************************************************************/
function getElementWidth(elem)
{
	var elem;
	var xPos;

	debugAlert("getElementWidth");

	// IF we can get the element from the getElementById function
	//  use that function to get the element
	// ELSE IF we can get the element from the all array
	//  use the all array to get the element
	// ENDIF
	if(document.getElementById)
	{
		elem = document.getElementById(elem);
	}
	else if (document.all)
	{
		elem = document.all[elem];
	}

	// IF we could find the element
	//  set the width
	// ENDIF
	if (elem)
	{
		xPos = elem.offsetWidth;
	}

	return xPos;
}

/******************************************************************************
 * FUNCTION: getElementLeft
 * PURPOSE: This function will get the x position of the left edge of the given
 *		element
 * ARGS:
 *	elem - the element whose lowest x-coordinate we are getting
 *****************************************************************************/
function getElementLeft(elem)
{
	var elem;
	var xPos;
	
	debugAlert("getElementLeft");

	// IF we can get the element from the getElementById function
	//  use that function to get the element
	// ELSE IF we can get the element from the all array
	//  use the all array to get the element
	// ENDIF
	if(document.getElementById)
	{
		elem = document.getElementById(elem);
	}
	else if (document.all)
	{
		elem = document.all[elem];
	}

	// IF we could find the element
	// ENDIF
	if (elem)
	{
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;

		// LOOP through all of the offset parents
		//  adjust our left edge to the left edge of the offset parent
		// ENDLOOP
		while (tempEl != null)
		{
  			xPos += tempEl.offsetLeft;
  			tempEl = tempEl.offsetParent;
		}
	}

	return xPos;
}

/******************************************************************************
 * FUNCTION: getElementTop
 * PURPOSE: This function will get the y postition of the top edge of the given
 *		element
 * ARGS:
 *	elem - the element whose largest y-coordinate we are getting
 *****************************************************************************/
function getElementTop(elem)
{
	var elem;
	var yPos;
	
	debugAlert("getElementTop");

	// IF we can get the element from the getElementById function
	//  use that function to get the element
	// ELSE IF we can get the element from the all array
	//  use the all array to get the element
	// ENDIF
	if(document.getElementById)
	{	
		elem = document.getElementById(elem);
	}
	else if (document.all)
	{
		elem = document.all[elem];
	}

	// IF we could find the element
	// ENDIF
	if (elem)
	{
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;

		// LOOP through all of the offset parents
		//  adjust our top to the top level of the offset parent
		// ENDLOOP
		while (tempEl != null)
		{
  			yPos += tempEl.offsetTop;
  			tempEl = tempEl.offsetParent;
		}
	}

	return yPos;
}

/******************************************************************************
 * FUNCTION: moveXY
 * PURPOSE: This function will set the X,Y position of the given object.
 * ARGS:
 *	myObject - the object we are changing coordinates for
 *	x - the new x-coordinate
 *	y - the new y-coordinate
 *****************************************************************************/
function moveXY(myObject, x, y)
{
	var obj = getStyleObject(myObject);

	debugAlert("moveXY");

	// IF we able to get the style object
	//  set the top left starting point
	// ENDIF
	if (obj)
	{
		obj.top = y + 'px';
		obj.left = x + 'px';
	}
}

/******************************************************************************
 * FUNCTION: changeVisibility
 * PURPOSE: This function will change the visiblity of the given object.
 * ARGS:
 *	objectId - the id of the object we are changing
 *	newVisibility - the new visibility value for the object
 *****************************************************************************/
function changeVisibility(objectId, newVisibility)
{
	var styleObject = getStyleObject(objectId, document);
	
	debugAlert("changeVisibility");

	// IF we able to get the style object
	//  set the top left starting point
	//  return true
	// ELSE
	//  return false
	// ENDIF
	if(styleObject)
	{
		styleObject.visibility = newVisibility;
		return true;
	}
	else
	{
		return false;
	}
}
