function OnMouseOver (newID)
{
  // Change the style of the currently selected icon
  var elementID = "main_"+newID
  
  if (mainMenu.GetMainMenuID() != newID)
  {
    document.getElementById (elementID).className = "topNavigationSelected"
    document.getElementById ("Image_"+elementID).src = mainMenu.selectedImages[newID].src
  }
}

function OnMouseOut (newID)
{
  // Change the style of the currently selected icon
  var elementID = "main_"+newID
  if (mainMenu.GetMainMenuID() != newID)
  {
    document.getElementById (elementID).className = "topNavigationNotSelected"
    document.getElementById ("Image_"+elementID).src = mainMenu.images[newID].src
  }
}

// Constructor
function Menu (mainMenuID, sideMenuID) 
{
  this.selectedMenu   = "";         // The currently selected menu
  this.helpText       = new Array ();  // Help text for each page
  this.mainMenuCount  = 0;          // The number of main menu items
  this.currentPages   = new Array ();  // The last selected page in each main menus
                                    // tree menu
  this.selectedImages = new Array ();
  this.images         = new Array ();
  this.startPage      = null;       // startPage and startMenu allow the user
  this.startMenu      = null;       // to specify what page to start on.
  // The name of the cookie to store the current page for each main menu.
  this.currentPagesCookieName     = "_currentPage";
  // The cookie to store the current pages for each main menu.
  this.currentPagesCookie         = new CookieDef(0,"/","","0");
  // The name of cookie to store the currently selected menu.
  this.currentMenuCookieName      = "_currentMenu";
  // The cookie to store the currently selected menu.
  this.currentMenuCookie          = new CookieDef(0,"/","","0"); 
  // The name of cookie to store the currently selected menu.
  this.currentMenuCookieName      = "_menuStates";
  // An array of cookies to store the currently selected menu state.
  this.menuStateCookies           = new Array;
  // The character used to seperate the elements in cookie
  this.kCookieElemSeperator       = "|";  
  // The character used to seperate the values in the elements in the cookie
  this.kCookieValueSeperator      = ":";
  this.trees          = new Array ();  // The menu trees
  // The name of the element in the document that contains the main menu.
  this.mainMenuID     = mainMenuID;
  // The name of the element in the document that contains the side menu.
  this.sideMenuID     = sideMenuID;
}

Menu.prototype.SetStartPage = function (startPage)
{
  this.startPage = startPage;
}

Menu.prototype.SetStartMenu = function (startMenu)
{
  this.startMenu = startMenu;
}

Menu.prototype.SetHelpText = function (url, text)
{
  this.helpText [url] = ((text && text != "") ? text : url + " help text");
  //alert ("SetHelpText ("+url+", "+this.helpText [url]+")");
}

Menu.prototype.SetCurrentMenuCookie = function (value)
{                                                                      
   //alert ("Save the current menu in a cookie as \""+value+"\".");
   this.currentMenuCookie.setValue (this.currentMenuCookieName, value);
}

Menu.prototype.GetCurrentMenuCookie = function ()
{
   return this.currentMenuCookie.getValue (this.currentMenuCookieName);
}

Menu.prototype.GetHelpText = function (url)
{
  if (url == null) url = this.GetCurrentPage ();
  return this.helpText [url]
}

Menu.prototype.GetMenuTree = function (id)
{
  return this.trees [id]
}

Menu.prototype.GetCurrentTree = function ()
{
  return this.GetMenuTree (mainMenu.GetMainMenuID());
}

Menu.prototype.SetMainMenuID = function (id)
{
  // This assusmes that the user menu is always the last menu added.
  // This is for old pre release versions this can be removed before release
  if (typeof id == "string" && id.length > 1) id = this.mainMenuCount -1; 
  if (id < 0 || id >= this.mainMenuCount) id = this.mainMenuCount -1;
  this.selectedMenu = id;
  this.SetCurrentMenuCookie (id);
}

Menu.prototype.GetMainMenuID = function ()
{
  // Save this in a cookie
  return this.selectedMenu;
}

Menu.prototype.ArrayToCookieString = function (array)
{
  var index;
  var results = "";
  for (index in array)
  {
    if (results !="") results += this.kCookieElemSeperator;
    results += index + this.kCookieValueSeperator + array[index];
  }
  return results;
}

Menu.prototype.CookieStringToArray = function (string)
{
  var index;
  var index2;
  var elements;
  var indexValuePair;
  var results = new Array;

  if (!string || string == "") return results;

  elements = string.split (this.kCookieElemSeperator);
  for (index in elements)
  {
    indexValuePair =  elements[index].split (this.kCookieValueSeperator)
    results [indexValuePair[0]] = indexValuePair[1]; 
  }
  return results;
}

