/***********************************************************************

		Pseudo-lightbox
		Autor: José Molina Cornejo (caricatos)

***********************************************************************/

var pseudo = new Object();
pseudo.cargada = new Boolean;
pseudo.temporizador = 25;

pseudo.bordeGrueso = 3;
pseudo.bordeTipo = "inset";
pseudo.bordeColor = "gray";
pseudo.fondo = "gray";
pseudo.color_mensaje = "white";
pseudo.reloj = "../dibujos/arena.gif";
pseudo.imagen_reloj = new Image();
pseudo.imagen_reloj.src = this.reloj;
pseudo.cerrar = "../dibujos/no.gif";
pseudo.imagen_cerrar = new Image();
pseudo.imagen_cerrar.src = this.cerrar;
pseudo.mensaje_cerrar = "cerrar";
pseudo.fin_aleatorio = true;
pseudo.orden_fin_efecto = 0;
pseudo.ie = (document.all) ? true : false; // detectamos explorer
pseudo.ff = navigator.userAgent.indexOf ('Firefox') != -1; // detectamos firefox
pseudo._x = new Number();
pseudo._y = new Number();
pseudo._xx = new Number();
pseudo._yy = new Number();
pseudo._imagen = new String();
pseudo._id = new String();
pseudo._mensaje = new String();
pseudo._hay_mensaje = new Boolean;
pseudo.visitadas = new Object();
pseudo.clon = new String();

pseudo.nada = function() {return false;}
pseudo.todo = function() {return true;}
pseudo.tag = function(id)	{return document.getElementById(id);}

pseudo.eventoscroll = ["keypress", "keydown", "keyup", "mousewheel"];

pseudo.usar_reloj = function(cual)	{
	this.imagen_reloj.src = cual;
	this.reloj = cual;
}

pseudo.scroll_no = function()	{

	//if (navigator.userAgent.indexOf ('Firefox') != -1)
		document.body.style.overflow = "hidden";

	for (i in this.eventoscroll)	{
		document["on" + this.eventoscroll[i]] = this.nada;
	}
}

pseudo.scroll_si = function()	{
	for (i in this.eventoscroll)
		document["on" + this.eventoscroll[i]] = this.todo;
	document.body.style.overflow = "auto";
}

pseudo.opacar = function(capa, _n)	{
	__n = _n / 10;
	if (this.ie)	{
		this.tag(capa).style.filter = "alpha(opacity=" + _n + "0)";
		with(this.tag(capa))	{
			for (i = 0, total = childNodes.length; i < total; i ++)
				childNodes[i].style.filter = "alpha(opacity=" + _n + "0)";
		}
	}
	else	this.tag(capa).style.opacity = __n;
}

pseudo.ini_efecto = function()	{
	this.scroll_no();

	this._x = (pseudo.ie) ? document.documentElement.scrollLeft : window.scrollX;
	this._y = (pseudo.ie) ? document.documentElement.scrollTop : window.scrollY;
	this._xx = window.document.documentElement.clientWidth;
	this._yy = window.document.documentElement.clientHeight;
	pop = document.createElement("div");
	with(pop.style)	{

		visibility = "hidden";
		position = "absolute";
		top = this._y + "px";
		left = this._x + "px";
		height = this._yy + "px";
		width = this._xx + "px";

		(pseudo.ie)	?	filter = "alpha(opacity=0)" : opacity = "0";
		background = (this.cargada)	? this.fondo : this.fondo + " url(" + this.reloj + ") no-repeat center center";
		//background = this.fondo + " url(" + this.reloj + ") no-repeat center center";
	}
	pop.id = "pseudo_fondo";
	document.body.appendChild(pop);

	pop.style.visibility = "visible";

	//this.scroll_no();

	for (opacando = 1; opacando < 10; opacando ++)
		setTimeout("pseudo.opacar('pseudo_fondo', " + opacando + ")", opacando * this.temporizador);
}

