var isIE = false;
var item = '';
var contenedor = false;
var submenuActual = '';

function mostrarSubmenu(submenu)
 {
  var prefijo = 'submenu_';
  /*
  if(submenu == 'tarjetas')
   {
    mostrarLayer('video_lateral', true);
    document.getElementById('submenu').className = 'submenufondotarjetas';
   }
  else
   {
    mostrarLayer('video_lateral', false);
    document.getElementById('submenu').className = 'submenufondo';
   }
  */
  if(submenuActual != prefijo+submenu)
   {
    if(submenuActual.length > prefijo.length)
     { mostrarLayer(submenuActual, false); }
    submenuActual = prefijo+submenu;
    if(document.getElementById(submenuActual) != null)
     {
      mostrarLayer(submenuActual, true);
      mostrarLayer('banner_izquierda', false);
     }
    else
     {
      mostrarLayer('banner_izquierda', true);
     }
   }
 }

function mostrarLayer(obj, mostrar)
 {
  if(typeof obj == 'string')
   { obj = document.getElementById(obj); }
  if (obj==null)
   { return; }
  obj.style.display = mostrar ? 'block' : 'none';
 }

function cargarContenido(clave)
 {
  var archivo = 'submenu.xml?id='+clave;
  if(typeof contenedor == 'object')
   {
    if(contenedor == document.getElementById('item'+clave))
     {
      if(contenedor.childNodes[1].style.display == 'none')
       { mostrarLayer(contenedor.childNodes[1], true); }
      else
       { mostrarLayer(contenedor.childNodes[1], false); }
      return;
     }
    limpiarLista(contenedor);
   }

  contenedor = document.getElementById('item'+clave);
//  contenedor.childNodes[1].style.display = 'block';
  if (window.XMLHttpRequest)
   {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = processReqChange;
    xmlhttp.open("GET", archivo, true);
    xmlhttp.send(null);
   }
  else if (window.ActiveXObject)
   {
    isIE = true;
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp)
     {
      xmlhttp.onreadystatechange = processReqChange;
      xmlhttp.open("GET", archivo, true);
      xmlhttp.send();
     }
   }
 }

function processReqChange()
 {
  if (xmlhttp.readyState == 4)
   {
    if (xmlhttp.status == 200)
     { 
      limpiarLista(contenedor);
      construirLista(contenedor);
     }
    else
     { alert('Error:\n' + xmlhttp.statusText); }
   }
 }

function limpiarLista(contenedor)
 {
  while(contenedor.childNodes[1])
   { contenedor.removeChild(contenedor.childNodes[1]); }
 }

function construirLista(contenedor)
 {
  var subitemsLyr = document.createElement('ul');
  contenedor.appendChild(subitemsLyr);
  var subitems = xmlhttp.responseXML.getElementsByTagName("subitem");
  //if(subitems.length >= 1) subitemsLyr.parentNode.style.display = "block";
  for (var i = 0; i < subitems.length; i++)
   { agregarItem(subitemsLyr, subitems[i].attributes.getNamedItem("id").value, subitems[i].firstChild.nodeValue); }
 }

function agregarItem(lyr, valorId, valorTxt)
 {
  var li = document.createElement('li');
  var a = document.createElement('a');
  var texto = document.createTextNode(valorTxt);
  a.setAttribute('href', 'home?nota=contenidos/'+valorId+'&despliegue=principal2.php');
  a.setAttribute('target', 'ifcontenido');
  a.setAttribute('id', 'submenu'+valorId);
  a.onclick = function()
   { submenuActivo(valorId); }
  a.appendChild(texto);
  li.appendChild(a);
  lyr.appendChild(li);
 }

function menuActivo(seccion)
 {
  document.getElementById('peliculatarjetas').style.visibility = 'hidden';
  document.getElementById('ifcontenido').style.display = '';

  if(seccion != seccionActual)
   {
    if(seccionActual.length > 1) document.getElementById(seccionActual).className = '';
    seleccionado = document.getElementById('menu'+seccion);
    if(seleccionado != null)
     {
      seccionActual = 'menu'+seccion;
      document.getElementById(seccionActual).className = 'activo';
     }
    mostrarSubmenu(seccion);
   }
 }

function submenuActivo(seccion)
 {
  if(seccion != subseccionActual)
   {
    if(subseccionActual.length > 1)
     { if(document.getElementById(subseccionActual)) document.getElementById(subseccionActual).className = ''; }
    subseccionActual = 'submenu'+seccion;
    var subseccionActualEl = document.getElementById(subseccionActual);
    if(subseccionActualEl != null) document.getElementById(subseccionActual).className = 'activo';
   }
 }

var seccionActual = '';
var subseccionActual = '';

function tarjetas(categoria, mostrar)
 {
  var divtarjetas = document.getElementById('peliculatarjetas');
  var ifcontenido = document.getElementById('ifcontenido');
  divtarjetas.style.visibility = mostrar ? 'visible' : 'hidden'; // none
  ifcontenido.style.display = mostrar ? 'none' : '';
/*
  if(mostrar == true && divtarjetas.childNodes.length == 0)
   {
    var mvtarjetas = new SWFObject("./tarjetas.swf", "carrito", "459", "550", "8", "#ffffff");
    mvtarjetas.write("peliculatarjetas");
    divtarjetas.style.display = '';
   }
*/
  window.document.carrito.seleccionarProducto(categoria);
 }