var treeMenuIndex = 0;
function TreeMenuItem(text, url, target, icon) {

  this.text = text;

  if (url)
    this.url = url;
  else
    this.url = "";
  if (target)
    this.target = target;
  else
    this.target = "";
  if (icon)
    this.icon = icon;
  else
    this.icon = "";

  this.submenu = null;
  this.index = treeMenuIndex++;
  this.makeSubmenu = TreeMenuMakeSubmenu;
}

function TreeMenuMakeSubmenu(menu) {

  this.submenu = menu;
}

function TreeMenu() {

  this.items = new Array();
  this.addItem = treeMenuAddItem;
}

function treeMenuAddItem(item) {

  this.items[this.items.length] = item;
}

var treeMenuDocument;
var treeMenuWidth;
var treeMenuExpand;
var treeMenuSelected;
var treeMenuSelectedFound;
var treeMenuScrollX;
var treeMenuScrollY;
var treeMenuLastItem;
var treeMenuDepth;
var treeMenuBars;

// C1999 Mike Hall
function treeMenuDisplay() {

  var i, cookie;

  cookie = getCookie(treeMenuName);
  if (!cookie) {
    if (!treeMenuExpand) {
      treeMenuExpand = new Array();
      for (i = 0; i < treeMenuIndex; i++)
        treeMenuExpand[i] = 0;
      treeMenuSelected = -1;
    }
  }
  else {
    treeMenuExpand = cookie.split(",");
    cookie = getCookie(treeMenuName + "-selected");
    if (!cookie)
      treeMenuSelected = -1;
    else
      treeMenuSelected = cookie;
  }

  treeMenuDocument = window.frames[treeMenuFrame].document;

  treeMenuDepth = 0;
  treeMenuBars = new Array();

  treeMenuSelectedFound = false;
  treeMenuScrollX = 36;
  treeMenuScrollY = 18;

  if (document.images)
    treeMenuDocument.open("text/html", "replace");
  else
    treeMenuDocument.open("text/html");
  treeMenuDocument.writeln('<head>');
  treeMenuDocument.writeln('<title>' + treeMenuRoot + '</title>');
  treeMenuDocument.writeln('<style type="text/css">a {text-decoration:none;}</style>');
  treeMenuDocument.writeln('</head>');
  treeMenuDocument.writeln('<body background="' + treeMenuBackground + '" bgcolor="' + treeMenuBgColor + '" text="' + treeMenuFgColor + '" link="' + treeMenuFgColor + '" alink="' + treeMenuFgColor + '" vlink="' + treeMenuFgColor + '" onLoad="parent.treeMenuScroll();">');
  treeMenuDocument.writeln('<table border=0 cellpadding=0 cellspacing=0>');
  treeMenuDocument.write('<tr valign=top><td>');
  treeMenuDocument.write('<a href="#" onClick="return parent.treeMenuClickRoot();">');
  treeMenuDocument.write('<img src="' + treeMenuImgDir + 'menu_root.gif" align=left border=0 vspace=0 hspace=0>');
  treeMenuDocument.write('<font face="' + treeMenuFont + '" size=' + treeMenuFontSize + '>&nbsp;' + treeMenuRoot + '&nbsp;</font>');
  treeMenuDocument.writeln('</td></tr>');
  treeMenuListItems(treeMenu);
  treeMenuDocument.writeln('</table>');
  treeMenuDocument.writeln('</body>');
  treeMenuDocument.close();
}

function treeMenuListItems(menu) {

  var i;

  for (i = 0; i < menu.items.length; i++) {
    if (i == menu.items.length - 1)
      treeMenuLastItem = true;
    else
      treeMenuLastItem = false;
    treeMenuDisplayItem(menu.items[i]);
  }
}

