/*

  David Rodriguez Sanchez
  20060518

*/

/* #########################################################
                  DEFINICIÓN DEL OBJECTO MENÚ
   ######################################################### */
function ObjMenu(id, enlace, descripcion, nuevaventana, posicion, tipoventana, parametros, idnivel, nivel) {
  var tmpPosicion;

  // Atributos
  this.id           = id;
  this.enlace       = enlace;
  this.descripcion  = descripcion;
  this.nuevaventana = nuevaventana;
  this.colocado     = false;

  tmpPosicion       = posicion.toUpperCase();
  this.posicion     = (tmpPosicion.length == 1) ? tmpPosicion : tmpPosicion.charAt(1);
  this.tipo         = (tmpPosicion.length == 1) ? 'L' : 'S';

  this.hijos        = new Array();
  this.tipoventana  = tipoventana;
  this.parametros   = parametros;
  this.idnivel      = idnivel;
  this.nivel        = nivel;
  this.padre        = null;
  this.referencia   = null;
  this.flecha       = null;

  // Métodos
  this.setX    = setX;
  this.setY    = setY;
  this.setW    = setW;
  this.setH    = setH;
  this.getX    = getX;
  this.getY    = getY;
  this.getW    = getW;
  this.getH    = getH;
  this.muestra = muestra;
  this.oculta  = oculta;
}

function setX(x) {
  this.referencia.style.left = x;
}

function setY(y) {
    if (document.layers)  // NS4.x
      this.referencia.top = y;
    else
    this.referencia.style.top = y;
}

function setW(w) {
    if (document.layers)  // NS4.x
    this.referencia.pixelWidth = w;
    else
    this.referencia.style.pixelWidth = w;
}

function setH(h) {
  this.referencia.style.pixelHeight = h;
}

function getX() {
  return this.referencia.offsetLeft;
}

function getY() {
  return this.referencia.offsetTop;
}

function getW() {
  return this.referencia.offsetWidth;
}

function getH() {
  return this.referencia.offsetHeight;
}

function muestra() {
  this.referencia.style.visibility = "visible";
}

function oculta() {
  this.referencia.style.visibility = "hidden";
}
/* #########################################################
                  /DEFINICIÓN DEL OBJECTO MENÚ
   ######################################################### */




