MAX_THUMBS = 6;
ENLACE_FOTO_THUMB = 0;
ENLACE_FOTO = 1;
DESC_FOTO = 2;
ID_FOTO = 3;

function Galeria(pos, album) {
	this.pos = pos;
	this.album = album;
	this.cambiando = false;
	this.nueva = false;
	this.nuevaImg = false;
	this.anchoImg = 200;
	this.intAnima = false;
	this.contador = 0;
	this.factorRed = 0.1;

	this.intervaloCambia = false;
	this.playActivado = false;
	this.stopActivado = false;
	this.numero = new Array(1,2,3,4,5,6);
}

function muestraAlbum(id,nombre) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("¡Tu navegador no soporta AJAX!. Actualizaló.");
  		return;
  	} 

	var contenido = document.createElement('div');
	contenido.id = 'visor_fotos_album';

	xmlHttp.onreadystatechange=function()
	   {
		if(xmlHttp.readyState == 1) {
			contenido.innerHTML = '<p>Espere un momento por favor...</p>';
		} else if(xmlHttp.readyState == 4) {
			contenido.innerHTML = xmlHttp.responseText;
			scripts = contenido.getElementsByTagName("script");
			for(i = 0; i < scripts.length; i++) {
				eval(scripts[i].innerHTML);
			}
			setTimeout("centrarVentanaModal()", 1000);
  		}
	   }

	xmlHttp.open("GET",'inc/aux/mostrar_album.php?idalbum=' + id, true);
	xmlHttp.send(null);

	creaVentanaModal(nombre, contenido);
}

Galeria.prototype.crearMiniFotos = function() {
	divMiniFotos = document.getElementById("minifotos");
	flechaIzq = document.getElementById("minifotos_izq");
	flechaIzq.firstChild.style.visibility = "hidden";

	for(var i = 0; i < MAX_THUMBS && i < fotos.length; i++) {
		contenedor = document.createElement('div');
		contenedor.className = "contenedor_thumb";
		enlace = document.createElement('a');
		enlace.href = "javascript:galeria.cambiaFoto(" + i + ")";
		enlace.className = "enlace_thumb";
		thumb = document.createElement('img');
		thumb.className = "thumb";
		thumb.id = "thumb" + i;
		thumb.src = fotos[i][ENLACE_FOTO_THUMB];

		thumbNum = document.createElement('span');
		thumbNum.className = "num_thumb";
		thumbNum.id = "num_thumb" + i;
		txtNum = document.createTextNode(i+1);
		thumbNum.appendChild(txtNum);

		enlace.appendChild(thumb);
		contenedor.appendChild(enlace);
		contenedor.appendChild(thumbNum);
		divMiniFotos.appendChild(contenedor);
	}

	if(fotos.length <= MAX_THUMBS) {
		flechaDer = document.getElementById("minifotos_der");
		flechaDer.firstChild.style.visibility = "hidden";
	}
}

Galeria.prototype.cambiaThumbs = function(pos) {
	for(i = 0; i < MAX_THUMBS; i++) {
		thumbActual = document.getElementById("thumb" + i);
		numeroThumbActual = document.getElementById("num_thumb" + i);
		numeroThumbActual.innerHTML = String(i + pos + 1);
	
		if((i + pos) < fotos.length) {
			thumbActual.src = fotos[i+pos][ENLACE_FOTO_THUMB];;
			thumbActual.parentNode.href = "javascript:galeria.cambiaFoto(" + (i+pos) + ")";
		} else {
			thumbActual.src = "";
			thumbActual.parentNode.href = "javascript.void(0);";
		}
	}

	flechaIzq = document.getElementById("minifotos_izq");
	flechaDer = document.getElementById("minifotos_der");

	if(pos > 0) {
		flechaIzq.firstChild.style.visibility = "visible";
		flechaIzq.firstChild.href = "javascript:galeria.cambiaThumbs(" + (pos - MAX_THUMBS) + ")";
	} else {
		flechaIzq.firstChild.style.visibility = "hidden";
	}

	if(pos + MAX_THUMBS < fotos.length) {
		flechaDer.firstChild.style.visibility = "visible";
		flechaDer.firstChild.href = "javascript:galeria.cambiaThumbs(" + (pos + MAX_THUMBS) + ")";
	} else {
		flechaDer.firstChild.style.visibility = "hidden";
	}
}

