var browser = new Browser();

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------
var activeButton = null;

// Capture mouse clicks on the page so any active button can be
// deactivated.
//if (!browser.isMac)
//{
	if (browser.isIE)
		document.onmouseover = page_mousedown;
	else
		document.addEventListener("mouseover", page_mousedown, true);
//}

// for ie change the td background since the highlighting doesn't work
function td_mouseover(td, new_color, sb_text)
{
	if (!browser.isMac)
	{
		if(document.getElementById||(document.all && !(document.getElementById)))
		{
			td.style.backgroundColor = new_color;
			td.style.cursor="hand";
			self.status = sb_text;
		}
	}
}
function td_mouseout(td, new_color)
{
	if (!browser.isMac)
	{
		if(document.getElementById||(document.all && !(document.getElementById)))
		{
			td.style.backgroundColor=new_color;
			td.style.cursor="normal";
			self.status = 'Done';
		}
	}
}

function page_mousedown(event)
{
	try
	{
		// if there is no active button, exit.
		if (activeButton == null)
			return;
		// find the element that was clicked on.
		element = getEventElement(event);

		var el = getAnyContainerWith(element, "menubar");
		//if (el != null)
			//alert("el: " + el );
		// if any elment on menubar then exit
		if ( getAnyContainerWith(element, "mbr_button"))
			return;
		// if any element on menu then exit
		if ( getAnyContainerWith(element, "menu"))
			return;

		//alert("activebutton: " + activeButton);
		//alert("element: " + element);
		// otherwise clear and null the active item
		removeClassName(activeButton, "mbr_activebutton");
		if(activeButton.menu)
			close_menu(activeButton.menu);
		activeButton = null;
	}
	catch(e) { alert("page_mouseover exception: " + e); }
}

