//*****************************************************************************
// Do not remove this notice.
// 
// Copyright 2000-2004 by Mike Hall.
// See http://www.brainjar.com for terms of use.
// Modified: Jeremy Schultz, removed a lot of unneeded functionality
//*****************************************************************************

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------
function Browser() {
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}
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.isIE)
  document.onmousedown = pageMousedown;
else
  document.addEventListener("mousedown", pageMousedown, true);

function pageMousedown(event) {
  var el;
  // If there is no active button, exit.
  if (activeButton == null)
    return;

  // Find the element that was clicked on.
  if (browser.isIE)
    el = window.event.srcElement;
  else
    el = (event.target.tagName ? event.target : event.target.parentNode);

  // If the active button was clicked on, exit.
  if (el == activeButton)
    return;

  resetButton(activeButton);
  activeButton = null;
}

function buttonClick(event, elementID, xOffSet, yOffSet) {
  var button;

  // Get the target button element.
  if (browser.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // Blur focus from the link to remove that annoying outline.
  button.blur();

  // Associate the named menu to this button if not already done.
  // Additionally, initialize menu display.
  if (button.menu == null) 
    button.menu = document.getElementById(elementID);
    
  // Reset the currently active button, if any.
  if (activeButton != null)
    resetButton(activeButton);

  // Activate this button, unless it was the currently active one.
  if (button != activeButton) {
    depressButton(button, xOffSet, yOffSet);
    activeButton = button;
  } else
    activeButton = null;

  return false;
}

function depressButton(button, xOffSet, yOffSet) {
  var x, y;
  if (xOffSet == undefined)	
   	xOffSet = 0;
   		
  if (yOffSet == undefined)	
    yOffSet = 0;

  // Position the associated drop down menu under the button and
  // show it.
  x = getPageOffsetLeft(button) + xOffSet;
  y = getPageOffsetTop(button) + yOffSet;

  // For IE, adjust position.
  if (browser.isIE) {
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;
  }

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

function resetButton(button) {
  // Hide the button's menu, first closing any sub menus.
  if (button.menu != null) 
    button.menu.style.visibility = "hidden";
}

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------
function getPageOffsetLeft(el) {
  var x;

  // Return the x coordinate of an element relative to the page.
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {
  var y;

  // Return the x coordinate of an element relative to the page.
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

