var IDIOMA = {

	inicio : function() {
		// asignamos un TITLE al STRONG en funcion del idioma
		var mensaje = (document.body.className == 'castellano')?'Cambiar de idiomaa':'Change language';
		//document.getElementById('lista-idiomas').getElementsByTagName('strong')[0].title = mensaje;
		document.getElementById('lista-idiomas').getElementsByTagName('strong')[0].title = literal["textos"][7];
		// le asignamos su ONCLICK, que muestra u oculta la opción para cambiar de idioma
		document.getElementById('lista-idiomas').getElementsByTagName('strong')[0].onclick = function() {
			if (document.getElementById('lista-idiomas').className != 'activa')
				document.getElementById('lista-idiomas').className = 'activa';
			else
				document.getElementById('lista-idiomas').className = '';
		}
	}
	
}




var COMPARTIR = {

	inicio : function() { 
		// añadimos el ONCLICK al botón de compartir
		document.getElementById('boton-compartir').onclick = function() {
			// comprobamos si está activo o no y lo mostramos u ocultamos según corresponda
			if (document.getElementById('compartir').className != 'activo') {
				document.getElementById('compartir').className = 'activo';
				// si hay que activarlo, llamamos a la función que gestiona el portapapeles
				COMPARTIR.portapapeles();
			} else {
				document.getElementById('compartir').className = '';
			}
		}
		// añadimos el ONCLICK al botón de cerrar
		var botones = document.getElementById('compartir').getElementsByTagName('button');
		botones[botones.length-1].onclick = function() {
			document.getElementById('compartir').className = '';
		}
	},
	
	portapapeles : function(cual) { 
		// creamos la película Flash que utilizaremos para copiar el contenido de los campos de texto
		ZeroClipboard.setMoviePath( '/design/common/swf/ZeroClipboard.swf');
		// creamos el clip que asociaremos a cada campo
		var clip_anyadir = new ZeroClipboard.Client();
		// lo asociamos al campo de "Añade a tu página"
		clip_anyadir.setText(document.getElementById('codigo-video').value);
		// le asignamos sus parámetros
		clip_anyadir.setHandCursor(true);
		clip_anyadir.setCSSEffects(false);
		// lo vinculamos a su botón correspondiente
		clip_anyadir.glue(document.getElementById('compartir').getElementsByTagName('button')[1]);
		// repetimos la acción para el campo de "Copia la URL de la página"
		var clip_copiar = new ZeroClipboard.Client();
		clip_copiar.setText(document.getElementById('url-video').value);
		clip_copiar.setHandCursor(true);
		clip_copiar.setCSSEffects(false);
		clip_copiar.glue(document.getElementById('compartir').getElementsByTagName('button')[2]);
		
	}
	
}


var ENVIARCOMPARTIR = {
//Envia los datos
	inicio : function() {
		// recogemos todas las pestañas
		var boton = document.getElementById('enviarCompartir');
		// le asignamos su ONCLICK
		
		boton.onclick  = function() {
			
			ENVIARCOMPARTIR.actualizaForm(this);
			return false;
		}	
	},
	actualizaForm : function(cual) {
			
		var tuNombre = document.getElementById('tu-nombre').value;
		var tuCorreo = document.getElementById('tu-correo').value;
		var suNombre = document.getElementById('su-nombre').value;
		var suCorreo = document.getElementById('su-correo').value;
		var Mensaje = document.getElementById('mensaje').value;
		var Url = document.getElementById('url').value;

		//Pasamos los datos en un array
		//var datosCompartir = new Array('"' + tuNombre + '"', '"' + tuCorreo + '"' , '"' + suNombre + '"', '"' + suCorreo + '"' );
		var datosCompartir = new Array(tuNombre , tuCorreo , suNombre , suCorreo, Mensaje, Url);
		//var datosCompartir = tuNombre;
		
		
		// comprobamos si es un enlace de "anterior" o "siguiente" para corregir la página seleccionada
		
		// hacemos la llamada de AJAX
		var instanciaCompartir;
		AJAX.cargaDatos('compartir',instanciaCompartir,datosCompartir);
	}
		
}
	
	
	