function treeMenuDisplayItem(item) {

  var bars, cmd, expanded, i, img, alt, link, more, submenu;

  if (item.index == treeMenuSelected)
    treeMenuSelectedFound = true;
  if (!treeMenuSelectedFound)
    treeMenuScrollY += 18;

  if (treeMenuExpand[item.index] == 1)
    expanded = true;
  else
    expanded = false;

  if (item.submenu)
    submenu = true;
  else
    submenu = false;
  if (item.url != "")
    link = true;
  else
    link = false;
  cmd = "return parent.treeMenuClick(" + item.index + ", " + link + ", " + submenu + ");";

  treeMenuDocument.write('<tr valign=top><td nowrap>');

  bars = new Array();
  for (i = 0; i < treeMenuDepth; i++) {
    if (treeMenuBars[i]) {
      treeMenuDocument.write('<img src="' + treeMenuImgDir + 'menu_bar.gif" align=left border=0 vspace=0 hspace=0>');
      bars[i] = true;
    }
    else {
      treeMenuDocument.write('<img src="' + treeMenuImgDir + 'menu_spacer.gif" align=left border=0 vspace=0 hspace=0>');
      bars[i] = false;
    }
    if (item.index == treeMenuSelected)
      treeMenuScrollX += 18;
  }

  more = false;
  if (item.submenu && treeMenuFolders >= 0)
    for (i = 0; i < item.submenu.items.length; i++)
      if (item.submenu.items[i].submenu != null || treeMenuFolders == 1)
        more = true;

  if (!more) {
    if (treeMenuLastItem) {
      img = "menu_corner.gif";
      bars[bars.length] = false;
    }
    else {
      img = "menu_tee.gif";
      bars[bars.length] = true;
    }
    treeMenuDocument.write('<img src="' + treeMenuImgDir + img + '" align=left border=0 vspace=0 hspace=0>');
  }

  if (item.url != "")
    treeMenuDocument.write('<a href="' + item.url + '" target="' + item.target + '" onClick="' + cmd + '">');
  else
    treeMenuDocument.write('<a href="#" onClick="' + cmd + '">');

  if (more) {
    if (expanded) {
      if (treeMenuLastItem) {
        img = "menu_corner_minus.gif";
        bars[bars.length] = false;
      }
      else {
        img = "menu_tee_minus.gif";
        bars[bars.length] = true;
      }
    }
    else {
      if (treeMenuLastItem) {
        img = "menu_corner_plus.gif";
        bars[bars.length] = false;
      }
      else {
        img = "menu_tee_plus.gif";
        bars[bars.length] = true;
      }
    }
    treeMenuDocument.write('<img src="' + treeMenuImgDir + img + '" align=left border=0 vspace=0 hspace=0>');
  }

  if (item.submenu) {
    if (expanded)
      img = "menu_folder_open.gif";
    else
      img = "menu_folder_closed.gif";
  }
  else {
    if (item.icon != "")
      img = item.icon;
    else if (item.url.indexOf("http://") == 0)
      img = "menu_link_external.gif";
    else
      img = "menu_link_local.gif";
  }
  if (treeMenuAltText)
    alt = ' alt="' + item.text + '"';
  else
    alt = '';
  treeMenuDocument.write('<img src="' + treeMenuImgDir + img + '"' + alt + ' align=left border=0 vspace=0 hspace=0>');

  if (item.index == treeMenuSelected)
    treeMenuDocument.write('<font face="' + treeMenuFont + '" size=' + treeMenuFontSize + '>&nbsp;<span style="background-color:' + treeMenuHiBg + ';color:' + treeMenuHiFg + ';">' + item.text + '</span></font>');
  else
    treeMenuDocument.write('<font face="' + treeMenuFont + '" size=' + treeMenuFontSize + '>&nbsp;' + item.text + '</font>');
  treeMenuDocument.write('</a>');
  treeMenuDocument.writeln('</td></tr>');

  treeMenuBars = bars;

  if (item.submenu && expanded) {
    treeMenuDepth++;
    treeMenuListItems(item.submenu);
    treeMenuDepth--;
  }
}

function treeMenuClick(n, link, submenu) {

  var date, cookie;

  if (!treeMenuExpand)
    treeMenuDisplay();

  if (submenu)
    treeMenuExpand[n] = 1 - treeMenuExpand[n];

  treeMenuSelected = n;
  var date = new Date ();
  date.setTime (date.getTime() + (86400 * 1000 * treeMenuDays));
  cookie = treeMenuExpand.toString();
  setCookie(treeMenuName, cookie, date);
  setCookie(treeMenuName + "-selected", treeMenuSelected, date);

  setTimeout("treeMenuDisplay()", 10);

  return link;
}

function treeMenuClickRoot() {

  treeMenuExpand = null;
  treeMenuSelected = null;

  deleteCookie(treeMenuName);
  deleteCookie(treeMenuName + "-selected");

  setTimeout("treeMenuDisplay()", 10);

  return false;
}

function treeMenuScroll() {

  var win, height, width;

  win = window.frames[treeMenuFrame];

  if (document.layers) {
    height = win.innerHeight;
    width = win.innerWidth;
  }
  else if (document.all) {
    height = win.document.body.clientHeight;
    width = win.document.body.clientWidth;
  }
  else if (document.images) {
    win.scroll(0, treeMenuScrollY);
    return;
  }
  else
    return;

  if (treeMenuScrollY > height)
    win.scrollBy(0, treeMenuScrollY);
  if (treeMenuScrollX > width)
    win.scrollBy(treeMenuScrollX, 0);
}


function setCookie (name, value, expires) {

  document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function getCookie(name) {

  var search;

  search = name + "=";
  offset = document.cookie.indexOf(search) ;
  if (offset != -1) {
    offset += search.length;
    end = document.cookie.indexOf(";", offset);
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}

function deleteCookie(name) {

  var expdate = new Date();
  expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
  setCookie(name, "", expdate);
}