pseudo.fin_efecto = function(orden)	{
	pseudo.scroll_si();
	for (opacando = 1; opacando < 10; opacando ++)
		setTimeout("pseudo.opacar('pseudo_fondo', " + (10 - opacando) + ")", opacando * pseudo.temporizador + (10 * pseudo.temporizador * orden));
	setTimeout("document.body.removeChild(pseudo.tag('pseudo_fondo'))", pseudo.temporizador * 20);
}

pseudo.ini_capa = function()	{
	if(this.cargada)	{
		pop = this.visitadas[this._id].cloneNode(true);
		with(pop.style)	{
			position = "absolute";
			backgroundImage = "none";
			top = this._y + "px";
			pop.style.left = this._x + "px";
			visibility = "hidden";
			(this.ie)	?	filter = "alpha(opacity=0)" : opacity = "0";
		}
		if (this.ie)
			for (i = 0, total = pop.childNodes.length; i < total; i ++)
				pop.childNodes[i].style.filter = "alpha(opacity=0)";
	}
	else	{
		pop = this.tag("pseudo_fondo").cloneNode(true);

		im = document.createElement("img");
		im.id = this._id;
		im.className = "pseudo_img";
		with(im.style)	{
			border = this.bordeGrueso + "px " + this.bordeTipo + " " + this.bordeColor;
			cursor = "pointer";
			position = "absolute";
		}


		


		pop_controles = document.createElement("div");
		pop_controles.id = "pseudo_controles";
		
		with(pop_controles.style)	{
			position = "absolute";
			textAlign = "right";
			backgroundColor = (this._hay_mensaje) ? this.color_mensaje : "transparent";
			margin = "0";
		}



		if (this._hay_mensaje)	{
			pop_mensaje = document.createElement("div");
			pop_mensaje.id = "pseudo_mensaje";
			pop_span = document.createElement("span");

			pop_span.appendChild(document.createTextNode(this._mensaje));
			pop_mensaje.appendChild(pop_span);
			pop_mensaje.style.textAlign = "center";
			pop_mensaje.style.position = "absolute";
			pop_mensaje.style.left = "0";
			pop_mensaje.style.top = "0";
			pop_mensaje.style.width = "100%";
			pop_mensaje.style.height = "100%";

			//pop_mensaje.style.backgroundColor = "red";
			//pop_mensaje.onclick = this.fin;

			pop_controles.appendChild(pop_mensaje);
		}

		//
		//else	{
			pop_cerrar = document.createElement("button");
			pop_cerrar.setAttribute("type", "button");
			pop_cerrar.appendChild(document.createTextNode(this.mensaje_cerrar));
			pop_cerrar_img = document.createElement("img");
			pop_cerrar_img.src = this.cerrar;
			pop_cerrar.appendChild(pop_cerrar_img);
			pop_cerrar.id = "pseudo_cerrar";

			pop_boton_cerrar = document.createElement("div");


			with(pop_boton_cerrar)	{

				with(style)	{
					position: "absolute";
					top = "0px";
					left = "0px";
					height = "100%";
					width = "100%";
					//backgroundColor = "blue";
				}
				appendChild(pop_cerrar);
				id = "pseudo_boton_cerrar";
			}
			pop_controles.appendChild(pop_boton_cerrar);
		//}
		
		pop.appendChild(pop_controles);

		pop.appendChild(im);
		im.onload = function()	{
			with(pseudo.tag("pseudo_fondo").style)	{	backgroundImage = "none";}
			with(pseudo.tag("pseudo_capa").style)	{
				opacity = "0";
				visibility = "visible";
			}
			if (pseudo.ie)	{
				pseudo.tag("pseudo_capa").style.filter = "alpha(opacity=0)";
				with(pseudo.tag("pseudo_capa"))	{
					for(i = 0, total = childNodes.length; i < total; i ++)
						childNodes[i].style.filter = "alpha(opacity=0)";
				}
			}
			this.style.backgroundColor = "white";
			xx = (parseInt(pseudo._xx) -  this.width) / 2;
			yy = ((parseInt(pseudo._yy) - this.height) / 2);
			yy -= (this.parentNode.childNodes.length == 2) ? 20 : 0;

			this.style.top = yy + "px";
			this.style.left = xx + "px";

			with(pseudo.tag("pseudo_controles").style)	{
				width = this.offsetWidth + "px";
				top = (yy + this.offsetHeight) + "px";
				left = xx + "px";
				padding = "0";
			}

			if (pseudo._hay_mensaje)	{
				pseudo.tag("pseudo_mensaje").style.width =
					(this.offsetWidth - pseudo.tag("pseudo_cerrar").offsetWidth) + "px";
			}

			if (!pseudo.cargada)	pseudo.visitadas[this.id] = this.parentNode.cloneNode(true);
			for (opacando = 1; opacando <= 10; opacando ++)
				setTimeout("pseudo.opacar('pseudo_capa', " + opacando + ")", opacando * pseudo.temporizador + (10 * pseudo.temporizador));
		}

		im.src = this._imagen;

	}

	pop.id = "pseudo_capa";

	pop.style.backgroundColor = "transparent";
	pop.style.visibility = "hidden";
	document.body.appendChild(pop);
	

	this.tag(this._id).title = "pulse para cerrar";
	this.tag(this._id).onclick = this.fin;
	this.tag("pseudo_cerrar").onclick = this.fin;

	//this.tag("pseudo_controles").style.top = this._y + "px";
	this.tag("pseudo_controles").style.visibility = "visible";

	if (this.cargada)	{
		(pseudo.ie)	?	pop.style.filter = "alpha(opacity=0)" : pop.style.opacity = "0";
		pop.style.visibility = "visible";
		for (opacando = 1; opacando <= 10; opacando ++)	{
			setTimeout("pseudo.opacar('pseudo_capa', " + opacando + ")", opacando * this.temporizador + (10 * this.temporizador));
			setTimeout("pseudo.opacar('pseudo_controles', " + opacando + ")", opacando * this.temporizador + (10 * this.temporizador));
		}
	}
}