/*
Menu.prototype.SetCurrentMenuState = function (index, currentPages)
{
   this.menuStateCookies[index].setValue (this.currentPagesCookieName+"_"+index,
     this.ArrayToCookieString (currentPages));
}

Menu.prototype.GetCurrentMenuState = function (index)
{
   return this.menuStateCookies[index].getValue (this.currentPagesCookieName+"_"+index).;
}
*/

Menu.prototype.SetCurrentPagesCookie = function (currentPages)
{
   this.currentPagesCookie.setValue (this.currentPagesCookieName,
     this.ArrayToCookieString (currentPages));
}

Menu.prototype.GetCurrentPagesCookie = function ()
{
   return this.currentPagesCookie.getValue (this.currentPagesCookieName);
}

Menu.prototype.GetCurrentPage = function ()
{
  return this.currentPages [this.GetMainMenuID()];
}

Menu.prototype.SetCurrentPage = function (url)
{
  // Save this in a cookie.
  this.currentPages[this.GetMainMenuID()] = url;
  this.SetCurrentPagesCookie (this.currentPages);
}

Menu.prototype.GetMainMenuName = function ()
{
  return this.GetCurrentTree().root.label
}

Menu.prototype.FindMainMenuId = function (url)
{
  for (var tree in this.trees)
  {
    for (var node in this.trees[tree].nodes)
    {
      if (this.trees[tree].nodes[node].url == url)
        return this.trees[tree].index
    }
  }
}

Menu.prototype.FindNodeId = function (url)
{
  var rValue = null
  for (var tree in this.trees)
  {
    rValue = this.trees[tree].FindNodeId (url)
    if (rValue) break
  }
  return rValue
}

Menu.prototype.AddNavImage = function AddNavImage (image, label)
{
  var newElement = document.createElement("td")
  newElement.noWrap = true
  //var d = new jsDocument

  newElement.id="main_"+this.mainMenuCount;
  newElement.className="topNavigationNotSelected";
  newElement.width="20%";
  newElement.onmouseout =
    new Function ("OnMouseOut"+this.mainMenuCount,
                  "OnMouseOut('"+(this.mainMenuCount)+"');");
  newElement.onmouseover =
    new Function ("OnMouseOver"+this.mainMenuCount,
                  "OnMouseOver('"+(this.mainMenuCount)+"');");
  newElement.onclick =
    new Function ("OnSubmit"+this.mainMenuCount,
                  "ChangeMainPage('"+(this.mainMenuCount)+"');");

  // Append the image
  newElement.appendChild (CreateElement ('<img id="Image_'+newElement.id+
    '" class="mainMenuImage" align="absmiddle" src="'+image+'">'))
  
  // Append the label
  newElement.appendChild (document.createTextNode (' '+label))

  // Append the table element to the table.
  document.getElementById (this.mainMenuID).appendChild (newElement)
}
  
Menu.prototype.AddMainMenuItem = function (image, selectedImage, id, label, url)
{

  this.images[this.mainMenuCount] = new Image ()
  this.images[this.mainMenuCount].src = image
  this.selectedImages[this.mainMenuCount] = new Image ()
  this.selectedImages[this.mainMenuCount].src = selectedImage
  
  this.AddNavImage (this.images[this.mainMenuCount].src, label)

  if (!this.currentPages [this.mainMenuCount])
  {
    // alert ("Setting the current page for "+label);
    this.currentPages [this.mainMenuCount] = url;
  }
  // Add a new tree menu
  this.trees[this.mainMenuCount] = new jsTree (this.mainMenuCount)
  
 // Add a cookie to remember the current state of the menu.
  this.menuStateCookies [this.mainMenuCount] = new CookieDef(365,"/","","0");

  // Create the root
  this.trees[this.mainMenuCount].createRoot("", id, label, url, "");

  return this.trees[this.mainMenuCount++];
}

Menu.prototype.AddChildMenu = function (startNode, id, image, label,
  url, help, target, condition)
{
  this.SetHelpText (url, help);
  var newChild = startNode.addChild(id, image, label, url, target, condition);
  //alert (newChild+", "+newChild.root+", "+newChild.root.root+", "+newChild.root.root.index);
  // if the current url matchs what the user has passed in as the
  // start page and root menu if one was supcifed then make this entry the
  // currently selected page and save the start menu.
  if (url == this.startPage && (this.startMenu == null || 
             this.startMenu == newChild.root.index))
  {
    this.currentPages[newChild.root.index] = url;
    // This will basicly have no effect if the start menu was already
    // set.
    this.SetStartMenu (newChild.root.index);
    // Set the start page to null so that if the page is found in more than
    // one tree we go to the first one;
    this.SetStartPage (null);
  }
  // if the current url is the current page than save it.
  if (url == this.currentPages[newChild.root.index])
  {
     newChild.root.selectedObject = newChild;
  }
  return newChild;
}