function writeMenu(ID, IDPadre, Imagen, Enlace, Descripcion, NuevaVentana, Posicion, Clase, ClaseAlt, TipoVentana, Parametros, IDNivel, Nivel) {
  if (ID != "") {
    var strMenu = "";
    var strImagen, strDescripcion, strFlecha;
    var div, i, maxW;

    var menu = new ObjMenu(ID, Enlace, Descripcion, NuevaVentana, Posicion, TipoVentana, Parametros, IDNivel, Nivel);

    if (!window.menus)
      window.menus = new Array();

    window.menus.push(menu);

    if (IDPadre != "") {
      for (i = 0 ; i < window.menus.length ; i++) {
        if (window.menus[i].id == IDPadre) {
          window.menus[i].hijos.push(menu);

          menu.padre = window.menus[i];
        }
      }
    }

    strMenu = '<div id="' + ID + '" name="' + (window.menus.length - 1) + '"';

    if (Clase != "")
      strMenu += ' class="' + Clase + '"';

//    strMenu += ' style="border:1px solid black;';
    strMenu += ' style="';

    if (IDPadre != "")
      strMenu += 'position:absolute;';

    if ((menu.tipo == "S") && (menu.nivel == "1"))
      strMenu += 'float:left;';

    if ((Enlace != "") || (TipoVentana == 1))
      strMenu += 'cursor:hand;';
    else
      strMenu += 'cursor:default;';

    if (ClaseAlt != "")
      strMenu += '" onmouseover="showMenu(this);this.className=\'' + ClaseAlt + '\';" onmouseout="hideMenu(this);this.className=\'' + Clase + '\'"';
    else
      strMenu += '" onmouseover="showMenu(this);" onmouseout="hideMenu(this);"';

    if ((Enlace != "") || (TipoVentana == 1))
      strMenu += ' onclick="dispMenu(this);"';

    strMenu += '>';

    strMenu += '<table width="100%" class="'+ Clase +'" border="0" style="border-width:0"';

    if (ClaseAlt != "")
      strMenu += ' onmouseover="this.className=\'' + ClaseAlt + '\'" onmouseout="this.className=\'' + Clase + '\'"';

    strMenu += '><tr>';

    if (Imagen != "")
      strImagen = '<td width="10%" align="left"><img src="' + Imagen + '" border="0"></td>';
    else
      strImagen = '';

    strDescripcion = '<td align="left">' + Descripcion + '</td>';

    if (Nivel != "1") {
      if (menu.posicion == "I") {
        if ((Enlace == "") && (TipoVentana == 0))
          strFlecha = '<td width="10%" align="right"><img valign="middle" src="images/menus/toRight.gif" id="imgFlecha' + ID + '"></td>';
        else
          strFlecha = '<td width="10%" align="right"><img valign="middle" src="images/menus/toRight.gif" style="visibility:hidden" id="imgFlecha' + ID + '"></td><td>'
      }
      else {
        if ((Enlace == "") && (TipoVentana == 0))
          strFlecha = '<td width="10%" align="left"><img align="left" valign="middle" src="images/menus/toLeft.gif" id="imgFlecha' + ID + '"></td><td>'
        else
          strFlecha = '<td width="10%" align="left"><img align="left" valign="middle" src="images/menus/toLeft.gif" style="visibility:hidden" id="imgFlecha' + ID + '"></td><td>'
      }
    }
    else
      strFlecha = '<td><span id="imgFlecha' + ID + '"></td>';

    if (menu.posicion == "I")
      strMenu += strImagen + strDescripcion + strFlecha;
    else
      strMenu += strFlecha + strDescripcion + strImagen;

    strMenu += '</tr></table>';

    strMenu += "</div>";

    document.write(strMenu);

    menu.referencia = document.getElementById(ID);
    menu.flecha = document.getElementById("imgFlecha" + ID);
  }
}


function showMenu(menu) {
  var i, posY;
  var menuHijo, mn, mn1;

  mn = window.menus[parseInt(menu.name)];

  // Muestra a los hermanos (si no es de nivel 0)
  if (mn.padre)
    for (i = 0 ; i < mn.hermanos.length ; i++)
      mn.hermanos[i].muestra();

  // Muestra todos los niveles hasta el actual (si no es de nivel 0)
  mn1 = mn;
  while (mn1.padre) {
    for (i = 0 ; i < mn1.padre.hermanos.length ; i++)
      mn1.padre.hermanos[i].muestra();
    mn1 = mn1.padre;
  }

  // Muestra a los hijos
  if (mn.hijos) {
    // Obtiene la posición inicial de los hijos
    if (!mn.padre && (mn.tipo == "S"))
      posY = mn.getY() + mn.getH();
    else
      posY = mn.getY();

    for (i = 0 ; i < mn.hijos.length ; i++) {
      mn.hijos[i].setY(posY);
      mn.hijos[i].setW(mn.hijos[i].getW());
      mn.hijos[i].muestra();
      posY += mn.hijos[i].getH();
    }
  }
}


function hideMenu(menu) {
  var i, mn = window.menus[parseInt(menu.name)];

  if (mn.hijos)
    for (i = 0 ; i < mn.hijos.length ; i++)
      mn.hijos[i].oculta();

  while (mn.padre) {
    mn = mn.padre;

    for (i = 0 ; i < mn.hijos.length ; i++)
      mn.hijos[i].oculta();
  }
}


function dispMenu(menu) {
  var mn = window.menus[parseInt(menu.name)];

  if (mn.tipoventana == 1)
    open("popup.aspx?idnivel=" + mn.idnivel + "&nivel=" + mn.nivel, "_blank", mn.parametros);
  else {
    if (mn.nuevaventana == 1)
      open(mn.enlace, "_blank", mn.parametros);
    else
      open(mn.enlace, "_self");
  }
}


