// JavaScript Document
var toggleMenu = {

  theMenu : null,

  subMenus : null,

  navLinks : null,
	
	utilMenu : null,
	
	utilLinks : null,
	
  init : function() {

    // block non-compliant browsers and isolate element nodes
    if (document.getElementById && document.getElementsByTagName) {
       this.theMenu = document.getElementById('toc');
       this.subMenus = this.theMenu.getElementsByTagName('ul');
       this.navLinks = this.theMenu.getElementsByTagName('a');
       this.utilMenu = document.getElementById('menu');
       this.utilLinks = this.utilMenu.getElementsByTagName('a');
    }

    // turn off all submenus by default
    for (var i=0, allSubmenus = this.subMenus.length; i<allSubmenus; i++) {
        this.subMenus[i].className = 'hide';
    }

    // isolate links with titles; those are the global nav
    // associate those links with the flip function
    // number the links as well, to synchronize them with their submenu
    var counter = 0;
    for (var i=0, totalNav = this.navLinks.length; i<totalNav; i++) {
      if (this.navLinks[i].title) {
         this.navLinks[i].number = counter;
         this.addEvent(this.navLinks[i],'click',this.flip);
         counter++;
      }
    }
    counter=0;
		for (i=0, totalUtil = this.utilLinks.length; i<totalUtil; i++) {
			this.utilLinks[i].number = counter;
			this.addEvent(this.utilLinks[i],'click', this.flip);
			counter++;
		}

  },

  flip : function() {
    if (toggleMenu.subMenus[this.number].className == 'hide') {
        toggleMenu.subMenus[this.number].className = '';
    }
    else {
        toggleMenu.subMenus[this.number].className = 'hide';
    }
  },

  addEvent : function(obj, type, func) {
   if (obj.addEventListener) {obj.addEventListener(type, func, false);}
   else if (obj.attachEvent) {
       obj["e" + type + func] = func;
       obj[type + func] = function() {obj["e" + type + func] (window.event);}
       obj.attachEvent("on" + type, obj[type + func]);
   }
   else {obj["on" + type] = func;}
  }

}

toggleMenu.init();
