/*
 * DO NOT REMOVE THIS NOTICE
 *
 * PROJECT:   mygosuMenu
 * VERSION:   1.2.0b
 * COPYRIGHT: (c) 2003,2004 Cezary Tomczak
 * LINK:    http://gosu.pl/dhtml/mygosumenu.html
 * LICENSE:   BSD (revised)
 *
 * MODIFIED:  2007 Cameron Clark
 * CHANGES:   1) Close all other nodes when a new node is clicked
 *            2) Remembers current open node with a cookie
 */

function TreeMenu(id) {

  this.init = function() {
    if (!document.getElementById(this.id)) {
      alert("Element '"+this.id+"' does not exist in this document. TreeMenu cannot be initialized");
      return;
      }
    document.getElementById(this.id).className = "tree-menu";  // change class name to invoke tree menu styles
    this.parse(document.getElementById(this.id).childNodes, this.tree, this.id, 0);
    this.load();
    if (window.attachEvent) {
      window.attachEvent("onunload", function(e) { self.save(); });
      }
    else if (window.addEventListener) {
      window.addEventListener("unload", function(e) { self.save(); }, false);
      }
    }

  this.parse = function(nodes, tree, id, depth) {
    var a, lastLi;
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeType != 1) {
        continue;
        }
      if (nodes[i].tagName.toLowerCase() == "li") {
        lastLi = nodes[i];
        nodes[i].id = id + "-" + tree.length;
        tree[tree.length] = new Array();
        if (a = this.getA(nodes[i].childNodes)) {
          a.id = nodes[i].id + "-a";
          if (hasClassName(a,"on")) {
            if (hasClassName(a,"primary")) {
              this.id_primarynode = nodes[i].id;
              }
            this.id_activenode = nodes[i].id;
            }
          }
        if (nodes[i].childNodes && this.hasUl(nodes[i].childNodes)) {
          nodes[i].className = (depth == 0) ? "top-section" : "section";
          if (a) {
            if (depth == 0)
              eval("document.getElementById('"+a.id+"').onclick = function() {self.click('"+nodes[i].id+"'); return false;}");
            else
              eval("document.getElementById('"+a.id+"').onclick = function() {self.click('"+nodes[i].id+"');}");
            }
          }
        else {
          nodes[i].className = (depth == 0) ? "top-item" : "item";
          if (a) {
            if (depth == 0) {
              a.id = nodes[i].id + "-a";
              eval("document.getElementById('"+a.id+"').onclick = function() {self.closeAll('"+nodes[i].id+"'); return true;}");
              }
            }
          }
        }
      if (nodes[i].tagName.toLowerCase() == "ul") {
        nodes[i].style.display = "none";
        id = id + "-" + (tree.length - 1);
        nodes[i].id = id + "-section";
        tree = tree[tree.length - 1];
        }
      if (nodes[i].childNodes) {
        this.parse(nodes[i].childNodes, tree, id, depth+1); // run recursively through menu tree
        }
      }
    if (lastLi) {
      lastLi.className = lastLi.className + "-end";
      }
    }

  this.hasUl = function(nodes) {
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeType != 1) {
        continue;
        }
      if (nodes[i].tagName.toLowerCase() == "ul") {
        return true;
        }
      if (nodes[i].childNodes) {
        if (this.hasUl(nodes[i].childNodes)) {
          return true;
          }
        }
      }
    return false;
    }

  this.getA = function(nodes) {
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeType == 1) {
        if (nodes[i].tagName.toLowerCase() == "a") {
          return nodes[i];
          }
        return false;
        }
      }
    }

  this.click = function(id) {
    childList = document.getElementById(id + "-section");
    if (childList) {
      if (childList.style.display == "none") {
        this.show(id);
        this.hideOthers(document.getElementById(this.id).childNodes,id); // pass top-level menu object to start recursion
        this.id_opennode = id;                                           // set current node for saving in cookie
        }
      else {
        this.hide(id);
        this.id_opennode = (id.split("-").length > 2) ? id.substr(0,id.lastIndexOf("-")) : ""; // set parent node, or if at top level, set to no id, for saving in cookie
        }
      }
    }

  this.show = function(id) {
    childList  = document.getElementById(id + "-section");
    parentItem = document.getElementById(id);
    if (childList) {
      childList.style.display = "";
      parentItem.className = parentItem.className.replace(/section(-open)?/, "section-open");
      }
    }

  this.hide = function(id) {
    childList  = document.getElementById(id + "-section");
    parentItem = document.getElementById(id);
    if (childList) {
      childList.style.display = "none";
      parentItem.className = parentItem.className.replace(/section(-open)?/, "section");
      }
    }

  // runs through child nodes recursively to hide all but current node and its parents
  this.hideOthers = function(nodes,id) {
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeType == 1 && nodes[i].tagName.toLowerCase() == "li") { // find all child <li> elements
        childList = document.getElementById(nodes[i].id + "-section");
        if (childList) {
          if (id.indexOf(nodes[i].id) == -1) {  // if this is not the current node or one of its parents, hide it
            this.hide(nodes[i].id);
            }
          if (id != nodes[i].id && childList.childNodes) {  // if this is not the current node and it has child nodes, run this function recursively to hide them
            this.hideOthers(childList.childNodes,id);
            }
          }
        }
      }
    }

  this.closeAll = function(id) {
    this.hideOthers(document.getElementById(this.id).childNodes,'');
    this.id_opennode = '';
    }

  this.showParents = function(id) { // Note that this will work backwards from closest to farthest ancestor nodes
    var idPieces = id.split("-");
    var depth = idPieces.length-2;
    for (var p = 0; p < depth; p++) {
      idPieces.pop();
      this.show(idPieces.join("-"))
      }
    }

  this.save = function() {
    if (this.id_opennode) {
      this.cookie.set(this.id, this.id_opennode);
      }
    else {
      this.cookie.del(this.id);
      }
    }

  this.load = function() {
    var id_savednode = this.cookie.get(this.id);
    if (this.id_primarynode) {
      this.id_opennode = this.id_primarynode;
      }
    else if (id_savednode) {
      this.id_opennode = id_savednode;
      }
    else if (this.id_activenode) {
      this.id_opennode = this.id_activenode;
      }
    if (this.id_opennode) {
      this.showParents(this.id_opennode);
      this.show(this.id_opennode);
      }
    }

  function Cookie() {
    this.get = function(name) {
      var cookies = document.cookie.split(";");
      for (var i = 0; i < cookies.length; i++) {
        var a = cookies[i].split("=");
        if (a.length == 2) {
          a[0] = a[0].trim();
          a[1] = a[1].trim();
          if (a[0] == name) {
            return unescape(a[1]);
            }
          }
        }
      return "";
      }
    this.set = function(name, value) {
      // don't set expiry; default is to expire at end of session, which we want
      document.cookie = name + "=" + escape(value) + "; path=/";
      }
    this.del = function(name) {
      document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
      }
    }

  var self = this;
  this.id = id;
  this.tree = new Array();
  this.cookie = new Cookie();
  this.init();
  }