if (typeof onload == 'function') __tree_old_onload = onload;

var __docloc = document.location.toString();
var __doclocEnd = parseUri(__docloc).path;
__doclocEnd = __doclocEnd.substring(__doclocEnd.lastIndexOf('/')+1);
var __query = document.location.search.toString().replace(/idi=\d/,"").replace(/\?/,"").replace(/^&/,"");
var __nChild = 0;
var __menuIzqHeight = 0;
var __areaAccesoHeight = 0;
var __separadorZona2Height = 94;
var __IE7 = ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion.indexOf("MSIE 7.") > -1));

function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};

    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }

    // Always end directoryPath with a trailing backslash if a path was present in the source URI
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }

    return uri;
}

function __build_menu_izquierdo() {
  var tree = new Bolo.Tree("menu_izq", {
    toggle: Bolo.Tree.SameBranchToggle,
    nodeOptions: {
      onclick: function (e) {
        if (this.container.firstChild.onclick) { eval(this.onclick) }
        else if (this.container.firstChild.href) { document.location = this.container.firstChild.href}
        else if (this.container.firstChild.tagName == "SPAN") { __resize_menu_izq(); __resize_vertical_zona_2(); }
        Event.stop(e);
        return false;
      }
    },
    plugins: [
      function (n) {
        try {
          var nodeHref = n.container.firstChild.getAttribute("href");
          if (nodeHref) {
            var loc  = parseUri(nodeHref);
            var source = loc.source;
            var path = loc.path.endsWith("/alianzas.htm") ? loc.path : loc.path.replace(/\.htm/,"");
            var pathEnd = path.substring(path.lastIndexOf('/')+1);
            var query = loc.query.replace(/idi=\d/,"").replace(/\?/,"").replace(/^&/,"");
            var samePath = path && (path != "/") && !(source.indexOf("#") > -1) && ((__docloc.indexOf(path) > -1) || (pathEnd.indexOf(__doclocEnd) > -1));
            var sameQuery = (query && query.indexOf(__query) > -1);
            if (samePath && (!query || (query && sameQuery))) {
              if (n.container.childNodes[1] && n.container.childNodes[1].parentNode && n.container.childNodes[1].parentNode.firstChild) {
                if (path.indexOf(n.container.childNodes[1].firstChild.firstChild.pathname.replace(/\.htm/,"")) == -1) {
                  Element.addClassName(n.container, "Current");
                }
              } else {
                Element.addClassName(n.container, "Current");
              }
            }
          }
          if (n.container.parentNode && (n.container.parentNode.id == "menu_izq")) {
            if (__nChild < n.container.parentNode.childNodes.length - 1) {
              var img = document.createElement('img');
              img.src = "/porquetuvuelves/images/menu_derecho/separador_mnu.gif";
              img.alt = img.title = "";

              var li = document.createElement('li');
              li.className = "Separator";
              li.appendChild(img);

              n.container.parentNode.insertBefore(li, n.container.parentNode.childNodes[++__nChild]);
            }
            __nChild++;
          }
          if (n.container.firstChild.innerHTML.length == 28) { n.container.firstChild.className = "condensed"; }
          if ((nodeHref.toUpperCase().indexOf("CALCULADORA") > -1)) { n.container.firstChild.href = nodeHref.replace(/idi=\d/,"").replace(/\?/,""); }
        } catch(e) { console.error(e) };
      }]
  });
}

function __resize_menu_izq() {
  var style = $('navegacion_right_style');
  var img = document.createElement('img');
  var imgUrl;
  if (navigator.appName == "Microsoft Internet Explorer") {
    imgUrl = style.styleSheet.rules[0].style.backgroundImage;
  }
  else { imgUrl = style.sheet.cssRules[0].style.backgroundImage; }

  img.src = imgUrl.toString().replace(/url\(/,"").replace(/\)/,"");
  if (__menuIzqHeight < (img.height + 12)) __menuIzqHeight = img.height + 12;
  if ((__IE7 ? $('menu_izq').parentNode.offsetHeight - 12 : $('menu_izq').parentNode.clientHeight) <= __menuIzqHeight) {
    $('menu_izq').style.height = __menuIzqHeight + 'px';
  } else if (__IE7) {
    __menuIzqHeight = $('menu_izq').parentNode.offsetHeight - 12;
    $('menu_izq').style.height = __menuIzqHeight + 'px';
  }
}

function __resize_form_areareservada() {
  var style = $('navegacion_right_areareservada_style');
  var img = document.createElement('img');
  var imgUrl;
  if (navigator.appName == "Microsoft Internet Explorer") {
    imgUrl = style.styleSheet.rules[0].style.backgroundImage;
  }
  else { imgUrl = style.sheet.cssRules[0].style.backgroundImage; }

  img.src = imgUrl.toString().replace(/url\(/,"").replace(/\)/,"");
  if (__areaAccesoHeight < (img.height + (__IE7 ? 14 : 8))) __areaAccesoHeight = img.height + (__IE7 ? 14 : 8);
  if ((__IE7 ? $('form_areareservada').parentNode.offsetHeight - 41 : $('form_areareservada').parentNode.clientHeight) <= __areaAccesoHeight) {
    $('form_areareservada').style.height = __areaAccesoHeight + 'px';
  }
}

function __resize_vertical_zona_2() {
  var contenidoZona2Height = $('contenido_zona_2').clientHeight - 10;
  var navegacionContenidoHeight = $('navegacion_contenido').clientHeight - 12;

  if ((__separadorZona2Height < contenidoZona2Height) || (__separadorZona2Height < navegacionContenidoHeight)) {
    __separadorZona2Height = (contenidoZona2Height > navegacionContenidoHeight) ? contenidoZona2Height : navegacionContenidoHeight;
  }
  $('separador_zona_2').style.height = __separadorZona2Height + 'px';
}

function __check_menu_izq() {
  if ($('menu_izq')) {
    __build_menu_izquierdo();
    __resize_menu_izq();
    __sfHover("menu_izq");
    __resize_vertical_zona_2();
  }
  else setTimeout(__check_menu_izq, 500);
}

function __check_form_areareservada() {
  if ($('form_areareservada')) {
    __resize_form_areareservada();
  }
  else setTimeout(__check_form_areareservada, 500);
}

function __check_vertical_zona_2() {
  if ($('separador_zona_2')) {
    __resize_vertical_zona_2();
  }
  else setTimeout(__check_vertical_zona_2, 500);
}

FastInit.addOnLoad(__check_form_areareservada);
FastInit.addOnLoad(__check_menu_izq);
FastInit.addOnLoad(__check_vertical_zona_2);

