// JavaScript Document
// arrImgs[pos] =  {srcMini:, srcGrande:, alt:, epigrafe:};
//orientacion ha(horiz-arriba), vd (vert-der), vi(vert-izq)

Galeria = function(idContenedor, arrImgs, orientacion, srcImgCerrar, xDifFirefox, yDifFirefox, wPagina){
	//
	this.isIE = false;
	this.isMz = false;
	var nav = navigator.userAgent.toLowerCase();
	if(nav.indexOf('msie') != -1) this.isIE = true;
	else if(nav.indexOf('gecko') != -1) this.isMz = true;
	
	//
	this.xDifFirefox = 0;
	if(xDifFirefox) this.xDifFirefox = xDifFirefox;
	this.yDifFirefox = 0;
	if(yDifFirefox) this.yDifFirefox = yDifFirefox;
	this.wPagina = 0;
	if(wPagina) this.wPagina = wPagina;
	//
	this.divCont = document.getElementById(idContenedor);
	this.orientacion = orientacion;
	
	//DIV PREVISUALIZAR
	this.divPrev = document.createElement('div');
	this.divCont.appendChild(this.divPrev);
	var aux = this.divPrev.style;
	aux.position = "absolute";
	aux.top = 0;
	aux.left = 0;
	aux.zIndex = 2;
	aux.overflow = "hidden";
	aux.height = 0;
	aux.visibility = "hidden";
	aux.textAlign = "center";
	aux.clear = "both";
	
	//IMAGEN CERRAR
	this.imgCerrar = document.createElement('img');
	this.imgCerrar.src = srcImgCerrar;
	var Obj = this;
	this.imgCerrar.onclick = function(){ Obj.cerrarPrevisualizacion(); };
	this.divPrev.appendChild(this.imgCerrar);
	aux = this.imgCerrar.style;
	aux.position = "absolute";
	aux.styleFloat = "right";
	aux.cursor = "pointer";
	aux.zIndex = 1;
	aux.border = 0;
	this.imgCerrar.onclick = function(){ Obj.cerrarPrevisualizacion(); };
	
	//DIV EPIGRAFE
	this.divEpigrafe = document.createElement('div');
	this.divPrev.appendChild(this.divEpigrafe);
	aux = this.divEpigrafe.style;
	aux.backgroundColor = "#FFFFFF";
	aux.margin = "2px";
	aux.padding  = "2px";
	aux.textAlign = "left";
	aux.clear = "both";
	
	//
	this.divImgs = document.createElement('div');
	this.divCont.appendChild(this.divImgs);
	//
	this.crearImagenes(this.divImgs, arrImgs,0);
	
	//
	if(orientacion=='ha' || orientacion=='hb'){
		this.divPrev.style.width = (this.divCont.offsetWidth)+"px";
		this.divEpigrafe.style.width = (this.divCont.offsetWidth-10)+"px";
	}
	else this.divPrev.style.height = (this.divCont.offsetHeight)+"px";
	
	//
	this.imgPrev = null;
	this.imgOnPrev = null
}
Galeria.prototype.cerrarPrevisualizacion = function(){
	this.divPrev.style.visibility = 'hidden';
	if(this.orientacion=='ha' || this.orientacion=='hb') this.divPrev.style.height = 0;
	else this.divPrev.style.width = 0;
}
Galeria.prototype.previsualizar = function(){
	var Clase = this;
	//
	this.imgPrev.onload = '';
	//eliminamos la imagen
	if(this.imgOnPrev) this.divPrev.removeChild(this.imgOnPrev);
	//
	if(this.orientacion=='ha' || this.orientacion=='hb') this.divPrev.style.height = 0;
	else this.divPrev.style.width = 0;
	
	this.divPrev.style.visibility = 'visible';
	//insertamos la nueva imagen
	this.divPrev.insertBefore(this.imgPrev,this.divEpigrafe);
	//pasamos la imagen
	this.imgOnPrev = this.imgPrev;
	//
	this.imgCerrar.style.top = 3+"px";
	this.imgCerrar.style.left = (this.divPrev.offsetWidth-this.imgCerrar.offsetWidth-3)+"px";
	//
	if(this.orientacion=='ha' || this.orientacion=='hb'){
		var dif = 0;
		if(this.isMz) dif = this.xDifFirefox;
		if(this.orientacion=='ha'){
			this.divPrev.style.top = (OffsetTopTotal(this.divCont)-this.divPrev.scrollHeight-dif)+"px";
			this.divPrev.style.height = this.divPrev.scrollHeight+"px";
		}
		else{
			this.divPrev.style.top = (OffsetTopTotal(this.divCont)+this.divCont.offsetHeight-dif)+"px";
			this.divPrev.style.height = (this.divPrev.scrollHeight+5)+"px";
		}
		dif = 0;
		if(this.isMz){
			var o = getBodyDims();
			dif = ((o.w - this.wPagina) / 2) + this.yDifFirefox;
			//alert(o.w +" - "+ this.wPagina + " - " + this.yDifFirefox);
			this.divPrev.style.left = (dif)+"px";
			//dif = this.yDifFirefox;
		}
		else this.divPrev.style.left = (OffsetLeftTotal(Clase.divCont)-dif)+"px";
	}
	else if(this.orientacion=='vd' || this.orientacion=='vi'){
		this.divPrev.style.width = (this.divPrev.scrollWidth)+"px";
		var dif = 0;
		if(this.isMz) dif = this.xDifFirefox;
		this.divPrev.style.top = (OffsetTopTotal(this.divCont)-dif)+"px";
		
		dif = 0;
		if(this.isMz) dif = this.yDifFirefox;
		if(this.orientacion=='vd') this.divPrev.style.left = (OffsetLeftTotal(Clase.divCont)+this.divCont.offsetWidth-dif)+"px";
		else this.divPrev.style.left = (OffsetLeftTotal(Clase.divCont)-this.divPrev.offsetWidth-dif)+"px";
		
		dif = 0;
		if(this.isMz) dif = 3;
		this.divEpigrafe.style.height = (this.divPrev.offsetHeight-this.imgPrev.offsetHeight-15+dif)+"px";
	}
}
Galeria.prototype.cargarImgGrande = function(o){
	var Clase = this;
	
	this.imgPrev = new Image();
	var aux = this.imgPrev.style;
	aux.styleFloat = "none";
	aux.border = 0;
	aux.margin = "2px";
	aux.borderColor = "";
	aux.cursor = "default";
	aux.clear = "both";
	this.imgPrev.onload = function(){
		if(o.alt) Clase.imgPrev.alt = o.alt;
		if(o.epigrafe) Clase.divEpigrafe.innerHTML = o.epigrafe;
		else Clase.divEpigrafe.innerHTML = "";
		//
		Clase.previsualizar();
	}
	this.imgPrev.align = "center";
	this.imgPrev.src = o.srcGrande;
}
Galeria.prototype.reportarError = function(msg){
	alert(msg);
}
Galeria.prototype.crearImagenes = function(contenedor, aImgs, pos){
	var Clase = this;
	var img = document.createElement('img');
	
	if(pos<aImgs.length && this.comprobarObjImg(aImgs[pos], pos)){
		img.onclick = function(){ Clase.cargarImgGrande(aImgs[pos]); };
		img.onload = function(){ contenedor.appendChild(img); Clase.crearImagenes(contenedor, aImgs, (pos+1));  };
		img.onerror = function(){ Clase.reportarError("La imagen "+aImgs[pos].srcMini+" no existe."); Clase.crearImagenes(contenedor, aImgs, (pos+1)); };
		img.src = aImgs[pos].srcMini;
	}
	else if(pos<aImgs.length) Clase.crearImagenes(contenedor, aImgs, (pos+1));
}
Galeria.prototype.comprobarObjImg = function(o, pos){
	if(o && o.srcMini && o.srcGrande) return true;
	else{
		this.reportarError("El objeto imagen de la posicion "+pos+" no existe");
		return false;
	}
}


function OffsetTopTotal(o){
	var total = 0;
	if(o.offsetTop) total += o.offsetTop
	if(o.parentNode) total += OffsetTopTotal(o.parentNode);
	return total;
}
function OffsetLeftTotal(o){
	var total = 0;
	if(o.offsetLeft) total += o.offsetLeft
	if(o.parentNode) total += OffsetLeftTotal(o.parentNode);
	return total;
}