var LISTAS = {

	inicio : function() {
		// recogemos todas las pestañas
		var pestanyas = document.getElementById('listas').getElementsByTagName('h5');
		// le asignamos su ONCLICK
		for (i=0;i<pestanyas.length;i++) {
			pestanyas[i].onclick  = function() {
				document.getElementById('listas').className = this.parentNode.id.split('-')[1];
			}	
		}
	}
	
}





var CARRUSEL = {

	inicio : function() {
		// recogemos todos los enlaces del carrusel
		var enlaces = document.getElementById('ie-en-fotos').getElementsByTagName('ul')[0].getElementsByTagName('a');
		// los recorremos añadiéndoles su ONCLICK
		for (i=0;i<enlaces.length;i++) {
			enlaces[i].onclick = function() {
				CARRUSEL.actualiza(this);
				return false;
			}
		}		
	},
	
	actualiza : function(cual) {
		// recogemos la página actual del carrusel
		var actual = parseInt(document.getElementById('ie-en-fotos').getElementsByTagName('ul')[0].getElementsByTagName('strong')[0].getElementsByTagName('span')[0].innerHTML);
		// recogemos la que ha seleccionado el usuario
		//var seleccionada = cual.href.split('/')[cual.href.split('/').length-2];
		var seleccionada = cual.rel;
		// comprobamos si es un enlace de "anterior" o "siguiente" para corregir la página seleccionada
		if (isNaN(seleccionada)) {
			switch (seleccionada) {
				case 'prev'	:	seleccionada = (actual > 1)?actual-1:5;
									break;	
				case 'next':	seleccionada = (actual < 5)?actual+1:1;
									break;
			}
		}
		// hacemos la llamada de AJAX
		var instanciaCarrusel;
		AJAX.cargaDatos('carrusel',instanciaCarrusel,seleccionada);
	},
	
	actualizaPaginacion : function(pagina,carpeta) {
		// recogemos la página seleccionada
		var estrong = document.getElementById('ie-en-fotos').getElementsByTagName('ul')[0].getElementsByTagName('strong')[0];
		// recogemos la página actual
		var actual = parseInt(estrong.getElementsByTagName('span')[0].innerHTML);
		// creamos un enlace
		var enlace = document.createElement('a');
		// la asignamos sus parámetros
		enlace.title = 'Ir a la página ' + actual + ' de fotos';
		//enlace.href = '/' + carpeta + '/ie-en-fotos/' + actual + '/';
		enlace.href = '#';
		enlace.rel = actual;
		enlace.innerHTML = '<span>' + actual + '</span>';
		// le asignamos su ONCLICK
		enlace.onclick = function() {
			CARRUSEL.actualiza(this);
			return false;
		}
		// reemplazamos el STRONG por el enlace
		estrong.parentNode.replaceChild(enlace,estrong);
		// recogemos el elemento seleccionado
		var seleccionada = document.getElementById('ie-en-fotos').getElementsByTagName('ol')[1].getElementsByTagName('li')[pagina-1];
		// creamos un STRONG
		estrong = document.createElement('strong');
		// lo rellenamos
		estrong.innerHTML = '<span>' + pagina + '</span>';
		// vaciamos el elemento seleccionado
		seleccionada.innerHTML = '';
		// le añadimos el STRONG
		seleccionada.appendChild(estrong);
	}
	
}



/*
var SUGERENCIAS = {

	inicio : function() {
		// recogemos todos los elementos
		var eleis = document.getElementById('sugerencias').getElementsByTagName('li');
		// los recorremos
		for (i=0;i<eleis.length;i++) {
			// comprobamos si es uno que abre una ventana nueva
			if (eleis[i].className == 'ventana-nueva') {
				// le añadimos su ONCLICK
				eleis[i].getElementsByTagName('a')[0].onclick = function() {
					GENERALES.ventanaNueva(200,200,'hhtp://www.todociclismo.com');
					return false;
				}	
			}
		}		
	}
	
}
*/




addLoadEvent(function() {
	VALORACION.inicio(document.getElementById('video').getElementsByTagName('div')[document.getElementById('video').getElementsByTagName('div').length-1]);
});


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

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

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


addLoadEvent(function() {
	FAVORITOS.inicio(document.getElementById('video').getElementsByTagName('button')[document.getElementById('video').getElementsByTagName('button').length-1]);
});

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

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

/*addLoadEvent(function() {
	SUGERENCIAS.inicio();
});*/
