var fotos_cargadas = 0;
var foto_activa = null;
var mini_activa = null;
var anim_foto = null;
var anim_tmr = null;
var anim_t = 200; //duracion del cambio entre imagenes (default: 50)
var auto_tmr = null;
var auto_t = 10000; //duracion de cada imagen (default: 3333)

function Inicializa()
{
	if(fotos.length != 0) {
		if(fotos_cargadas == 0) {
			setTimeout('Inicializa()', 1000);
		} else {
			var elm;

			for(i = 0; i != fotos.length; ++i) {
				elm = document.getElementById('foto' + i + 'IMG');
				if(i != 0) {
					elm.onload = FotoCargada;
					elm.onerror = Error;
					elm.onabort = Error;
					elm.src = fotos[i][0];
				}
			}
		}
	}
}

//Mod
window.onload = Inicializa;

function PrecargarFotos()
{
	var i, elm1, elm2, elm3;

	for(i = 0; i != fotos.length; ++i) {
		elm1 = document.getElementById('foto' + i + 'DIV');
		setOpacity(elm1, 0);

		elm2 = document.getElementById('mini' + i + 'DIV');
		elm2.num = i;
		elm2.onclick = function() {
			auto_tmr = setTimeout("document.getElementById('mini" + this.num + "DIV').onclick()", auto_t);
		};

		elm3 = document.getElementById('foto' + i + 'IMG');
		elm3.num = i;
	}
	if(fotos.length != 0) {
		// Cargar la primera foto antes que el resto de la página
		elm3 = document.getElementById('foto0IMG');
		elm3.onload = FotoCargada;
		elm3.onerror = Error;
		elm3.onabort = Error;
		elm3.src = fotos[0][0];
	} else {
		// Quitar el reloj
		setStyleById('reloj', 'visibility', 'hidden');
	}
};
function FotoCargada()
{
	if(!fotos[this.num][3]) {
		fotos[this.num][3] = true;
		var elm = document.getElementById('mini' + this.num + 'DIV');
		elm.className = 'mini';
		elm.onclick = PinchaMini;
		elm.onmouseover = OverMini;
		elm.onmouseout = OutMini;

		if(this.num == 0) {
			document.getElementById('mini0DIV').onclick();
		}

		if(++fotos_cargadas == fotos.length) {
			setStyleById('reloj', 'visibility', 'hidden');
		}
	}
};
function Error()
{
	window.status += '[' + this.num + ']';
};
function OverMini()
{
	if(this != mini_activa) this.className = 'miniover';
};
function OutMini()
{
	if(this != mini_activa) this.className = 'mini';
};
function PinchaMini()
{
	if(auto_tmr) {
		clearTimeout(auto_tmr);
		auto_tmr = null;
	}

	var elm = document.getElementById('foto' + this.num + 'DIV');
	if(elm && elm != foto_activa && elm != anim_foto) {
		if(anim_tmr) {
			clearTimeout(anim_tmr);
			anim_tmr = null;

			setOpacity(anim_foto, 1);
			if(foto_activa) {
				setOpacity(foto_activa, 0);
				setStyle(foto_activa, 'zIndex', '1');
			}
			setStyle(anim_foto, 'zIndex', '2');
			foto_activa = anim_foto;
			anim_foto = null;
		}

		if(mini_activa) mini_activa.className = 'mini';
		mini_activa = this;
		mini_activa.className = 'minion';

		anim_foto = elm;
		setStyle(anim_foto, 'zIndex', '3');
		AnimaFoto(this.num, 0);
	}
};
function AnimaFoto(num, alfa)
{
	anim_tmr = null;

	if(anim_foto) {
		setOpacity(anim_foto, alfa);
		var tmp = 0.333 * (1 - alfa);
		if(tmp <= 0.01) {
			setOpacity(anim_foto, 1);
			if(foto_activa) {
				setOpacity(foto_activa, 0);
				setStyle(foto_activa, 'zIndex', '1');
			}
			setStyle(anim_foto, 'zIndex', '2');
			foto_activa = anim_foto;
			anim_foto = null;

			var i = num + 1;
			if(i == fotos.length) i = 0;
			if(i != num) auto_tmr = setTimeout("document.getElementById('mini" + i + "DIV').onclick()", auto_t);
		} else {
			alfa += tmp;
			anim_tmr = setTimeout('AnimaFoto(' + num + ', ' + alfa + ')', anim_t);
		}
	}
};