function visMenu() {
  var i, j, maxW, menu1, menu2, aux, acum;

  if (window.menus) {
    // Obtencion del array de hermanos para cada item de menu
    for (i = 0 ; i < window.menus.length ; i++) {
      menu1 = window.menus[i];

      menu1.referencia.parentNode.style.position = "relative";
      menu1.referencia.parentNode.align = "left";

      for (j = 0 ; j < window.menus.length ; j++) {
        menu2 = window.menus[j];
        if ((!menu1.padre && !menu2.padre) || (menu1.padre && menu2.padre && (menu1.padre.id == menu2.padre.id))) {
          if (!menu1.hermanos)
            menu1.hermanos = new Array();

          menu1.hermanos.push(menu2);
        }
      }
    }

    // Obtiene el máximo ancho y la posición horizontal de cada grupo y se los asigna a los items de menu
    // Visualiza u oculta la flecha en función de que el item tenga o no hijos
    for (i = 0 ; i < window.menus.length ; i++) {
      menu1 = window.menus[i];

      if (menu1.hijos.length == 0)
        menu1.flecha.style.visibility = "hidden";

      if (!menu1.colocado) {
        maxW = 0;
        for (j = 0 ; j < menu1.hermanos.length ; j++) {
          if (maxW < menu1.hermanos[j].getW())
            maxW = menu1.hermanos[j].getW();
        }

        for (j = 0 ; j < menu1.hermanos.length ; j++) {
          // Carga de anchura
          menu1.hermanos[j].colocado = true;
          if (menu1.nivel != 1)
            menu1.hermanos[j].setW(maxW);

          // Carga de posición X
          if (menu1.tipo == "L") {
            if (menu1.hermanos[j].posicion == "D")
              aux = menu1.hermanos[j].padre ? menu1.hermanos[j].padre.getX() - menu1.hermanos[j].getW() - (menu1.hermanos[j].padre.padre ? 0 : 2) : menu1.hermanos[j].getX() + 2;
            else
              aux = menu1.hermanos[j].padre ? menu1.hermanos[j].padre.getX() + menu1.hermanos[j].padre.getW() : menu1.hermanos[j].getX();
          }
          else
          if (menu1.tipo == "S") {
            if (menu1.hermanos[j].posicion == "D") {
              if (menu1.hermanos[j].nivel == 1)
                aux = menu1.hermanos[j].getX();
              else
              if (menu1.hermanos[j].nivel == 2)
                aux = menu1.hermanos[j].padre.getX() - (menu1.hermanos[j].padre.getW() > menu1.hermanos[j].getW() ? 0 : menu1.hermanos[j].getW() - menu1.hermanos[j].padre.getW());
              else
                aux = menu1.hermanos[j].padre.getX() - menu1.hermanos[j].getW();
            }
            else {
              if (menu1.hermanos[j].nivel == 1)
                aux = menu1.hermanos[j].getX();
              else
              if (menu1.hermanos[j].nivel == 2)
                aux = menu1.hermanos[j].padre.getX() + (menu1.hermanos[j].padre.getW() < menu1.hermanos[j].getW() ? 0 : menu1.hermanos[j].padre.getW() - menu1.hermanos[j].getW());
              else
                aux = menu1.hermanos[j].padre.getX() + menu1.hermanos[j].padre.getW();
            }
          }

          menu1.hermanos[j].setX(aux);

          // Carga de posicion Y
          if (j == 0)
            acum = menu1.hermanos[0].padre ? menu1.hermanos[0].padre.getY() : 0;
          else
            acum += menu1.hermanos[j - 1].getH();

          menu1.hermanos[j].setY(acum);
        }
      }
    }

    // Visualiza los items de primer nivel y oculta el resto
    for (i = 0 ; i < window.menus.length ; i++) {
      if (!window.menus[i].padre)
        window.menus[i].muestra();
      else
        window.menus[i].oculta();
    }
  }
}

function redimensiona(imagen, maxW, maxH) {
  var newW, newH;

  newW = imagen.width;
  newH = imagen.height;

  if (newW > maxW) {
    newH *= (maxW / newW);
    newW = maxW;
  }

  if (newH > maxH) {
    newW *= (maxH / newH);
    newH = maxH;
  }

  imagen.width = newW;
  imagen.height = newH;
}
