function getElementsByClassName(classname){
  var eles = document.getElementsByTagName("*");
  var r = new Array();
  for(var i = 0; i < eles.length; i++){
    if(eles[i].className == classname){
      r.push(eles[i]);
    }
  }
  return r;
}
function setUpNavigations(){
  // el timeout para esconder todo este desmadre
  navigations_timeout = false;
  // una cosa son las navigations...
  navigations = getElementsByClassName("navigation_hor");
  // y otra los elementos de estas, esto solo es para IE y Safari
  navigations_elements = new Array();
  // funcion para clonar el array con los indexes de los ancestros
  function cloneIndexes(ixs,offset){
    var r = new Array();
    for(var i = offset; i < ixs.length; i++){
      r.push(ixs[i]);
    }
    return r;
  }
  // funcion para recorrer el array de las navigations y ocultar todo
  //function hideNavigations(){
  window.hideNavigations = function(ixs){
    for(var i = 0; i < navigations.length; i++){
      if(ixs && ixs.length > 0 && ixs[0] == i){
        hideNavigation(navigations[i],cloneIndexes(ixs,1));
      }else{
        hideNavigation(navigations[i]);
      }
    }
  }
  function hideNavigation(nav,ixs){
    for(var i = 0; i < nav.childNodes.length; i++){
      if(nav.childNodes[i].childNodes.length > 1){
        if(ixs && ixs.length > 0 && ixs[0] == i){
          hideNavigation(nav.childNodes[i].childNodes[1],cloneIndexes(ixs,1));
        }else{
          nav.childNodes[i].childNodes[1].style.display = "none";
          nav.childNodes[i].childNodes[1].style.visibility = "hidden";
          hideNavigation(nav.childNodes[i].childNodes[1]);
        }
      }
    }
  }
  // -------------------------------------- el parent de las siguientes clases un simple item
  function NavigationItem(n,ixs){// el <li> asociado con este objeto, y al array con los indexes de sus ancestros
    this.nodo = n;
    this.indexes = ixs;
    this.listenToEvent(this.nodo.childNodes[0], "mouseover");
    this.listenToEvent(this.nodo.childNodes[0], "mouseout");
    this.listenToEvent(this.nodo.childNodes[0], "click");
  }
  NavigationItem.prototype.handleEvent = function(ev){
    switch(ev.type){
      case "mouseover":
      clearTimeout(navigations_timeout);
      hideNavigations(this.indexes);
      break;
      case "mouseout":
      navigations_timeout = setTimeout("hideNavigations();",50);
      break;
      case "click":
      hideNavigations();
    }
    //window.status = this.indexes.toString();
  }
  if(navigator.userAgent.indexOf("Safari") < 0 && navigator.userAgent.indexOf("MSIE") < 0 ){
    NavigationItem.prototype.listenToEvent = function(ev_dispatcher,ev_nombre){
      ev_dispatcher.addEventListener(ev_nombre,this,false);
    }
  }else{
    function navsRightThisEvent(){
    loop_i:
      for(var i = 0; i < navigations_elements.length; i++){
        for(var j = 0; j < navigations_elements[i].length; j++){
          if(navigations_elements[i][j].nodo.childNodes[0] == this){
            navigations_elements[i][j].handleEvent(event);
            break loop_i;
          }
        }
      }
    }
    NavigationItem.prototype.listenToEvent = function(ev_dispatcher,ev_nombre){
      ev_dispatcher["on"+ev_nombre]=navsRightThisEvent;
    }
  }
  // ---------------------------------------- subclass menu Horizontal
  function NavigationMenuHor(n,ixs){
    NavigationItem.call(this,n,ixs);
  }
  // heredar el prototype
  for(i in NavigationItem.prototype){
    NavigationMenuHor.prototype[i] = NavigationItem.prototype[i];
  }
  NavigationMenuHor.prototype.handleEvent = function(ev){
    NavigationItem.prototype.handleEvent.call(this,ev);
    this.nodo.childNodes[1].style.display = "block";
    this.nodo.childNodes[1].style.visibility = "visible";
    this.nodo.childNodes[1].style.left = this.nodo.offsetLeft.toString()+"px";
    if(this.nodo.childNodes[1].offsetLeft != this.nodo.offsetLeft){
      this.nodo.childNodes[1].style.left = this.nodo.offsetLeft - (this.nodo.childNodes[1].offsetLeft - this.nodo.offsetLeft) + "px";
    }
    this.nodo.childNodes[1].style.top = (this.nodo.offsetHeight + 4) + "px";
  }
  // ---------------------------------------- subclass menu Vertical
  function NavigationMenuNested(n,ixs){
    NavigationItem.call(this,n,ixs);
  }
  // heredar el prototype
  for(i in NavigationItem.prototype){
    NavigationMenuNested.prototype[i] = NavigationItem.prototype[i];
  }
  NavigationMenuNested.prototype.handleEvent = function(ev){
    NavigationItem.prototype.handleEvent.call(this,ev);
    this.nodo.childNodes[1].style.display = "block";
    this.nodo.childNodes[1].style.visibility = "visible";
    this.nodo.childNodes[1].style.left = (this.nodo.offsetLeft + this.nodo.offsetWidth)+2+"px";
    this.nodo.childNodes[1].style.top = (this.nodo.offsetTop + 4) + "px";
  }
  // -----------------------------------------------------------------
  // funciones para transversar los elementos de un menu y crear los objetos que correspondan
  // -------------------------------------- el primer nivel de un menu horizontal
  function setUpNavigationMenuHor(navs_ele,menu){
    for(var i = 0; i < menu.childNodes.length; i++){
      if(menu.childNodes[i].childNodes.length == 1){
        navs_ele.push(
                      new NavigationItem(menu.childNodes[i])
                      );
      }else{
        navs_ele.push(
                      new NavigationMenuHor(menu.childNodes[i])
                      );
        setUpNavigationMenu(navs_ele,menu.childNodes[i].childNodes[1]);
      }
    }
  }
  // -------------------------------------- los siguientes niveles de un menu
  function setUpNavigationMenu(navs_ele,menu,ixs){
    var ixs_plus_i;
    for(var i = 0; i < menu.childNodes.length; i++){
      ixs_plus_i = cloneIndexes(ixs,0);
      ixs_plus_i.push(i);
      if(menu.childNodes[i].childNodes.length == 1){
        navs_ele.push(
                      new NavigationItem(menu.childNodes[i],ixs_plus_i)
                      );
      }else{
        //ixs_plus_i.push(1);
        if(ixs_plus_i.length > 2){
          navs_ele.push(
                        new NavigationMenuNested(menu.childNodes[i],ixs_plus_i)
                        );
        }else{
          navs_ele.push(
                        new NavigationMenuHor(menu.childNodes[i],ixs_plus_i)
                        );
        }
        setUpNavigationMenu(navs_ele,menu.childNodes[i].childNodes[1],ixs_plus_i);
      }
    }
  }
  // ----------------------------------------------------------------------------------------
  var ixs;
  for(var i = 0; i < navigations.length; i++){
    ixs = new Array();
    ixs.push(i);
    navigations_elements[i] = new Array();
    setUpNavigationMenu(navigations_elements[i],navigations[i],ixs);
  }
}
window.onload = setUpNavigations;
