var ciclo_bloquear = false;
var mini_over = [];

var mini_activa = [];
var foto_activa = [];

var anim_foto = [];
var anim_tmr = [];
var anim_t = 70;

var auto = true;
var auto_rand = false;
var auto_start = [];
var auto_tmr = [];
var auto_t = 4000;

function InicializaGaleria()
{
	var elm;

	for(var g = 0; g != fotos.length; ++g) {

		mini_activa[g] = null;
		foto_activa[g] = null;

		anim_foto[g] = null;
		anim_tmr[g] = null;
		auto_tmr[g] = null;
		if(auto_rand) auto_start[g] = Math.floor(fotos[g].length * Math.random());
		else auto_start[g] = 1;

		for(var n = 1; n != fotos[g].length; ++n) {
			elm = document.getElementById('fot' + g + 'x' + n);
			setOpacity(elm, 0);
			elm.g = g;
			elm.n = n;
			elm.bot = document.getElementById('bot' + g + 'x' + n);
			setOpacity(elm.bot, 0.5);
			elm.bot.g = g;
			elm.bot.n = n;
			if(fotos[g][0][0] == 'foto') {
				elm.bot.img = elm.bot.getElementsByTagName('img');
				elm.bot.img[0].src = fotos[g][n][0];
			}
			elm.img = document.getElementById('img' + g + 'x' + n);
			elm.img.g = g;
			elm.img.n = n;
			elm.img.src = '';
			elm.img.onload = FotoCargada;
			elm.img.onerror = Error;
			elm.img.onabort = Error;
			elm.img.src = fotos[g][n][0];
		}
	}
}
window_onload[window_onload.length] = 'InicializaGaleria()';

function FotoCargada()
{
	if(!fotos[this.g][this.n][3]) {
		fotos[this.g][this.n][3] = true;
		var elm = document.getElementById('bot' + this.g + 'x' + this.n);
		elm.AutoMini = AutoMini;
		elm.ActivaMini = ActivaMini;
		elm.onmouseover = OverMini;
		elm.onmouseout = OutMini;

		if(this.n == auto_start[this.g] && !mini_over[this.g]) {
			document.getElementById('bot' + this.g + 'x' + auto_start[this.g]).AutoMini();
		}
	}
};
function Error()
{
	window.status += '[' + this.n + ']';
};
function OverMini()
{
	if(auto_tmr[this.g]) {
		clearTimeout(auto_tmr[this.g]);
		auto_tmr[this.g] = null;
	}

	if(!ciclo_bloquear) {
		ciclo_bloquear = true;

		mini_over[this.g] = true;

		if(mini_activa[this.g].n != this.n) this.ActivaMini();

		ciclo_bloquear = false;
	}
};
function OutMini()
{
	if(auto_tmr[this.g]) {
		clearTimeout(auto_tmr[this.g]);
		auto_tmr[this.g] = null;
	}

	if(!ciclo_bloquear) {
		ciclo_bloquear = true;

		var n = this.n + 1;
		if(n == fotos[this.g].length) n = 1;
		if(auto) {
			auto_tmr[this.g] = setTimeout("var elm = document.getElementById('bot" + this.g + 'x' + n + "'); if(elm && elm.AutoMini) elm.AutoMini();", auto_t);
		}

		mini_over[this.g] = false;

		ciclo_bloquear = false;
	}
};
function AutoMini()
{
	auto_tmr[this.g] = null;

	if(!mini_over[this.g]) this.ActivaMini();
}
function ActivaMini()
{
	var elm = document.getElementById('fot' + this.g + 'x' + this.n);

	if(elm && elm != foto_activa[this.g] && elm != anim_foto[this.g]) {
		if(anim_tmr[this.g]) {
			clearTimeout(anim_tmr[this.g]);
			anim_tmr[this.g] = null;

			setOpacity(anim_foto[this.g], 1);
			if(foto_activa[this.g]) {
				setOpacity(foto_activa[this.g], 0);
				setStyle(foto_activa[this.g], 'zIndex', '1');
			}
			setStyle(anim_foto[this.g], 'zIndex', '2');
			foto_activa[this.g] = anim_foto[this.g];
			anim_foto[this.g] = null;
		}

		if(mini_activa[this.g]) {
			setOpacity(mini_activa[this.g], 0.5);
			mini_activa[this.g].className = 'off';
		}
		mini_activa[this.g] = this;
		setOpacity(mini_activa[this.g], 1);
		mini_activa[this.g].className = 'on';

		anim_foto[this.g] = elm;
		setStyle(anim_foto[this.g], 'zIndex', '3');
		anim_tmr[this.g] = setTimeout('AnimaFoto(' + this.g + ',' + this.n + ', 0)', anim_t);
	}
};
function AnimaFoto(g, n, alfa)
{
	anim_tmr[g] = null;

	if(anim_foto[g]) {
		setOpacity(anim_foto[g], alfa);
		var tmp = 0.333 * (1 - alfa);
		if(tmp <= 0.01) {
			setOpacity(anim_foto[g], 1);
			if(foto_activa[g]) {
				setOpacity(foto_activa[g], 0);
				setStyle(foto_activa[g], 'zIndex', '1');
			}
			setStyle(anim_foto[g], 'zIndex', '2');
			foto_activa[g] = anim_foto[g];
			anim_foto[g] = null;

			++n;
			if(n == fotos[g].length) n = 1;
			if(!mini_over[g] && (auto && !auto_tmr[g])) auto_tmr[g] = setTimeout("document.getElementById('bot" + g + 'x' + n + "').AutoMini()", auto_t);
		} else {
			alfa += tmp;
			anim_tmr[g] = setTimeout('AnimaFoto(' + g + ', ' + n + ', ' + alfa + ')', anim_t);
		}
	}
};

