if(!window.Node){
  var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}

function checkNode(node, filter){
  return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}

function getChildren(node, filter){
  var result = new Array();
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    if(checkNode(children[i], filter)) result[result.length] = children[i];
  }
  return result;
}

function getChildrenByElement(node){
  return getChildren(node, "ELEMENT_NODE");
}

function getFirstChild(node, filter){
  var child;
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    child = children[i];
    if(checkNode(child, filter)) return child;
  }
  return null;
}

function getFirstChildByText(node){
  return getFirstChild(node, "TEXT_NODE");
}

function getNextSibling(node, filter){
  for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
    if(checkNode(sibling, filter)) return sibling;
  }
  return null;
}
function getNextSiblingByElement(node){
        return getNextSibling(node, "ELEMENT_NODE");
}

var activeMenu = null;

function showMenu() {
  if(activeMenu){
    activeMenu.className = "";
    getNextSiblingByElement(activeMenu).style.display = "none";
		var texto = "+ " + activeMenu.innerHTML.substring(2);
		activeMenu.innerHTML = texto;
  }
  if(this == activeMenu){
    activeMenu = null;
  } else {
    this.className = "active";
		var texto = "- " + this.innerHTML.substring(2);
		this.innerHTML = texto;
    getNextSiblingByElement(this).style.display = "block";
    activeMenu = this;
  }
  return false;
}

function initMenu(){
  preparamenu();
  var menus, menu, text, a, i;
  menus = getChildrenByElement(document.getElementById("menu"));
  for(i = 0; i < menus.length; i++){
    menu = menus[i];
    text = getFirstChildByText(menu);
		text.nodeValue = "+ " + text.nodeValue;
    a = document.createElement("a");
    menu.replaceChild(a, text);
    a.appendChild(text);
    a.href = "#";
    a.onclick = showMenu;
    a.onfocus = function(){this.blur()};
  }
}

function preparamenu() {
var menu = varmenu();
var link = varlink();
menu.sort();
menu.reverse();

var stringHTML="";
var base = "";
var iter=0;
while (iter < menu.length) {
      if (menu[iter][1] != base) {
			   if (base) { stringHTML += '</ol></li>';}
				 base = menu[iter][1];
				 if (base) { stringHTML += '<li>' + base + '<ol>'; }
			}
			stringHTML += '<li><a href="' + link + ',' + menu[iter][0] + '">' + menu[iter][2] + '</a></li>';
			iter++;
}
stringHTML += '</ol></li>';

var divmenu = document.getElementById("menu");
divmenu.innerHTML = stringHTML;
}

if(document.createElement) window.onload = initMenu;
