// ABA, 31/05/2004: Visor de imágenes
// v1.0: vía diapositivas

var w_imagen = null;

function visor(nombre, tit) {
	this.identificador = nombre;
	this.titulo = tit;
	this.imagenes = new Array();
	this.diapositivas = new Array();
	this.titulos = new Array();
	this.mensajes = new Array();
	this.precargadas = new Array();
	this.precargar = -1;
	this.alto = 0;
	this.ancho = 0;
	this.actual = 0;
	this.idVisor = "imgvisor";
	this.idPosicion = "posicion";
	this.idPie = "pie"
	this.imagen = function (imagen, diapositiva, titimg, msg) {
		var i = this.imagenes.length;
		this.imagenes[i] = imagen;
		this.diapositivas[i] = diapositiva;
		if (titimg.length > 0)
			this.titulos[i] = titimg;
		else
			this.titulos[i] = this.titulo;
		this.mensajes[i] = msg;
		if (i < this.precargar) {
			this.precargadas[i] = new Image();
			this.precargadas[i].src = imagen;
		}
	}
	this.escribir_diapositivas = function(num_cols) {
		document.open();
		document.write('<h1 align="center">');
		document.write(this.titulo);
		document.write('</h1>')
		var i, c, n, p1, p2, t;
		n = this.imagenes.length;
		if (n > 0) {
			t = '<p> ' + n + ' imágenes</p>';
			t += '<table border="2" id="' + this.identificador + '" cellspacing="1" width="100%" cellpadding="2">';
			p2 = Math.floor(100 / num_cols);
			p1 = 100 - (num_cols - 1) * p2;
			for(i = 0, c = 1; i < n; ++i) {
				if (c == 1) t += '<tr>';
				t += '<td width="' + (c == 1 ? p1 : p2) + '%"><p align="center">';
				t += '<img border="0" src="' + this.diapositivas[i] + '"' +
					'onClick="ventana_imagen(\'' + this.titulos[i] + '\',\'' + this.imagenes[i] + '\' )">';
				if (this.mensajes[i].length > 0) t += '<br>' + this.mensajes[i] 
				t += '</td>';
				++c;
				if (c > num_cols) {
					t += '</tr>';
					c = 1;
				}
			}
			if (c > 1) {
				while (c <= num_cols) {
					t += '<td width="' + (c == 1 ? p1 : p2) + '%"><p align="center">';
					t += '</td>';
					++c;
				}
			}
		}
		document.write(t);
		document.close();
	}
	this.mostrar = function(i) {
		var cimg;
		this.actual = i;
		cimg = document.getElementById(this.idVisor);
		cimg.style.visibility="hidden";
		cimg.src = this.imagenes[i];
		if (cimg.filters) {
			cimg.filters.item(0).Apply();
			cimg.filters.item(0).Transition = 23;	// Random
			cimg.filters.item(0).Play(3.0);
		}
		cimg.style.visibility="inherit";		
		document.getElementById(this.idPie).innerHTML = this.mensajes[i];
		document.getElementById(this.idPosicion).innerHTML = 'Nº ' + (i + 1) + ' de ' + (this.imagenes.length); 
	}	
	this.primera = function() {
		this.mostrar(0);
	}
	this.siguiente = function() {
		if (this.actual < this.imagenes.length - 1)
			this.mostrar(this.actual + 1);
		else
			this.mostrar(0);
	}
	this.anterior = function() {
		if (this.actual > 0)
			this.mostrar(this.actual - 1);
		else
			this.mostrar(this.imagenes.length - 1);
	}
	this.ultima = function() {
		this.mostrar(this.imagenes.length - 1);
	}
	this.automatico = function() {
		// Tomar datos del formulario. Si automatico, setTimeout("javascript:v.siguiente()", segundos * 1000);
		var autom = document.forms.fvisor.automatico.checked;
		var segs = parseInt(document.forms.fvisor.segundos.value, 10);
		if (autom && segs > 2) {
			setTimeout("v.siguiente();v.automatico()", segs * 1000);
			window.status = 'Siguiente imagen en ' + segs + ' segundos.';
		} else {
			clearTimeout(0);
			window.status = 'Utiliza los botones para desplazarte por las imágenes.';
		}
	}
}


function cerrar_ventana()
{
	w_imagen.close();
	w_imagen = null;
}

function ventana_imagen(tit, imag)
{
	var s, i, ancho, alto;
	
	i = new Image();
	i.src = imag;
	ancho = i.width + 30;
	alto = i.height + 80;
	w_imagen = window.open('', 'Imagen', 'toolbar=no,location=no,directories=no,width='+ancho+', height='+alto+',resizable=yes');
	w_imagen.document.open();
	s = '<html><title>' + tit + '</title>';
	s += '<body bgcolor="#000066"><img src="' + imag + '" alt=" ' + imag + ' no disponible"><br>';
	s += '<form><p align="center"><input type="button" name="cerrar" value="Cerrar" onclick="window.close()"></form>';
	s += '</body></html>';
	w_imagen.document.write(s);
	w_imagen.document.close();
	delete(i);
}