function show_menu(element, menu_id)
{
	try
	{
		//alert("showing menu");
		var button = getAnyContainerWith(element, "mbr_button");

		//alert("checking for menu");
		// Associate the named menu to this button if not already done.
		// Additionally, initialize menu display.
		if (button.menu == null) 
		{
			button.menu = document.getElementById(menu_id);
			if (button.menu.isInitialized == null)
				init_menu(button.menu);
		}

		//alert("checking for different active button");
		//first make sure that we are not messing with the same button
		if (button != activeButton)
		{
			// reset the currently active button, if any.
			if (activeButton)
			{
				// restore the button's style class.
				//alert("removing class name for button");
				removeClassName(activeButton, "mbr_activebutton");
				//alert("checking for activeButton.menu");
				if (activeButton.menu)
					close_menu(activeButton.menu);
				//alert("closed");
				activeButton = null;
			}
			// activate this button
			if (button)
			{
				// Update the button's style class to make it look active
				button.className += " mbr_activebutton";

				if (button.menu)
				{
					var x, y;

					// Position the drop down menu under the button and show it
					// if the menubar is a left menubar then add the width of the button
					if ( getAnyContainerWith(button, "menubar_left") != null)
					{
						x = getPageOffsetLeft(button) + button.offsetWidth - 5;
						y = getPageOffsetTop(button);
					}
					else
					{
						x = getPageOffsetLeft(button);
						y = getPageOffsetTop(button) + button.offsetHeight;
					}

					// For IE, adjust position.

					if (browser.isIE && !browser.isMac) 
					{
						x += button.offsetParent.clientLeft;
						y += button.offsetParent.clientTop;
					}

					// ---------------- CODE ADDITION: PLACEMENT -----------------
					// Adjust position to fit in view.
					var maxX, maxY;

					//alert("original x: " + x);
					//alert("original y: " + y);
					if( typeof( window.innerWidth ) == 'number' ) 
					{
						//Non-IE
						maxX = window.innerWidth;
						maxY = window.innerHeight;

					}
					else if ( document.documentElement && 
						( document.documentElement.clientWidth || 
						document.documentElement.clientHeight ) ) 
					{
						//IE 6+ in 'standards compliant mode'
						maxX = document.documentElement.clientWidth;
						maxY = document.documentElement.clientHeight;
					}
					else if( document.body && ( document.body.clientWidth 
						|| document.body.clientHeight ) ) 
					{
						//IE 4 compatible
						maxX = document.body.clientWidth;
						maxY = document.body.clientHeight;
					}

	// adjust for mac on ie
	if (browser.isMac && browser.isIE)
	{
		//x -= 0;
		//y -= 0;
		x -= 12;
		y -= 3;
		//maxX -= 3;
		//maxY -= 3;
	}
					//alert('button.menu.menuwidth: ' + button.menu.menuwidth);
					maxX -= button.menu.menuwidth;
					//alert('maxx: ' + maxX);
					//alert('button.menu.menuheight: ' + button.menu.menuheight);
					maxY -= button.menu.menuheight;
					//alert('maxy: ' + maxY);

					if (x > maxX)
						x = Math.max(0, Math.min(x, maxX));
					y = Math.max(0, Math.min(y, maxY));
					//---------------- CODE ADDITION: PLACEMENT -----------------

					button.menu.style.left = x + "px";
					button.menu.style.top  = y + "px";
					button.menu.style.visibility = "visible";
				}
				activeButton = button;
			}
		}
	
		return false;
	}
	catch(e) { alert("show_menu exception: " + e); return false; }
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------
function menuMouseover(event) 
{
	try
	{
		var element = getEventElement(event);
		var menu = getContainerWith(element, "DIV", "menu");;
		
		// Close any active sub menu.
		if (menu.activeItem)
			removeClassName(menu.activeItem, "mnu_activeitem");
		if (menu.activeItem && menu.activeItem.subMenu)
			close_menu(menu.activeItem.subMenu);
	}
	catch(e) { alert("menuMouseover exception: " + e); return; }
}

function menuItemMouseover(event, menu_id) 
{
	try
	{
  var x, y;
	var element = getEventElement(event);
	var item = getContainerWith(element,  "A", "mnu_item");
	var menu = getContainerWith(item, "DIV", "menu");

  // close any active sub menu 
	//alert("closing any active sub menus");
  if (menu.activeItem && menu.activeItem.subMenu)
		close_menu(menu.activeItem.subMenu);
	// set this item to the new active item
	//alert("setting activeitem");
  menu.activeItem = item;

  // Highlight the item element.
	//alert("highlighting");
  item.className += " mnu_activeitem";

  // Initialize the sub menu, if not already done.
	//alert("initializing submenu");
  if (item.subMenu == null) 
	{
    item.subMenu = document.getElementById(menu_id);
    if (item.subMenu.isInitialized == null)
      init_menu(item.subMenu);
  }

  // Get position for submenu based on the menu item.
  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);
	//alert("starting x: " + x);
	//alert("starting y: " + y);
	//alert("width: " + item.offsetWidth);
  // Adjust position to fit in view.
  var maxX, maxY;

						if( typeof( window.innerWidth ) == 'number' ) 
					{
						//Non-IE
						maxX = window.innerWidth;
						maxY = window.innerHeight;

					}
					else if ( document.documentElement && 
						( document.documentElement.clientWidth || 
						document.documentElement.clientHeight ) ) 
					{
						//IE 6+ in 'standards compliant mode'
						maxX = document.documentElement.clientWidth;
						maxY = document.documentElement.clientHeight;
					}
					else if( document.body && ( document.body.clientWidth 
						|| document.body.clientHeight ) ) 
					{
						//IE 4 compatible
						maxX = document.body.clientWidth;
						maxY = document.body.clientHeight;
					}

	//alert("maxx: " + maxX);
	//alert("maxy: " + maxY);
	//alert("adjusting for widths");
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

	// adjust for mac on ie
	if (browser.isMac && browser.isIE)
	{
		x -= 12;
		y -= 3;
		//maxX -= 3;
		//maxY -= 3;
	}
	//alert("making sure not neg");
  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

	//alert("x: " + x);
	//alert("y: " + y);
	
  // Position and show it.
	//alert("setting left");
  item.subMenu.style.left = x + "px";
	//alert("setting top");
  item.subMenu.style.top  = y + "px";
	//alert("setting visible");
  item.subMenu.style.visibility = "visible";

	//alert("stopping bubble");
  // Stop the event from bubbling.
  if (browser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
	//alert("done");
	}
	catch(e) { alert(e); return; }
}

function close_menu(menu) 
{
  if (menu == null)
    return;

	try
	{
		// Recursively close any active items or sub menus.
		//alert("testing for activeItem");
		if (menu.activeItem)
		{
			//alert("testing for activeItem.subMenu");
			if (menu.activeItem.subMenu)
			{
				//alert("recursively calling");
				close_menu(menu.activeItem.subMenu);
				//menu.activeItem.subMenu.style.visibility = "hidden";
				menu.activeItem.subMenu = null;
			}
			//alert("removing classname");
			removeClassName(menu.activeItem, "mnu_activeitem");
			menu.activeItem = null;
		}	
		menu.style.visibility = "hidden";
	}
	catch(e) { alert("MenuItemMO exception: " + e); return; }
}

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------
function init_menu(menu) 
{
  var itemList, spanList;
  var textEl, arrowEl;
  var max_width;
  var w, dw;
  var i, j;

	try
	{
	//	if (browser.isMac)
	//	{
	//		alert("in mac code");
	//		menu.isInitialized = true;
	//		return;
	//	}

		// For IE, replace arrow characters.
		if (browser.isIE) 
		{
			menu.style.lineHeight = "2.5ex";
			spanList = menu.getElementsByTagName("SPAN");
			for (i = 0; i < spanList.length; i++)
				if ( hasClassName(spanList[i], "mnu_arrow")) 
				{
					spanList[i].style.fontFamily = "Webdings";
					spanList[i].firstChild.nodeValue = "4";
				}
		}

		//alert("menuoffset is: " + menu.offsetWidth);
		max_width = 0;
		is_px = false;
		max_px = 0;
		max_em = 0;

		// Find the width of a menu item.
		//alert("about to find width");
		itemList = menu.getElementsByTagName("A");
		if (itemList.length > 0)
		{
			if (browser.isMac && browser.isIE)
			{
				//we can only find the proper size by querying the lowest elements
				// For items with arrows, add padding to item text to make the
				// arrows flush right.
				//alert("there are " + itemList.length + " anchors to go through");
				for (i = 0; i < itemList.length; i++) 
				{
					spanList = itemList[i].getElementsByTagName("SPAN");
					if (is_px || spanList.length > 0)
					{
						textEl  = null;
						arrowEl = null;
						for (j = 0; j < spanList.length; j++) 
						{
							if ( hasClassName(spanList[j], "mnu_label"))
								textEl = spanList[j];
							if ( hasClassName(spanList[j], "mnu_arrow"))
								arrowEl = spanList[j];
						}
						if (textEl != null && arrowEl != null)
						{
							//alert("el_text: " + textEl.offsetWidth);
							//alert("arrow_el: " + arrowEl.offsetWidth);
							if (textEl.offsetWidth + arrowEl.offsetWidth > max_width)
								max_px = textEl.offsetWidth + arrowEl.offsetWidth;
							is_px = true;
						}
					}
					else
					{
						if (itemList[i].innerHTML.length > max_width)
							max_em = itemList[i].innerHTML.length;
					}
				}
				if (is_px)
					max_width = max_px;
				else
					max_width = max_em;
				//alert("max_with: " + max_width);
				//alert("is_px: " + is_px);

				if (is_px)
					menu.style.width = max_width + "px";
				else
					menu.style.width = max_width + "em";
			}
			else
			{
				max_width = itemList[0].offsetWidth;
				//alert("link width is: " + max_width);
				//alert("link size is: " + itemList[0].text.length);
				//alert("scrollwidth is: " + itemList[0].scrollWidth);
				//alert("innerHTML lenth is: " + itemList[0].innerHTML.length);
				//alert("attempt at style: " + itemList[0].style.width);
				//alert("max_width: " + max_width);
			}
		}
		else
			return;

		// For items with arrows, add padding to item text to make the
		// arrows flush right.
		for (i = 0; i < itemList.length; i++) 
		{
			spanList = itemList[i].getElementsByTagName("SPAN");
			textEl  = null;
			arrowEl = null;
			for (j = 0; j < spanList.length; j++) 
			{
				if ( hasClassName(spanList[j], "mnu_label"))
					textEl = spanList[j];
				if ( hasClassName(spanList[j], "mnu_arrow"))
					arrowEl = spanList[j];
			}
			
			if (textEl != null && arrowEl != null)
			{
			//alert("el_text: " + textEl.offsetWidth);
			//alert("arrow_el: " + arrowEl.offsetWidth);
			 if (browser.isMac && browser.isIE)
			 {
				 if (is_px)
				 {
					 textEl.style.paddingRight = (max_width - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
					 itemList[i].style.width = max_width + 12 + "px";
				 }
				 else
				 {
					 textEl.style.paddingRight = (max_width - (textEl.innerHTML.length + arrowEl.innerHTML.length)) + "em";
					 itemList[i].style.width = max_width + "em";
				 }
			 }
			 else
				 textEl.style.paddingRight = (max_width - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
			 //alert("textel.style.paddingright: " + textEl.style.paddingRight);
			}
		}
	
		// Fix IE hover problem by setting an explicit width on first item of
		// the menu.
		if (browser.isIE && !browser.isMac) 
		{

				w = itemList[0].offsetWidth;
				itemList[0].style.width = w + "px";
				dw = itemList[0].offsetWidth - w;
				w -= dw;
				itemList[0].style.width = w + "px";
		//	}
		}

		// NOTE: for some reason if you use the offsetWidth repeatedly
		//			it will decrease by 10px each time in netscape/mozilla. 
		menu.menuwidth = menu.offsetWidth;
		menu.menuheight = menu.offsetHeight;

		// Mark menu as initialized.
		menu.isInitialized = true;
	}
	catch(e) { alert("init exception: " + e); return; }
}
//----------------------------------------------------------------------------
// utility fn's
//----------------------------------------------------------------------------
function getEventElement(event) 
{
	var element = null;

	try
	{
		// Find the target event element.
		if (browser.isIE)
			element = window.event.srcElement;
		else
			element = (event.target.tagName ? event.target : event.target.parentNode);
			//element = event.currentTarget;

		return element;
	}
	catch(e) { alert("getEventElemnt exception: " + e); return element; }
}