// JavaScript Document
var arrSe = new Array();
arrSe['nota'] = "nota";
arrSe['producto'] = "hoteles";
arrSe['fichaAutos'] = "autos";
arrSe['fichaCruceros'] = "cruceros";
arrSe['fichaEventos'] = "eventos";
arrSe['fichaConsejos'] = "consejos";
arrSe['fichaNotas'] = "notas";
var Galeria = function(contenedor,contenedorGaleria,se,idElemento,archivoXML,dirImg,tipo,cantidad){
	var oCl = this;
	var cont = document.getElementById(contenedor);
	var contGaleria = document.getElementById(contenedorGaleria);
	this.req = new Request(oCl);	
	this.accion = "obtenerCuadro";
	this.dirImg = dirImg;
	this.tipo = tipo;
	this.cantidad = cantidad;
	this.navegador = document.getElementById("navegador");
	this.idElemento = idElemento;
	var posicion = 1;
	if(this.directorio != "programas")posicion = 0;
	var archivo = this.directorio+"/"+this.idElemento+"Galeria"+posicion+".xml";
	this.req.pedir("../xmlHttpRequest/galeria.php","idElemento|=|"+this.idElemento+"|&|tipo|=|"+tipo+"|&|");
	this.arreglo = new Array();
	this.fotoActual = "";
	this.archivoXML = archivoXML;
	this.foto = "";
	
	var cerrarVista = function(){
		oCl.foto.style.visibility = "hidden";
		oCl.blockeador.style.display = "none";
		oCl.foto.style.top = "-1000px";
	}
	this.onRequestLoad = function(){
		switch(this.accion){
			case "obtenerCuadro":

				if(this.req.respuestaHTML != ""){
					var prov = document.createElement("div");
					prov.style.display = "none";
					document.body.appendChild(prov);
					prov.innerHTML = this.req.respuestaHTML;
					this.foto = document.getElementById("popupGal");
					document.body.appendChild(this.foto);
					this.foto.style.visibility = "visible";
					this.imagen = document.getElementById("imagenGrande");
					this.loading = document.getElementById("leyendoImagen");
					this.anterior = document.getElementById("anteriorGaleria");
					this.siguiente = document.getElementById("siguienteGaleria");
					var cerrar = document.getElementById("cerrarGaleria");
					AddEvent(cerrar,'click',cerrarVista);
					this.foto.style.width = "400px";
					this.contImag = document.getElementById("contImag");
					this.popupFotoGal = document.getElementById("popupFotoGal");
					this.contFlecha = document.getElementById("contFlecha");
					this.epigrafe = document.getElementById("epigrafe");
					this.blockeador = document.createElement("div");
					this.blockeador.className = "blockeadorGaleria";
					this.anteriorGris = document.getElementById("anteriorGrisGal");
					this.siguienteGris = document.getElementById("siguienteGrisGal");
					document.body.appendChild(this.blockeador);
					
					if(Nav.esIE || Nav.esIE7) this.blockeador.style.filter = 'alpha(opacity=45)';
					else this.blockeador.style.MozOpacity = ".45";

					AddEvent(oCl.anterior,'click',function(){cambiarFoto(-1)});
					AddEvent(oCl.siguiente,'click',function(){cambiarFoto(1)});					

					this.centrarContenido();
					cerrarVista();
					this.crearGaleria(0);
				}
				else{
					var gal = document.getElementById("galeria");
					if(gal)gal.style.display = "none";	
				}
			break;
			case "armarGaleria":
				var nodo = this.req.respuestaXML;
				if(nodo && nodo.childNodes.length >0){
					contGaleria.innerHTML ="";
					var imag = obtenerHijo(nodo,0);
					for(i=0;i<imag.childNodes.length;i++){
						if(imag.childNodes[i].tagName)crearCuadro(imag.childNodes[i]);
					}
					if(imag.childNodes.length == 0){
						var gal = document.getElementById("galeria");
						if(gal)gal.style.display = "none";	
	
					}
					var sep = document.createElement("div");
					sep.className = "floatFix";
					sep.style.border = "0";
					//contGaleria.appendChild(sep);
					this.armarNavegador(oCl.arreglo.length);
				}
				
			break;

		}
	}
	this.armarNavegador = function(longitud){
		cantidad = Math.ceil(longitud/this.cantidad);
		for(k=1;k<=cantidad;k++){
			armarCuadro(k);			
		}
	}
	var armarCuadro = function(i){
		var href = document.createElement("div");
		href.actual = new Object();
		if(i==1){
			href.className = "navHover"; 
			href.actual = 1
			oCl.paginaActual = href;
		}	
		else{
			href.actual = 0;
			href.className = "navOut";
		}	
		href.style.display = "inline";
		href.onclick = function(){
			oCl.cambiarPagina(i);
			if(oCl.paginaActual){
				oCl.paginaActual.className = "navOut";
				oCl.paginaActual.actual = 0;
			}
			oCl.paginaActual = href;
			href.className = "navHover";
			href.actual = 1;
		}
		href.onmouseover = function(){
			href.className = "navHover";
			href.style.cursor = "pointer";
		}
		href.onmouseout = function(){
			if(href.actual != 1)href.className = "navOut";
		}
		href.innerHTML = i;
		oCl.navegador.appendChild(href);			
	}
	this.cambiarPagina = function(indice){
		var ind = ((indice-1)*this.cantidad);
		for(m=0;m<this.arreglo.length;m++)this.arreglo[m].style.display = "none";
		for(k=ind;k<(ind+this.cantidad);k++)if(this.arreglo[k])this.arreglo[k].style.display = "";
		
	}
	var obtenerHijo = function(objeto,indice){
		var hijo = null;
		var descontar = indice;
		for(k=0;k<objeto.childNodes.length;k++){
			if((objeto.childNodes[k].tagName) || (objeto.childNodes[k].nodeName == "#cdata-section"))descontar--;
			if(descontar < 0){
				hijo = new Object();
				hijo = objeto.childNodes[k];
				return hijo;
			}
		}
		return hijo;
	}

	var cambiarFoto = function(valor){
		var ind = parseInt(oCl.fotoActual)+(valor);
		mostrarFoto(oCl.arreglo[ind]);	
	}

	var mostrarFoto = function(objeto){
		var scrn = getBodyDims();
		oCl.blockeador.style.display = "block";
		oCl.blockeador.style.height = (scrn.h+18)+"px";
		oCl.blockeador.style.width = scrn.w+"px";
		oCl.popupFotoGal.style.height = (parseInt(objeto.h)+80)+"px";
		oCl.popupFotoGal.style.width = (parseInt(objeto.w)+10)+"px";
		oCl.foto.style.height = (parseInt(objeto.h)+80)+"px";
		oCl.foto.style.width = (parseInt(objeto.w)+10)+"px";
		oCl.imagen.src = objeto.grande;
		oCl.imagen.alt = objeto.epigrafe;


		RemEvent(oCl.imagen,'load',cargaFinalizada);
		AddEvent(oCl.imagen,'load',cargaFinalizada);


		oCl.anterior.style.display = "none";
		oCl.siguiente.style.display = "none";
		oCl.contFlecha.style.display = "none";
		oCl.contImag.style.display = "none";
		oCl.loading.style.display = "block";
		oCl.loading.style.top = (Math.round((oCl.foto.offsetHeight - oCl.loading.offsetHeight)/2))+"px";
		oCl.loading.style.left = (Math.round((oCl.foto.offsetWidth - oCl.loading.offsetWidth)/2))+"px";
		oCl.epigrafe.innerHTML = objeto.epigrafe;
		oCl.centrarContenido();
		if(objeto.indice == oCl.arreglo.length-1){
			oCl.siguiente.style.display = "none";
			oCl.siguienteGris.style.display = "";
		}
		else{
			oCl.siguiente.style.display = "";
			oCl.siguienteGris.style.display = "none";
		}
		if(objeto.indice == 0){
			oCl.anterior.style.display = "none";
			oCl.anteriorGris.style.display = "";
		}
		else{
			oCl.anterior.style.display = "";
			oCl.anteriorGris.style.display = "none";
		}
		oCl.epigrafe.style.display = "none";
		oCl.foto.style.visibility = "visible";
		if((objeto.indice == oCl.fotoActual) && ((typeof oCl.fotoActual) == "number"))cargaFinalizada();
		oCl.fotoActual = objeto.indice;

	}
	var cargaFinalizada = function(){
		oCl.loading.style.display = "none";
		if(oCl.epigrafe.innerHTML != "")oCl.epigrafe.style.display = "block";
		oCl.contImag.style.display = "block";	
		oCl.popupFotoGal.style.height = "auto";
		if(oCl.arreglo.length > 1)oCl.contFlecha.style.display = "block";
		oCl.foto.style.height = oCl.popupFotoGal.offsetHeight+"px";
	}
	var crearCuadro = function(objeto){
		var div = document.createElement("div");		
		div.className = "fotoMini";
		div.style.backgroundImage = "url('"+oCl.dirImg+objeto.getAttribute("pathMini")+"')";
		div.w = objeto.getAttribute("anchoGrande");
		div.h = objeto.getAttribute("altoGrande");		
		div.grande = oCl.dirImg+objeto.getAttribute("path");
		div.onclick = function(){mostrarFoto(div)};
		div.epigrafe = obtenerHijo(objeto,0).data//objeto.firstChild.data;
		oCl.arreglo[oCl.arreglo.length] = div;
		oCl.arreglo[oCl.arreglo.length-1].indice = oCl.arreglo.length-1;
		if(oCl.arreglo.length > oCl.cantidad)div.style.display = "none";
		contGaleria.appendChild(div);
	}
	this.centrarContenido = function(){
		var pos = getScrollPos();
		var scrn = getBodyWHAvaible();
		var dim = getBodyDims();
		this.foto.style.top = (Math.round((scrn.h - this.foto.offsetHeight)/2)+pos.y)+"px";
		this.foto.style.left = (Math.round((scrn.w - this.foto.offsetWidth)/2)+pos.x)+"px";
	}
	this.crearGaleria = function(posicion){
		this.accion = "armarGaleria";
		this.req.pedir(this.archivoXML,"idElemento|=|"+this.idElemento+"|&|");
	}
	
}