Galeria.prototype.cambiaFoto = function(posAct, avance) {
	if(this.cambiando) {
			return;
	}
	avance = typeof(avance) != 'undefined' ? avance : 0;

	this.pos = parseInt(posAct) + parseInt(avance);
	if(this.pos == fotos.length) {
		this.pos = 0;
	} else if(this.pos < 0) {
		this.pos = fotos.length - 1;
	}

	numFoto = document.getElementById("desc_num_foto_actual");
	numFoto.innerHTML = (this.pos + 1);
	descFoto = document.getElementById("desc_foto_actual");
	descFoto2 = document.getElementById("area_mod_desc_foto");
	if (descFoto) {
		descFoto.innerHTML = fotos[this.pos][DESC_FOTO];
	} else {
		descFoto2.value = fotos[this.pos][DESC_FOTO].replace(/<br \/>/g, "\n");
	}
	document.getElementById("foto_principal").src = fotos[this.pos][ENLACE_FOTO];

	flechaSig = document.getElementById("boton_sig_foto");
	flechaSig.href = "javascript:galeria.cambiaFoto(" + this.pos + ",1)";
	flechaAnt = document.getElementById("boton_ant_foto");
	flechaAnt.href = "javascript:galeria.cambiaFoto(" + this.pos + ",-1)";

	setTimeout("centrarVentanaModal()", 50);
	/*this.anchoImg = parseInt(document.getElementById("foto_principal").width);
	this.intAnima = setTimeout("galeria.anima()",100);
	this.cambiando = true;*/
}

Galeria.prototype.avanzaAuto = function() {
	this.cambiaFoto(this.pos, 1);
	this.intervaloCambia = setTimeout("galeria.avanzaAuto()", 8000);
} 

Galeria.prototype.activaPlay = function() {
	if(this.playActivado) return;
	this.stopActivado = false;
	this.playActivado = true;
	this.intervaloCambia = setTimeout("galeria.avanzaAuto()", 8000);
	document.getElementById("boton_play").src = "img/galeria/play_select.png";
	document.getElementById("boton_stop").src = "img/galeria/stop.png";
}

Galeria.prototype.activaStop = function() {
	if(this.stopActivado) return;
	this.stopActivado = true;
	this.playActivado = false;
	clearTimeout(this.intervaloCambia);
	document.getElementById("boton_play").src = "img/galeria/play.png";
	document.getElementById("boton_stop").src = "img/galeria/stop_select.png";
}

Galeria.prototype.borraFotoAct = function() {
	galeria.activaStop();

	if(!confirm("¿Realmente desea borrar esta foto del álbum?")) {
		return false;
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("¡Tu navegador no soporta AJAX!. Actualizaló.");
  		return false;
  	} 

	xmlHttp.onreadystatechange=function()
	   {
		if(xmlHttp.readyState == 4) {
			alert(xmlHttp.responseText);
			galeria.activaStop();
			cierraVentanaModal();
			document.getElementById('form_borrar_foto').borra_foto.click();
  		}
	   }

	xmlHttp.open("POST",'inc/aux/funciones_php_ajax.php',true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	xmlHttp.send('funcion=borraFoto&album=' + this.album + '&foto=' + fotos[this.pos][ID_FOTO]);

	return false;
}

Galeria.prototype.modDescAct = function(nuevaDesc) {
	galeria.activaStop();

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("¡Tu navegador no soporta AJAX!. Actualizaló.");
  		return false;
  	}

	xmlHttp.onreadystatechange=function()
	   {
		if(xmlHttp.readyState == 4) {
			alert(xmlHttp.responseText);
  		}
	   }

	fotos[this.pos][DESC_FOTO] = nuevaDesc;
	xmlHttp.open("POST",'inc/aux/funciones_php_ajax.php',true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	xmlHttp.send('funcion=modDescFoto&album=' + this.album + '&foto=' + fotos[this.pos][ID_FOTO] + "&desc=" + nuevaDesc);

	return false;
}

/*
Galeria.prototype.anima = function() {
	this.intAnima = setTimeout("galeria.anima()",100);
	if(this.nueva == false) {
		this.contador += 1;
		tam = parseInt(this.anchoImg*(1.0 - (this.factorRed*this.contador)));
		document.getElementById("foto_principal").style.width = tam + 'px';
		if(tam <= this.anchoImg) {
			document.getElementById("foto_principal").src = this.nuevaImg;
			this.anchoImg = parseInt(document.getElementById("foto_principal").width);
			this.nueva = true;
			this.contador = 0;
		}
	} else {
		this.contador += 1;
		tam = parseInt(this.anchoImg*(this.factorRed*this.contador)) + 'px';
		document.getElementById("foto_principal").style.width = tam + 'px';
		if(tam >= this.anchoImg) {
			document.getElementById("foto_principal").style.width = this.anchoImg;
			this.cambiando = false;
			this.nueva = false;
			clearTimeout(this.intAnima);
			setTimeout("centrarVentanaModal()", 50);
			this.contador = 0;
		}
	}
}
*/
