var aImages = [
	{ file: "fachada.jpg",      txt: "Fachada"        },
	{ file: "recepcion.jpg",    txt: "Recepción"      },
	{ file: "tienda1.jpg",      txt: "Tienda"         },
	{ file: "tienda2.jpg",      txt: "Tienda"         },
	{ file: "pasillo.jpg",      txt: "Pasillo"        },
	{ file: "salaespera1.jpg",  txt: "Sala de espera" },
	{ file: "salaespera2.jpg",  txt: "Sala de espera" },
	{ file: "consulta1.jpg",    txt: "Consulta"       },
	{ file: "consulta2.jpg",    txt: "Consulta"       },
	{ file: "ecografo.jpg",     txt: "Ecógrafo"       },
	{ file: "quirofano.jpg",    txt: "Quirófano"      },
	{ file: "servicios.jpg",    txt: "Equipamiento"   },
	{ file: "jaula.jpg",        txt: "Jaula"          },
	{ file: "mesatrabajo.jpg",  txt: "Laboratorio"    },
	{ file: "peluqueria.jpg",   txt: "Peluquería"     },
	{ file: "radiografias.jpg", txt: "Rayos X"        }
];

var nCur    = 0;
var nLoaded = 0;
var oWrap   = null;
var oImg    = null;
var oText   = null;
var oPrev   = null;
var oNext   = null;
var nMaxW   = 0;
var nMaxH   = 0;

$(document).ready(function () {

	oWrap = $("#wrap");
	oImg  = $("#img");
	oPrev = $("#prev");
	oNext = $("#next");
	oText  = $("#text");
	oPrev.css({ opacity: 0 });
	oNext.css({ opacity: 0 });
	oText.css({ opacity: 0 });
	oWrap.hover(f_imgIn, f_imgOut);
	oPrev.click(function () {
		nCur--; if(nCur<0) nCur=nLoaded;
		f_refresh();
		return false;
	});
	oNext.click(function () {
		nCur++; if(nCur>nLoaded) nCur=0;
		f_refresh();
		return false;
	});

	for(img in aImages) {
		var oImage = aImages[img];
		var img = new Image();
		img = new Image();
		img.src = "pics/"+oImage.file;
		img.onload = f_loaded;
		oImage.img = img;
	}

});

function f_loaded() {
	nLoaded++;
	nMaxW = Math.max(nMaxW, this.width);
	nMaxH = Math.max(nMaxH, this.height);
	if(nLoaded==aImages.length) {
		$("#content").height(nMaxH);
		nLoaded--;
		f_ready();
	}
}

function f_ready() {
	f_refresh();
}

function f_refresh() {
	var img = aImages[nCur].img;
	var txt = aImages[nCur].txt;
	oImg.empty();
	oImg.append(img);
	oText.empty();
	oText.html(txt);
	oText.width(img.width-54);
	oWrap.width(img.width);
	oWrap.height(img.height);
}

function f_imgIn() {
	oPrev.css({ opacity: 1 });
	oNext.css({ opacity: 1 });
	oText.animate({ opacity: 0.5 });
}

function f_imgOut() {
	oPrev.css({ opacity: 0 });
	oNext.css({ opacity: 0 });
	oText.animate({ opacity: 0 });
}
