TPopUp.aVentanas = Array ();

TPopUp._Alto         = 50;
TPopUp._AltoPagina   = 0;
TPopUp._AltoVentana  = 0;
TPopUp._Ancho        = 100;
TPopUp._AnchoPagina  = 0;
TPopUp._AnchoVentana = 0;
TPopUp._zIndex       = 1000;

TPopUp.prototype.Alpha        = 50;
TPopUp.prototype.AlphaSombra  = 80;
TPopUp.prototype.ColorFondo   = '';
TPopUp.prototype.ColorSombra  = '';
TPopUp.prototype.ColorVelo    = '';
TPopUp.prototype.Vars         = null;
TPopUp.prototype.XY           = null;

TPopUp.prototype.onCerrar = null;

TPopUp.prototype._oBorde      = null;
TPopUp.prototype._oSombra     = null;
TPopUp.prototype._oVelo       = null;


TPopUp._addEvent = function (oElemento, sEvento, oFuncion)
{
	if (oElemento.addEventListener)	oElemento.addEventListener (sEvento, oFuncion, false);
	else if (oElemento.attachEvent) oElemento.attachEvent ('on' + sEvento, oFuncion);
}


TPopUp.prototype._Borde = function ()
{
	this._oBorde   = document.createElement ('div');

	this._oBorde.setAttribute ('Capa', 'Borde');
	this._oBorde.style.borderWidth = '1px';
	this._oBorde.style.borderStyle = 'solid';
	this._oBorde.style.borderColor = '#CCCCCC';
	this._oBorde.style.position    = 'absolute';
	this._oBorde.style.left        = '0px';
	this._oBorde.style.top         = '0px';
	this._oBorde.style.width       = '0px';
	this._oBorde.style.height      = '0px';
	this._oBorde.style.zIndex      = TPopUp._zIndex + 2;
	document.body.appendChild (this._oBorde);
}


TPopUp.prototype._Centrar = function ()
{
	var ScrollTop = window.pageYOffset ? window.pageYOffset : Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	var fLeft     = (this.XY && this.XY [0] ? this.XY [0] : ((TPopUp._AnchoPagina - this._oVentana.clientWidth) / 2));

	if (fLeft + this._oVentana.offsetWidth > TPopUp._AnchoPagina) fLeft -= this._oVentana.offsetWidth;
	this._oVentana.style.left =  fLeft + 'px';
	this._oVentana.style.top  = (this.XY && this.XY [1] ? this.XY [1] : ((TPopUp._AnchoVentana - this._oVentana.clientHeight) / 2) + ScrollTop) + 'px';

	this._PosBorde ();
	this._PosSombra ();
}


TPopUp.Cerrar = function ()
{
	var Ventana = TPopUp.aVentanas.pop ();

	if (Ventana)
	{	if (Ventana.onCerrar) Ventana.onCerrar (Ventana);
		if (Ventana)
		{ document.body.removeChild (Ventana._oVentana);
			if (Ventana._oSombra) document.body.removeChild (Ventana._oSombra);
			if (Ventana._oBorde)  document.body.removeChild (Ventana._oBorde);
			document.body.removeChild (Ventana._oVelo);
		}
	}
}


TPopUp.prototype._Contenedor = function ()
{
	this._oCargando   = document.createElement ('img');
	this.Contenedor = document.createElement ('div');

	this.Contenedor.className = 'cssPopUp';
	this.Contenedor.setAttribute ('Capa', 'Contenedor');
	this.Contenedor.style.clear           = 'both';
	this.Contenedor.style.overflow        = 'hidden';
	this.Contenedor.style.fontSize        = 12;
	this.Contenedor.style.backgroundColor = this.ColorFondo;

	this._oCargando.style.padding     = '10px 40px';
	this._oCargando.src               = '/lib/ventanas/cargando.gif';

	this.Contenedor.appendChild (this._oCargando);
	this._oVentana.appendChild (this.Contenedor);
	this._Centrar ();
}


TPopUp.prototype._Create = function ()
{
	TPopUp.aVentanas.push (this);
	TPopUp._TamanoPagina ();

	this._Velo ();
	this._Sombra ();
	this._Borde ();
	this._Ventana ();
	this._Contenedor ();
}


TPopUp.prototype._PosBorde = function ()
{
	this._oBorde.style.left   = (this._oVentana.offsetLeft - 1) + 'px';
	this._oBorde.style.top    = (this._oVentana.offsetTop - 1) + 'px';
	this._oBorde.style.width  = (this._oVentana.offsetWidth) + 'px';
	this._oBorde.style.height = (this._oVentana.offsetHeight) + 'px';
}


TPopUp.prototype._PosSombra = function ()
{
	this._oSombra.style.left = (this._oVentana.offsetLeft + 3) + 'px';
	this._oSombra.style.top  = (this._oVentana.offsetTop + 3) + 'px';
	this._oSombra.style.width  = (this._oVentana.offsetWidth + 1) + 'px';
	this._oSombra.style.height = (this._oVentana.offsetHeight + 1) + 'px';
}


TPopUp.prototype._setAlpha = function (oElemento, iAlpha)
{
	oElemento.style.filter = 'alpha(opacity=' + iAlpha + ')';
	iAlpha /= 100;
	oElemento.style.opacity      = iAlpha;
	oElemento.style.MozOpacity   = iAlpha;
	oElemento.style.KHTMLOpacity = iAlpha;
}