pseudo.fin_capa = function(orden)	{
	for (opacando = 1; opacando < 10; opacando ++)
		setTimeout("pseudo.opacar('pseudo_capa', " + (10 - opacando) + ")", opacando * pseudo.temporizador + (10 * pseudo.temporizador * orden));
	setTimeout("document.body.removeChild(pseudo.tag('pseudo_capa'))", pseudo.temporizador * 20);
}

pseudo.fin = function()	{
	orden = (pseudo.fin_aleatorio) ? parseInt(Math.random() * 100) % 2 : pseudo.orden_fin_efecto;
	pseudo.fin_capa(1 - orden);
	pseudo.fin_efecto(orden);
}

pseudo.mostrar = function(que, id, texto)	{
	this._imagen = que;
	this._id = id;
	this._hay_mensaje = (texto != undefined);
	this._mensaje = (this._hay_mensaje) ? texto : "";
	this.cargada = (this.visitadas[this._id] != undefined);
	this.ini_efecto();
	this.ini_capa();
}

pseudo.ini_clon = function()	{
	clon = this.tag(this._id).cloneNode(true);
	clon.id = "pseudo_capa";
	capa_controles = document.createElement("div");
	capa_controles.id = "pseudo_controles";

	boton_cierre = document.createElement("button");
	
	cierre = document.createElement("img");
	cierre.src = pseudo.cerrar;
	boton_cierre.onclick = pseudo.fin_clon;

	//clon.onclick = pseudo.fin_clon;

	boton_cierre.id = "pseudo_cierre";
	boton_cierre.style.cursor = "pointer";

	boton_cierre.appendChild(document.createTextNode(pseudo.mensaje_cerrar));
	boton_cierre.appendChild(cierre);
	capa_controles.style.position = "absolute";
	capa_controles.style.textAlign = "right";
	//capa_controles.style.backgroundColor = "green";

	//clon.appendChild(boton_cierre);
	ancho = this.tag(this._id).offsetWidth;
	alto = this.tag(this._id).offsetHeight;

	xx = (parseInt(this._xx) - ancho) / 2;
	yy = ((parseInt(this._yy) - alto) / 2);
	yy -= 20;

	capa_controles.appendChild(boton_cierre);

	with(clon.style)	{
		backgroundColor = "white";
		position = "absolute";
		top = (yy + this._y) +"px";
		width = ancho + "px";
		left = (xx + this._x) + "px";
		height = alto + "px";
		visibility = "visible";
		(this.ie)	?	filter = "alpha(opacity=0)" : opacity = "0";
	}

	document.body.appendChild(clon);
	
	with(capa_controles.style)	{
		top = (clon.offsetTop + clon.offsetHeight) + "px";
		left = clon.offsetLeft + "px";
		width = clon.offsetWidth + "px";
		visibility = "visible";
		(this.ie)	?	filter = "alpha(opacity=0)" : opacity = "0";
	}


	document.body.appendChild(capa_controles);

	for (opacando = 1; opacando <= 10; opacando ++)	{
		setTimeout("pseudo.opacar('pseudo_capa', " + opacando + ")", opacando * this.temporizador + (10 * this.temporizador));
		setTimeout("pseudo.opacar('pseudo_controles', " + opacando + ")", opacando * this.temporizador + (10 * this.temporizador));
	}

}

