// scripts realizados a partir del código de Sergi Meseguer - www.zigotica.com


var FORMULARIOS = {
	
	inicio : function() {
		document.getElementById('contenedor-formularios').getElementsByTagName('form')[0].onsubmit = function() {
			return FORMULARIOS.valida(this);
		}
		document.getElementById('contenedor-formularios').getElementsByTagName('form')[1].onsubmit = function() {
			return FORMULARIOS.valida(this);
		}
	},
	
	valida : function(cual) {
		var valor = cual.getElementsByTagName('input')[0].value;
		if ((valor.indexOf('@') == -1) || (valor.indexOf('.') == -1)) {
			alert('Please enter a valid e-mail address');
		} else {
			if (cual.getElementsByTagName('fieldset')[0] == 'formulario-enviar-amigo')
				AJAX.cargaDatos(cual);
			else
				return true;
		}		
		return false;
	}
	
}




var EPISODIOS = {
	
	inicio : function() {
		document.getElementById('contenedor-episodios').className = 'cargado';
		var enlaces = document.getElementById('navegacion-episodios').getElementsByTagName('a');
		for (i=0;i<enlaces.length;i++) {
			enlaces[i].onclick = function() {
				EPISODIOS.muestra(this);
				return false;
			}	
		}
		var campos = document.getElementById('contenedor-formularios').getElementsByTagName('input');
		for (i=0;i<campos.length;i++) {
			campos[i].onfocus = function() {
				this.value = '';
			}	
		}
		EPISODIOS.ocultarTodos();
		var inicio = document.getElementById('navegacion-episodios').getElementsByTagName('strong')[0].innerHTML.split('-')[0];
		var final = document.getElementById('navegacion-episodios').getElementsByTagName('strong')[0].innerHTML.split('-')[1];
		EPISODIOS.mostrarIntervalo(inicio,final);
	},
	
	muestra : function(cual) {
		EPISODIOS.actualizarMenu(cual);
		var inicio = cual.innerHTML.split('-')[0];
		var final = cual.innerHTML.split('-')[1];
		EPISODIOS.ocultarTodos();
		EPISODIOS.mostrarIntervalo(inicio,final);
	},
	
	actualizarMenu : function(cual) {
		var actual = document.getElementById('navegacion-episodios').getElementsByTagName('strong')[0];
		var enlace = document.createElement('a');
		enlace.href = '#';
		enlace.title = 'See the episodes ' + actual.innerHTML;
		enlace.innerHTML = actual.innerHTML;
		enlace.onclick = function() {
			EPISODIOS.muestra(this);
			return false;
		}
		actual.parentNode.replaceChild(enlace,actual);
		var estrong = document.createElement('strong');
		estrong.innerHTML = cual.innerHTML;
		cual.parentNode.replaceChild(estrong,cual);
	},
	
	ocultarTodos : function() {
		var eleis = document.getElementById('contenidos-episodios').getElementsByTagName('li');
		for (i=0;i<eleis.length;i++) {
			if (navigator.appName.indexOf('Internet Explorer') == -1) {
				var myAnim = glow.anim.css(eleis[i], 0.5,
				{
					'opacity': {to: '0'}
				},
				{
					tween: glow.tweens.linear()
	  			}
				);
				myAnim.start();
			}
			eleis[i].style.display = 'none';
		}
	},
	
	mostrarIntervalo : function(inicio,final) {
		var eleis = document.getElementById('contenidos-episodios').getElementsByTagName('li');
		for (i=inicio-1;i<final;i++) {
			eleis[i].style.display = 'inline';
			if (navigator.appName.indexOf('Internet Explorer') == -1) {
				var myAnim = glow.anim.css(eleis[i], 0.5,
				{
					'opacity': {to: '1'}
				},
				{
					tween: glow.tweens.linear()
	  			}
				);
				myAnim.start();
			}
		}
	}
	
}




// añadido de eventos realizado como en http://simon.incutio.com/archive/2004/05/26/addLoadEvent

function addLoadEvent(fn) {
	var old = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	} else {
		window.onload = function() {
			old();
			fn();
		}
	}
}


addLoadEvent(function() {
	FORMULARIOS.inicio();
});

addLoadEvent(function() {
	EPISODIOS.inicio();
});






/***********************************************************
*********** FUNCIONES DE CARGA CON XMLHttpRequest **********
***********************************************************/


var instancia;

var AJAX = {
	
	cargaDatos : function(formulario) {
		instancia = false;
		if (window.XMLHttpRequest) {
			try {
				instancia = new XMLHttpRequest();
			} catch(e) {
				instancia = false;
			}
		} else if(window.ActiveXObject) {
			try {
				instancia = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					instancia = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					instancia = false;
				}
			}
		}
		if (instancia) {
			instancia.onreadystatechange = function() {
				AJAX.procesaCambios(formulario);
			}
			instancia.open(formulario.method, formulario.action, true);
			if (formulario.method == 'post')
				instancia.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			instancia.send(AJAX.construirParametros(formulario));
		} else {
			alert('Se ha producido un error al intentar crear el objeto');		
		}
	},
	
	construirParametros : function(cual) {
		var campos = cual.getElementsByTagName('input');
		var cadena = '';
		for (i=0;i<campos.length;i++) {
			cadena += campos[i].name + '=' + campos[i].value + '&';	
		}
		cadena = cadena.substr(0,cadena.length-1);
		return cadena;
	},
	
	procesaCambios : function(cual) {
		if (instancia.readyState == 4) {
			if ((instancia.status == 200) || (instancia.status == 304)) {
				var resultado = instancia.responseText.split('&')[0].split('=')[1];
				var mensaje = instancia.responseText.split('&')[1].split('=')[1];
				if (resultado == 'ok') {
					var parrafo = document.createElement('p');
					parrafo.innerHTML = mensaje;
					cual.getElementsByTagName('fieldset')[0].insertBefore(parrafo,cual.getElementsByTagName('label')[0]);
					cual.reset();
				}
			} else {
				alert('Se produjo un error al intentar recuperar los datos');
			}
		}
	}
	
}