// JavaScript Document
var arrSe = new Array();
arrSe['fichaProgramas'] = DIR_PROGRAMAS;
arrSe['home'] = DIR_PROGRAMAS;
arrSe['atractivo'] = DIR_NOTICIAS;
var Galeria = function(contenedor,contenedorGaleria,se,idElemento){
	var oCl = this;
	var cont = document.getElementById(contenedor);
	var contGaleria = document.getElementById(contenedorGaleria);
	if(contGaleria)contGaleria.style.display = "none";
	this.req = new Request(oCl);	
	ERROR.alerta = false;
	this.accion = "obtenerCuadro";
	
	this.directorio = arrSe[se];
	this.idElemento = idElemento;
	var posicion = 1;
	if(this.directorio != DIR_PROGRAMAS)posicion = 0;
	var archivo = this.directorio+this.idElemento+"Galeria"+posicion+".xml";
	this.req.pedir(DIR_SERVER_ROOT+"xmlHttpRequest/galeria.php","archivo|=|"+"../"+archivo+"|&|directorio|=|"+this.directorio+"|&|");
	this.arreglo = new Array();
	this.fotoActual = "";
	
	this.foto = "";

	var cerrarVista = function(){
		oCl.foto.style.visibility = "hidden";
		oCl.blockeador.style.display = "none";
		oCl.foto.style.top = "-1000px";
		visualizarSolapas('visible');	
	}
	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(1);
				}
			break;
			case "armarGaleria":

				var nodo = this.req.respuestaXML;
				if(nodo && nodo.childNodes.length >0){
					if(contGaleria){

						contGaleria.innerHTML ="";
						var prev = obtenerHijo(nodo,0);
						var imag = obtenerHijo(prev,1);	
						for(i=0;i<imag.childNodes.length;i++){
							if(imag.childNodes[i].tagName)crearCuadro(imag.childNodes[i]);
							if(i == imag.childNodes.length-1){
								AddEvent(oCl.arreglo[oCl.arreglo.length-1], 'load', controlarAltoLateral);	
							}
						}
						var sep = document.createElement("div");
						sep.className = "floatFix";
						sep.style.border = "0";
						contGaleria.appendChild(sep);
						contGaleria.style.display = "block";
					}

				}
			break;

		}
	}
	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";
		visualizarSolapas('hidden');
		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("img");		
		div.className = "fotoMini";
//		div.style.backgroundImage = "url('"+objeto.getAttribute("m")+"')";
		div.src = DIR_RAIZ_WEB+objeto.getAttribute("m");
		div.w = objeto.getAttribute("wF");
		div.h = objeto.getAttribute("hF");		
		div.grande = DIR_RAIZ_WEB+objeto.getAttribute("f");
		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;
		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(DIR_SERVER_ROOT+"xmlHttpRequest/cargador_xml.php?file="+DIR_RAIZ_WEB+archivo, null, 'GET');
	}
	
}