// Código original de Ismael Jurado
// http://www.ismaelj.com/articulos/menu-vertical-multinivel/
// Este código es libre de usarse y modificarse en cualquier proyecto personal
// o comercial siempre que se mantenga este comentario intacto
// Versión 2.0

function DHmenuvm(tagId) {
  function dc(l, s) {
    for (var c = l.firstChild; c = c.nextSibling;)
      if (c.tagName == 'UL') c.style.display = s? 'inline' : 'none';
  }

  tagId = (tagId && (typeof(tagId) == 'string')) || 'DHmenuvm'

  var dss = document.styleSheets[0]
  var ss0 = dss.cssRules || dss.rules

  for (var x in ss0)
    if ((ss0[x].selectorText + '').toLowerCase() == '#' + tagId.toLowerCase() + ' li ul')
      ss0[x].style.display = 'none'

  var li = document.getElementById(tagId)

  if (li) {
    li = li.getElementsByTagName('LI');
    var prev_pc = 0;
    var prev_li = null;

    for (var i in li) {
      li[i].onclick = function(e) {

        var pc = 0;
        var t = this;

        while(t = t.parentNode) if (t.tagName == 'UL') pc++;

        if (pc <= prev_pc) {
          dc(prev_li, false);
          for (var c = prev_li, d = prev_pc - pc; d > 0 ;c = c.parentNode)
            if (c.tagName == 'UL') { c.style.display = 'none'; d-- }
        }

        dc(this, true);

        prev_pc = pc;
        prev_li = this;

        if (e) e.cancelBubble = true; else event.cancelBubble = true;
        if (this.className.indexOf('parent') > -1) return false
      }
    }
  }
}

