// System
$=function(i){return document.getElementById(i);};
// Menu
var showMenuOptions = function(e) {
	if(menu.active) {
		if(e.id != menu.active.id) {
	    menu.active.className = menu.active.id.replace('menu_', '');
	  } else {
			return;
		}
	}
	$('options').innerHTML = menu[e.id.replace('menu_', '')];
	menu.active = e;
	e.className = 'active';
};
// AJAX
var getXMLHttpRequest = function() {
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) { // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else { 
    alert('Tu navegador no soporta XMLHTTP (AJAX).\nActualiza tu navegador para poder apuntarte.');
    $('cargando').style.display = 'none';
    $('botonApuntarse').style.display = '';
    return false;
  }
}
var loadContent = function(file) {
  var xmlhttp = getXMLHttpRequest();
  if(!xmlhttp) return;
  
  xmlhttp.onreadystatechange = function()
  {
    if(xmlhttp.readyState == 4) {
      $('ContentBody').innerHTML = xmlhttp.responseText;
    }
  };
  xmlhttp.open('GET', file, true);
  $('ContentBody').innerHTML = '<img alt="" src="/img/cargando.gif" />';
  xmlhttp.send(null);
};
// Intro
var fade = function() {
  if(fx.o < 0) {
    clearInterval(fx.i);
    fx.e.parentNode.removeChild(fx.e);
    delete(fx);
    return;
  }
  fx.o -= 0.05
  fx.e.style.mozOpacity = fx.o;
  fx.e.style.opacity = fx.o;
  fx.e.style.filter = 'Alpha(opacity=' + (fx.o*100) + ');';
};
var fadeToBlack = function(e) {
  window.fx = {
    'e':e,
    'o': 1,
    'time': 2 * (1000), // Two seconds
    i: null
  };
  fx.i = setInterval(fade, fx.time/30); // Cinema framerate
  
  // Show hidden flash player
  setTimeout("$('player').style.display = '';", fx.time+1);
};

// Content
var apuntarse = function() {
  $('cargando').style.display = '';
  $('botonApuntarse').style.display = 'none';
  var	email = $('email').value;
  if(email.length < 5) {
    alert('Dirección de e-mail incorrecta');
    $('cargando').style.display = 'none';
    $('botonApuntarse').style.display = '';
    return;
  }
  var xmlhttp = getXMLHttpRequest();
  if(!xmlhttp) { return; } // No se puede hacer la petición AJAX
  
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
			switch(xmlhttp.responseText) {
				case '0':
					mensaje = 'Dirección de e-mail incorrecta';
					$('cargando').style.display = 'none';
        	        $('botonApuntarse').style.display = '';
				break;
				case '1':
					mensaje = 'Gracias por suscribirte a la lista de correo de Eternal Dream.';
					$('tablaApuntarse').style.display = 'none';
					$('divApuntarse').innerHTML = mensaje;
				break;
				case '2':
					mensaje = 'Ha ocurrido un error. Contacte con master@eternaldream.es';
				break;
			}
	    alert(mensaje);
    }
	}
  xmlhttp.open('GET', '/backend/suscribe.php?email=' + email, true);
  xmlhttp.send(null);
};
var getAlbumDownloads = function() {
  var xmlhttp = getXMLHttpRequest();
  if(!xmlhttp) { return; }
  
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
      $('ContadorDescargas').innerHTML = 'Descargas: ' + xmlhttp.responseText;
    }
	};
  xmlhttp.open('GET', '/backend/get_album_downloads.php', true);
  xmlhttp.send(null);
}

var initfs = [];
initfs.push(function() { document.body.className = ''; });
initfs.push(function() { fadeToBlack($('carga')); });
initfs.push(getAlbumDownloads);

// Load
var init = function() {
  for(i=0; i<initfs.length; i++) {
		initfs[i]();
	}
};
window.onload = init;