var GaleriaMultimedia = Class.create({

	initialize: function(elementos) {
		this.mostrando = -10;
		if (elementos) {
			this.elementos = elementos;
		}
	},
	
	initInterfaz: function() {
		$("miniaturas_slide").select("li").invoke("observe", "click", this.mostrarClick.bindAsEventListener(this));
		var botones = $("descripcionElementoGaleria").previous().select("span")[1].select("img");
		botones[0].observe("click", this.avanzar.bind(this, -1));
		botones[1].observe("click", this.avanzar.bind(this, +1));
		
		$(botones[2], $("carga").next()).invoke("observe", "click", this.pantallaCompleta.bind(this));
	},
	
	mostrarClick: function(e) {
		var li = e.findElement("li");
		var pos = li.previousSiblings().size();
		this.mostrar(pos);
	},
	
	avanzar: function(valor) {
		var pos = this.mostrando + valor;
		if (pos < 0) {
			pos = this.elementos.length + valor;
		}
		pos = pos % this.elementos.length;
		this.mostrar(pos);
	},
	
	pantallaCompleta: function() {
		return;
		if (this.mostrando >= 0 && this.mostrando < this.elementos.length) {
			var elemento = this.elementos[this.mostrando];
			
			if (elemento.tipo == "foto") {
				open(elemento.urlOriginal, "", "");
			} else {
				alert("No disponible");
			}
		}
	},
	
	mostrar: function(pos) {
		if (pos >= 0 && pos < this.elementos.length) {
			if (this.mostrando != pos) {
				// Marcamos como actual la miniatura				
				var minis = $("miniaturas_slide").select("li");
				minis.invoke("removeClassName", "actual")
				minis[pos].addClassName("actual");
				
				// la centramos
				var desplazamiento = 0;
				if (pos >= 3) {
					desplazamiento = -((80 + 5) * (pos - 2));
				}
				$("miniaturas_slide").setStyle({
					marginLeft: desplazamiento + "px"
				});
				
				this.mostrando = pos;
				var elemento = this.elementos[pos];
				
				var tipo = elemento.tipo;
				$("reproductorGaleria").childElements().invoke("hide");
				var capa = $("multimedia_tipo_" + tipo).show();

				var botonPantallaCompleta = $$(".botoneraTextoAsociado")[0].select("img[title='Pantalla Completa']")[0];
				botonPantallaCompleta.setStyle({
					visibility: "hidden"
				});

				// Paramos la reproduccion de audio
				player.pause();
				player.disable();
				
				// Borramos el player de video
				this.escribirVideo("");
				
				switch (tipo) {
					case "video":
						this.escribirVideo(elemento);
						break;
					case "audio":
						// Reiniciamos la interfaz
						var botonPausa = $$(".datosReproduccionAudio .botonPause")[0];
						if (botonPausa) {
							botonPausa.className = "botonPlay";
						}
						$$(".icoScrollReproductor img")[0].setStyle({
							marginLeft: 0
						});
						var capaTiempo = $("tiempoRepro");
						capaTiempo.innerHTML = "00:00 / 00:00";
						
						player.url = "";
						player.setPosition(0);
						break;
					case "foto":
						botonPantallaCompleta.setStyle({
							visibility: "visible"
						});
						$("carga").className = "cargaGaleriaOn";
						var img = capa.select(".detalleGaleria")[0]
						img.hide();
						img.src = elemento.url;
						img.onload = function() {
							$("carga").className = "cargaGaleriaOff";
							$(this).show();
						}
						
						break;
				}
				if (elemento.pie.length == 0) {
					$("descripcionElementoGaleria").update("Sin descripción");
				} else {
					$("descripcionElementoGaleria").update(elemento.pie);
				}
			}
		}
	},
	
	escribirVideo: function(video, data) {
		var text = "";
		if (video) {
			if (!data) {
				data = {
					url: video.url,
					ancho: 480,
					alto: 270,
					prev: video.urlPrevia
				};
			}
			var ajp = new AjaxPages();
			var base = "/" + location.href.split("/")[3] + "/";
			data.urlBase = base;
			ajp.load(base + "js/player.tpl");
			var plantilla = ajp.getProcessor();
			text = plantilla(data);
		}
		
		$("contenedorReproductor").update(text);
	}
});



