// File: navMenu.js

function NavMenu (containerIdName, itemTagName, idleClassName, hiliteIdName, activeIdName) {
   //---[ Instance variables ]-------------
   var containerID   = null; // ID of element enclosing the menu items
   var itemTag       = null; // Element name (e.g. "a" or "span") of the menu items themselves

   var idleClass     = null; // Name of CSS style for idle menu items
   var highlightID   = null; // Name of CSS style for highlighted menu items
   var activeID      = null; // Name of CSS style for active menu items

   var clickFunc     = null; // User hook for onclick function
   var mouseOverFunc = null; // User hook for onmouseover function
   var mouseOutFunc  = null; // User hook for onmouseout function

   var items         = null; // The menu items
   var activeItem    = null; // The menu item that is currently selected
   var currItem      = null; // The menu item the mouse is over

   var targetFrame   = null; // Target frame for loadIframe(url)

   //---[ Public methods ]-------------
   this.numItems = function () { if (items != null) { return items.length; } else { return 0; } }
   this.setItems = function (newItemsArray) {
      items = newItemsArray;
      initNewMenu();
   }
   this.getItem = function (index) {
      if (items == null ||index >= items.length) return null;
      return items[index];
   }
   this.setClassNames = function (idle, hiName, actName) {
      idleClass   = idle;
      highlightID = hiName;
      activeID    = actName;
   }
   this.setTargetFrame = function (frameName) {
      //targetFrame = document.getElementById(frameName);
      targetFrame = frames[frameName];
   }
   this.setClickFunc = function (clickFuncName) {
      clickFunc = clickFuncName ? clickFuncName : trueFunc;
   }
   this.setMouseOverFunc = function (mouseOverFuncName) {
      mouseOverFunc = mouseOverFuncName ? mouseOverFuncName : trueFunc;
   }
   this.setMouseOutFunc = function (mouseOutFuncName) {
      mouseOutFunc = mouseOutFuncName ? mouseOutFuncName : trueFunc;
   }
   this.highlight = function (item) {
      if (item.id != activeID) {
         this.unhighlight();
         item.id = highlightID;
         currItem = item;
      }
   }
   this.unhighlight = function (item /*not used*/) {
      if (currItem != null && currItem.id != activeID) {
         currItem.id = "";
         currItem = null;
      }
   }
   this.click = function (item) {
      if (item == activeItem) return false;
      else {
         // deactivate old item
         item.blur();
         if (activeItem != null) activeItem.id = "";

         // activate new item
         activeItem = item;
         item.id = activeID;
         activeItem.blur();
      }
      return clickFunc(item); // call user hook
   }
   this.mouseover = function (item) {
      this.highlight(item);
      return mouseOverFunc(item); // call user hook
   }
   this.mouseout = function (item) {
      this.unhighlight(item);
      return mouseOutFunc(item); // call user hook
   }
   this.loadIframe = function (url) {
      if (targetFrame == null) return;
      try {
         //targetFrame.src = url;
         targetFrame.location.href = url;
      } catch (e) {
         //alert("Couldn't find target iframe: " + id);
      }
   }
   this.syncTab = function () {
      if (!targetFrame || targetFrame == null) return;
      //var currHref = targetFrame.src;
      var currHref = targetFrame.location.href;
      if (currHref == null) return;
      if (items != null && items.length != null) {
         for (var i = 0; i < items.length; i++) {
            if (items[i].href == currHref) {
//alert("activate tab " + i + ": href=" + currHref);
               // deactivate old item
               if (activeItem != null) activeItem.id = "";

               // activate new item
               activeItem = items[i];
               items[i].id = activeID;
               activeItem.blur();
            }
         }
      }
   }


   //---[ Private methods ]-------------
   function initNewMenu () { // private function called after new menu is attached
      if (items != null && items.length != null) {
         for (var i = 0; i < items.length; i++) {
            if (items[i].id == activeID) {
               activeItem = items[i];
               //activeItem.onclick = function (e) { return false; } // do nothing
            }
         }
      }
      currItem = null;
   }
   function trueFunc (item) { return true; }
   function falseFunc (item) { return false; }

   //---[ Initialization ]-------------
   containerID = containerIdName;
   itemTag     = itemTagName;

   this.setClickFunc(trueFunc);
   this.setMouseOverFunc(trueFunc);
   this.setMouseOutFunc(trueFunc);

   if (containerID != null && itemTag != null) {
      var container = document.getElementById(containerID);
      if (container != null) {
         items = container.getElementsByTagName(itemTag);
      }
   }
   this.setClassNames(idleClassName, hiliteIdName, activeIdName);
   initNewMenu();
}
