  var oMenuBar = null;
  var oCurrentPageMenuItem = null;
  var idMenuTimeout = null;
/* Adding Top Menu items to the site by copying the <li> tags.
  Requirements: 
    1) All image controls need to have an id that starts in "topnav"
    2) All menu items need to have two images in the following formats.
      a) on status: filename-on.gif
      b) off status: filename.gif
*/
  

/* swap images (from on to off and the other way around as well)
obj = id of control
offImage = string of image to set the control for off image
onImage = string of image to set the control for on image
*/
  var imgSwap = function (obj, offImage, onImage) {
    var imgEl = YAHOO.util.Dom.get(obj);

    if (imgEl.src.indexOf("-on") !== -1) {
      // the current image is the "on" image, replace with "off" image
      var oldSrc = imgEl.src;
      if (offImage == null || offImage == 'undefined')
        var newSrc = oldSrc.replace("-on", "");
      else
        var newSrc = offImage;
      imgEl.src = newSrc;
    }
    else{
      // the current image is the "off" image, replace with "on" image
      var oldSrc = imgEl.src;
      if (onImage == null || onImage == 'undefined')
        var newSrc = oldSrc.replace(".gif", "-on.gif");
      else
        var newSrc = onImage;
      imgEl.src = newSrc;
    }
  };

/* turn the img control to an on status
obj = id of control
onImage = string of image to set the control for on image
*/  
  var imgOn = function (obj, onImage) {
    var imgEl = YAHOO.util.Dom.get(obj);

    if (imgEl.src.indexOf("-on") === -1) {
      // the current image is the "off" image, replace with "on" image
      var oldSrc = imgEl.src;
      if (onImage == null || onImage == 'undefined')
        var newSrc = oldSrc.replace(".gif", "-on.gif");
      else
        var newSrc = onImage;
      imgEl.src = newSrc;
    }
  };
  
/* swap images (from on to off and the other way around as well)
obj = id of control
offImage = string of image to set the control for off image
onImage = string of image to set the control for on image
*/
  var imgOff = function (obj, offImage) {
    var imgEl = YAHOO.util.Dom.get(obj);

    if (imgEl.src.indexOf("-on") !== -1) {
      // the current image is the "on" image, replace with "off" image
      var oldSrc = imgEl.src;
      if (offImage == null || offImage == 'undefined')
        var newSrc = oldSrc.replace("-on", "");
      else
        var newSrc = offImage;
      imgEl.src = newSrc;
    }
  };
  
/* Controls if the user moves off the main menu image without going to the submenu */
  var imgSetTimeoutOff = function (obj, offImage){
    clearTimeout(idMenuTimeout);
    idMenuTimeout = setTimeout ("imgOff('"+obj+"', '"+offImage+"')", 300);
  };
  
/* turn all of the top menus to the off status */
  var allTopMenuItemsOff = function (){
    clearTimeout(idMenuTimeout);
    if (oMenuBar != null){
      var aMenuBarObj = oMenuBar.getItems();
      for(var i=0; i < aMenuBarObj.length; i++){
        if (aMenuBarObj[i]._oAnchor != null){
          for(var j=0; j < aMenuBarObj[i]._oAnchor.childNodes.length; j++){
            if(aMenuBarObj[i]._oAnchor.childNodes[j].id != null && oCurrentPageMenuItem != aMenuBarObj[i]._oAnchor.childNodes[j]){
              if(aMenuBarObj[i]._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
                if(aMenuBarObj[i]._oAnchor.childNodes[j].src.indexOf("-on") !== -1){
                  imgOff(aMenuBarObj[i]._oAnchor.childNodes[j].id);
                }
              }
            }
          }
        }
      }
    }
  };
  
  var topMenuItemOn = function(obj, onImage){
    allTopMenuItemsOff();
    imgOn(obj, onImage);
  }
  
  var checkMenuBarItemActive = function(oMenuItem){
    var oMenuBarImgItem = null
    for(var j=0; j < oMenuItem.activeItem.parent.parent._oAnchor.childNodes.length; j++){
      if(oMenuItem.activeItem.parent.parent._oAnchor.childNodes[j].id != null){
        if(oMenuItem.activeItem.parent.parent._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
          oMenuBarImgItem = oMenuItem.activeItem.parent.parent._oAnchor.childNodes[j];
          break;
        }
      }
    }
    if (oMenuBarImgItem.src.indexOf("-on") !== -1) {
      return true;
    }
    else{
      return false;
    }
  };
  
  var initMenuEvents = function(aMenuObj){
    for(var i=0; i < aMenuObj.length; i++){
      aMenuObj[i].mouseOverEvent.subscribe(onMenuItemMouseOver,  aMenuObj[i], true);
      aMenuObj[i].mouseOutEvent.subscribe(onMenuItemMouseOut,  aMenuObj[i], true);
      aMenuObj[i].hideEvent.subscribe(onMenuHide,  aMenuObj[i], true);
    }
  };
  
  var getCurrentPageMenuItem = function(aMenuBarObj){
    for(var i=0; i < aMenuBarObj.length; i++){
      if (aMenuBarObj[i]._oAnchor != null){
        for(var j=0; j < aMenuBarObj[i]._oAnchor.childNodes.length; j++){
          if(aMenuBarObj[i]._oAnchor.childNodes[j].id != null){
            if(aMenuBarObj[i]._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
              if(aMenuBarObj[i]._oAnchor.childNodes[j].src.indexOf("-on") !== -1){
                oCurrentPageMenuItem = aMenuBarObj[i]._oAnchor.childNodes[j];
              }
            }
          }
        }
      }
    }
  };
  
  var onMenuItemMouseOver = function(p_sType, p_aArguments, p_oMenuItem) {
    clearTimeout(idMenuTimeout);
    var oChildNode = null
    for(var j=0; j < this.activeItem._oAnchor.childNodes.length; j++){
      if(this.activeItem._oAnchor.childNodes[j].id != null){
        if(this.activeItem._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
          oChildNode = this.activeItem._oAnchor.childNodes[j];
        }
      }
    }
    imgSwap(oChildNode.id);
  };
  
  var onMenuItemMouseOut = function(p_sType, p_aArguments, p_oMenuItem) {
    var oChildNode = null
    for(var j=0; j < this.activeItem._oAnchor.childNodes.length; j++){
      if(this.activeItem._oAnchor.childNodes[j].id != null){
        if(this.activeItem._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
          oChildNode = this.activeItem._oAnchor.childNodes[j];
        }
      }
    }
    imgSwap(oChildNode.id);
  };
  
  var onMenuHide = function (p_sType, p_aArguments, p_oMenuItem) {
    var oChildNode = null
    for(var j=0; j < this.activeItem.parent.parent._oAnchor.childNodes.length; j++){
      if(this.activeItem.parent.parent._oAnchor.childNodes[j].id != null){
        if(this.activeItem.parent.parent._oAnchor.childNodes[j].id.indexOf("topnav") !== -1){
          oChildNode = this.activeItem.parent.parent._oAnchor.childNodes[j];
        }
      }
    }
    if(oCurrentPageMenuItem != null){
      if(oCurrentPageMenuItem.id != oChildNode.id){
        imgOff(oChildNode.id); //get active item menubaritem img tag. 
      }
    }
    else{
      imgOff(oChildNode.id); //get active item menubaritem img tag. 
    }
  };
