// 
//  Menu
//  Version: 2.4
//  Author: Tom Peters (tapeters@buffalo.edu)
//  Last Modified: 2007-02-08
//

function Menu(levels)
{
  this.url = new String(document.location);
  this.levels = new Array();
  this.colors = new Array();
    this.colors[1] = '#00f';
    this.colors[2] = '#00f';
    this.colors[3] = '#00f';
  this.side = new Obj('side');
  this.sideLinks = this.side.getElementsByTagName('a'); 


  this.boldLinks = false;
  this.boldNavigationLinks = false;
  this.boldSideLinks = false;

  this.numLevelsToSearch = (levels) ? levels : 5;

  this.setLevels = function()
  {
    var stack;

    stack = "";

    for (i=1; i<=this.numLevelsToSearch; i++)
    {
      var regex, match;

      regex = new RegExp("^http://[^/]+(/([A-Z_-]+)){" + i + "}","i");
      match = this.url.match(regex);

      if (match)
      {
        if (stack == "")
        {
          stack = match[2];
        }
        else
        {
          stack += "-" + match[2];
        }
        var level = new Obj(stack);

        if (level.id)
          this.levels.push(level);
      }
      else
        break;
    }
  }

  this.expandLevels = function()
  {
    if (this.levels.length)
    {
      for (i=0; i<this.levels.length; i++)
      {
        this.levels[i].style.display = 'block';
      }
    }
    else
    {
      // IE is having trouble with the next two lines
      // home = new Obj('home');
      // home.style.display = 'block'; 

      document.getElementById('home').style.display = 'block';
    }

    enableNewWindowLinks();
  }

  this.getLevels = function()
  {
    return this.levels;
  }

  this.expand = function(link,name)
  {
    var obj, eof;

    this.destylize();
    this.stylize();

    obj = new Obj(name);
    lists = obj.parentNode.parentNode.getElementsByTagName("ul");

    for (i=0; i<lists.length; i++)
      if (lists[i].id)
        lists[i].style.display = "none";

    this.expandLevels();
    this.stylize(link);
    obj.style.display = "block";
  }

  this.style = function()
  {
  }

  this.stylize = function(url)
  {
    var navigationLink, url

    url = (url) ? new String(url) : this.url;

    navigationLink = false;
    if (this.levels[0])
      navigationLink = new Obj('li-' + this.levels[0].id);
    else
      if (this.getDepth() == 0)
        navigationLink = new Obj('li-home');

    if (navigationLink.id)
    {
      navigationLink.getElementsByTagName('a')[0].style.color = this.colors[1];
      if (this.boldLinks || this.boldNavigationLinks)
        navigationLink.getElementsByTagName('a')[0].style.fontWeight = "bold";
    }

    if (true)
    {
      for (i=0; i<this.sideLinks.length; i++)
      {
        if (url.indexOf(this.sideLinks[i]) >= 0)
        {
          var numLevels, colorNumber;

          numLevels = this.getNumLevels(this.sideLinks[i]);
          colorNumber = (numLevels <=3) ? numLevels : 3;
          if (colorNumber > 1)
          {
            this.sideLinks[i].style.color = this.colors[colorNumber];

            if (this.boldLinks || this.boldSideLinks)
              this.sideLinks[i].style.fontWeight = "bold"; 
          }
        }
      }
    }
  }

  this.stylizeUrl = function(url)
  {
    var documentPath = this.url
    var url = new String(url);
    var regex = new RegExp("^http://[^/]+","i");
    var match = documentPath.match(regex);
    this.stylize(match + url);
  }

  this.destylize = function()
  {
    var l;

    for (l=0; l<this.sideLinks.length; l++)
    {
      this.sideLinks[l].style.color = "";
      this.sideLinks[l].style.fontWeight = "";
    }
  }

  this.anchor = function(url)
  {
    this.url = new String(url);
    this.destylize();
    this.stylize();
  }

  /* Difference between this.getDepth() and this.getNumLevels()
     this.getDepth() will return how many directories deep the current page is
     this.getNumLevels() will return how many ULs deep the current link is
  */

  this.getDepth = function()
  {
    var depth = 0;

    for (i=1; i<=this.numLevelsToSearch; i++)
    {
      var regex, match;

      regex = new RegExp("^http://[^/]+(/([A-Z_-]+)){" + i + "}","i");
      match = this.url.match(regex);

      if (match)
        depth += 1;
      else
        break;
    }
    return depth;
  }

  this.getNumLevels = function(obj)
  {
    var eof, count

    eof = false;
    count = 0;

    do
    {
      if (obj = obj.parentNode)
      {
        var nodeName = new String(obj.nodeName)
        if (nodeName.indexOf("UL") >= 0)
          count++;
      }
      else
      {
        eof = true;
      }
    }
    while(eof != true)

    return count + 1;
  }

  this.setColor = function(linkType, color)
  {
    switch(linkType)
    {
      case 'navigation':
        this.colors[1] = color;
        break;
      case 'side':
        this.colors[2] = color;
        break;
      case 'sub':
        this.colors[3] = color;
        break;
    }
  }

  this.setBoldLinks = function(value)
  {
    this.boldLinks = value;
  }
  this.setBoldNavigationLinks = function(value)
  {
    this.boldNavigationLinks = value;
  }
  this.setBoldSideLinks = function(value)
  {
    this.boldSideLinks = value;
  }

  this.setLevels(); 
}

function Obj(name)
{
  return document.getElementById(name);
} 
