var reproductor = null;

function cambiaSeccion(seccion, li) {
  seccion = unescape(seccion);
  if(reproductor) {
    var sec = seccion.substring(1);
    switch(sec) {
      case '?seccion=Audios':
      case '?seccion=Vídeos':
      case '?seccion=Animaciones':
      case '?seccion=Fotos':
      case '?seccion=Móviles':
        sec = sec.substring(9);
      case 'Destacado':
        var activo = $$('#menuPrincipal .activo')[0];
        if(activo == null) {
          activo = $$('#menuPrincipal .activa')[0];
        }

        if(activo.id != "pestana" + sec) {
          activo.className = '';
          $("pestana" + sec).addClass('activo');
          reproductor.cambiaSeccion(sec);
        }

        var abajo = document.getElementById('abajo');
        if(abajo != null) {
          abajo.style.display = sec == 'Destacado' ? 'block' : 'none';
        }

        return false;
    }
  }

  if(li) {
    document.location = seccion;
  } else {
    return true;
  }
}

function colocaCosas() {
  var ellos = document.getElementById('ellos');
  if(ellos == null) {
    ellos = document.createElement('img');
    ellos.src = '/resources/i/fotoellos.jpg';
    ellos.width = 175;
    ellos.height = 120;
    ellos.id = 'ellos';
  }

  document.getElementById('enlaces').appendChild(ellos);
  ellos.style.position = 'absolute';
  ellos.style.left = $('enlaces').getPosition().x + 'px';
  ellos.style.top = ($('enlaces').getPosition().y - 130) + 'px';
}

function inicializaMenu() {
  var sec = null;

  var trozos = window.location.search.substring(1).split('&');
  for(var i = 0, max = trozos.length; i < max; i++) {
    var trozos2 = trozos[i].split('=');
    if(trozos2[0] == 'seccion') {
      sec = unescape(trozos2[1]);
      break;
    }
  }

  if(sec == null) {
    var indice = 0;
    sec = unescape(window.location.pathname).substring(1).replace(/\+/g, " ");
  }

  if(sec.indexOf('/') != -1) {
    sec = sec.substring(0, sec.indexOf('/'));
  }

  var pestana = document.getElementById('pestana' + sec);
  if(pestana != null && pestana.id != 'pestanaDestacado') {
  	pestana.className = 'activo';
  	if(reproductor) {
  	  reproductor.cambiaSeccion(sec);
  	}
  } else {
    var titulo = $('titulo');
    if(titulo) {
      switch(sec) {
        case 'Error':
          $('titulo').style.backgroundImage = 'url(/resources/i/cabecera_error.jpg)';
          break;
        case 'Newsletter':
          break;
        default:
          pestana = $('pestanaDestacado');
          pestana.className = 'activo';
          $('titulo').style.backgroundImage = 'url(/resources/i/cabecera_portada.jpg)';
          break;
      }
    }
  }

  $('pestanaDestacado').onclick = null;
  $('pestanaDestacado').href = "/";
}

window.addEvent('load', colocaCosas);
window.addEvent('load', inicializaMenu);
window.addEvent('resize', colocaCosas);