TPopUp.prototype._Sombra = function ()
{
	this._oSombra  = document.createElement ('div');

	this._oSombra.setAttribute ('Capa', 'Sombra');
	this._setAlpha (this._oSombra, this.AlphaSombra);
	this._oSombra.style.backgroundColor = this.ColorVelo;
	this._oSombra.style.position        = 'absolute';
	this._oSombra.style.left            = '0px';
	this._oSombra.style.top             = '0px';
	this._oSombra.style.width           = '0px';
	this._oSombra.style.height          = '0px';
	this._oSombra.style.zIndex          = TPopUp._zIndex + 1;
	document.body.appendChild (this._oSombra);
}


TPopUp._TamanoPagina  = function ()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{	xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight)
	{ xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else
	{ xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	if (self.innerHeight)
	{	if(document.documentElement.clientWidth) TPopUp._AnchoVentana = document.documentElement.clientWidth;
		else TPopUp._AnchoVentana = self.innerWidth;
		TPopUp._AltoVentana = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight)
	{	TPopUp._AnchoVentana = document.documentElement.clientWidth;
		TPopUp._AltoVentana = document.documentElement.clientHeight;
	} else if (document.body)
	{ TPopUp._AnchoVentana = document.body.clientWidth;
		TPopUp._AltoVentana = document.body.clientHeight;
	}

	if(yScroll < TPopUp._AltoVentana) TPopUp._AltoPagina = TPopUp._AltoVentana;
	else TPopUp._AltoPagina = yScroll;

	if(xScroll < TPopUp._AnchoVentana) TPopUp._AnchoPagina = xScroll;
	else TPopUp._AnchoPagina = TPopUp._AnchoVentana;
}


TPopUp.prototype._Velo = function ()
{
	this._oVelo = document.createElement ('div');

	this._oVelo.setAttribute ('Capa', 'Velo');
	if (this.Alpha)	this._setAlpha (this._oVelo, this.Alpha);
	this._oVelo.style.backgroundColor = this.ColorVelo;
	this._oVelo.style.position        = 'absolute';
	this._oVelo.style.left            = '0px';
	this._oVelo.style.top             = '0px';
	this._oVelo.style.width           = '100%';
	this._oVelo.style.height          = TPopUp._AltoPagina + 'px';
	this._oVelo.style.zIndex          = TPopUp._zIndex;
	document.body.appendChild (this._oVelo);

	this._oVelo.oncontextmenu = function (oEvent) { if (oEvent) oEvent.preventDefault (); else window.event.returnValue = false; }
	TPopUp._addEvent (this._oVelo, 'click', function () { TPopUp.Cerrar (); });
}


TPopUp.prototype._Ventana = function ()
{
	this._oVentana = document.createElement ('div');

	this._oVentana.setAttribute ('Capa', 'Ventana');
	
	this._oVentana.style.borderWidth     = '1px';
	this._oVentana.style.borderStyle     = 'solid';
	this._oVentana.style.borderColor     = '#666666';
	this._oVentana.style.backgroundColor = this.ColorFondo;
	this._oVentana.style.width           = TPopUp._Ancho + 'px';
	this._oVentana.style.height          = TPopUp._Alto + 'px';	

	this._oVentana.style.position = 'absolute';
	this._oVentana.style.left     = '0px';
	this._oVentana.style.top      = '0px';
	this._oVentana.style.zIndex   = TPopUp._zIndex + 3;

	document.body.appendChild (this._oVentana);
}


function TPopUp ()
{
	TPopUp._zIndex   += 10;
	this.ColorFondo  = '#F9F9F9';
	this.ColorSombra = '#A1A1A1';
	this.ColorVelo   = '#E5E5E5';
	this.Vars        = new Object ();
}


/////////////////////////////////////////////////
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/////////////////////////////////////////////////




TPopUp.prototype.Cargar = function (sURL)
{
	var Ajax = new TAjax ();
	var self = this;

	this._Create ();
	if (sURL || this.Vars.Plantilla)
	{	Ajax.Vars     = this.Vars;
		Ajax.onCargar = function (oPeticion)
										{
											var Div   = document.createElement ('div');

											Div.className        = 'cssPopUp';
											Div.style.position   = 'absolute';
											Div.style.visibility = 'hidden';
											Div.style.top        = '0px';
											Div.style.left       = '0px';
											Div.innerHTML        = oPeticion.asText ();

											document.body.appendChild (Div);
											self._oVentana.style.width = (Div.offsetWidth + 16) + 'px';
											self._oVentana.style.height = Div.offsetHeight + 'px';
											self._Centrar ();
											document.body.removeChild (Div);
											Div.style.position   = 'static';
											Div.className        = '';
											Div.style.visibility = 'visible';
											self.Contenedor.removeChild (self._oCargando);
											self.Contenedor.appendChild (Div);
											if (self.onCargar) self.onCargar (self);
										}
		if (sURL) Ajax.Cargar (sURL);
		else Ajax.Cargar ('/js/php/plantilla.php');
		this.Vars = new Object ();
	} else
	{	this.Contenedor.removeChild (self._oCargando);
		if (this.onCargar) this.onCargar (this);
	}
}