pseudo.limpia_clon = function()	{
	pseudo.tag(pseudo._id).innerHTML = pseudo.clon;
}

pseudo.fin_clon = function()	{
	orden = (pseudo.fin_aleatorio) ? parseInt(Math.random() * 100) % 2 : pseudo.orden_fin_efecto;
	pseudo.tag("pseudo_controles").parentNode.removeChild(pseudo.tag("pseudo_controles"));
	pseudo.clon = pseudo.tag("pseudo_capa").innerHTML;

	pseudo.fin_capa(1 - orden);
	pseudo.fin_efecto(orden);
	setTimeout(pseudo.limpia_clon, pseudo.temporizador * 11);
}


pseudo.mostrarCapa = function(id)	{
	this.cargada = true;
	this._id = id;
	this.ini_efecto();
	this.ini_clon();
}



pseudo.asignaCapa = function(actual, copia)	{
	if ((this.visitadas[copia] == undefined))	{
		al = window.document.documentElement.clientHeight + "px";
		an = window.document.documentElement.clientWidth + "px";

		pop = document.createElement("div");
		pop.id = "pseudo_capa";

		with(pop.style)	{
			height = al;
			width = an;
			(pseudo.ie)	?	filter = "alpha(opacity=0)" : opacity = "0";
		}

		clon = pseudo.tag(actual).cloneNode(true);
		clon.id = copia;
		clon.title = "pulse para cerrar";
		this.scroll_no();
		anchura = pseudo.tag(actual).offsetWidth;
		altura = pseudo.tag(actual).offsetHeight;
		this.scroll_si();
		tope = parseInt((parseInt(al) - altura) / 2);
		//alert(tope);

		with(clon.style)	{
			display = "block";
			position = "absolute";
			width = anchura + "px";
			height = altura + "px";
			backgroundColor = "white";
			top = tope + "px";
			left = parseInt((parseInt(an) - anchura) / 2) + "px";
			cursor = "pointer";
		}
		pop_cerrar = document.createElement("div")
		pop_cerrar.id = "pseudo_controles";
		pop_cerrar.style.left = parseInt((parseInt(an) - anchura) / 2) + "px";
		pop_cerrar.style.width = anchura + "px";
		pop_cerrar.style.textAlign = "right";
		pop_cerrar.style.top = tope + altura + "px";
		pop_cerrar.style.position = "absolute";
		//pop_cerrar.style.backgroundColor = "red";

		pop_boton_cerrar = document.createElement("button");
		pop_boton_cerrar.id = "pseudo_cerrar";
		pop_boton_cerrar.appendChild(document.createTextNode(this.mensaje_cerrar));


		pop_cerrar_img = document.createElement("img");
		pop_cerrar_img.src = this.cerrar;
		pop_boton_cerrar.appendChild(pop_cerrar_img);
		pop_cerrar.appendChild(pop_boton_cerrar);



		pop.appendChild(clon);
		pop.appendChild(pop_cerrar);
		
		this.visitadas[copia] = pop;
	}
}