
TCapa.aVentanas = Array ();
TCapa._zIndex   = 100;

TCapa.prototype.Alpha       = 50;
TCapa.prototype.Alto        = 0;
TCapa.prototype.AlphaSombra = 50;
TCapa.prototype.Ancho       = 0;
TCapa.prototype.Scrolls     = true;
TCapa.prototype.Vars        = null;
TCapa.prototype.XY          = null;

TCapa.prototype.onCargar = null;
TCapa.prototype.onCerrar = null;

TCapa.prototype._iAlto       = 0;
TCapa.prototype._iAncho      = 0;
TCapa.prototype._oBorde      = null;
TCapa.prototype._oCargando   = null;
TCapa.prototype._oContenedor = null;
TCapa.prototype._aDim        = null;
TCapa.prototype._oSombra     = null;
TCapa.prototype._oVelo       = null;
TCapa.prototype._oVentana    = null;


function TCapa ()
{
	TCapa._zIndex += 10;
	this.Vars = new Object ();
}


TCapa.prototype._Capa = function (oElemento, sClase, sAncho, sAlto, iAlpha, izIndex)
{
	if (iAlpha)	this._setAlpha (oElemento, iAlpha);
	oElemento.className      = sClase;
	oElemento.style.position = 'absolute';
	oElemento.style.left     = '0px';
	oElemento.style.top      = '0px';
	oElemento.style.width    = sAncho;
	oElemento.style.height   = sAlto;
	oElemento.style.zIndex   = TCapa._zIndex + izIndex;
	if (oElemento != this._oVentana) document.body.appendChild (oElemento);
}


TCapa.prototype.Cargar = function (sURL)
{
	var Ajax = new TAjax ();

	if (sURL)
	{	Ajax.Vars     = this.Vars;
		Ajax._Ventana = this;
		Ajax.onCargar = this._CargarOn;
		this._Show ();
		Ajax.Cargar (sURL);
		this.Vars = new Object ();
	} else this._Show ();
}


TCapa.prototype._CargarOn = function (Peticion)
{
	Peticion._Ventana._CargarOn2 (Peticion);
}


TCapa.prototype._CargarOn2 = function (Peticion)
{
	var Div = document.createElement ('div');

	Div.className        = 'TPopUpVentana';
	Div.style.position   = 'absolute';
	Div.style.visibility = 'hidden';
	Div.style.top        = '0px';
	Div.style.left       = '0px';
	Div.innerHTML        = Peticion.asText ();
	Peticion.InHTML (Div);	

	document.body.appendChild (Div);
	
	this._iAlto  = this.Alto ? this.Alto : Div.clientHeight;
	this._iAncho = this.Ancho ? this.Ancho : Div.clientWidth;
	
	this._oContenedor.style.height = this._iAlto + 'px';
	this._oContenedor.style.width  = this._iAncho + 'px';
	
	this._doCargarOn ();
	document.body.removeChild (Div);
	Div.style.position   = 'static';
	Div.className        = '';
	Div.style.visibility = 'visible';
	this._oContenedor.removeChild (this._oCargando);
	this._oContenedor.appendChild (Div);
	if (this.onCargar) this.onCargar (this);
}


TCapa.Cerrar = function ()
{
	var Ventana = TCapa.aVentanas.pop ();
	
	if (Ventana)
	{	if (Ventana.onCerrar) Ventana.onCerrar (Ventana);
		if (Ventana)
		{ document.body.removeChild (Ventana._oVentana);
			document.body.removeChild (Ventana._oSombra);
			document.body.removeChild (Ventana._oBorde);
			document.body.removeChild (Ventana._oVelo);
		}
	}
}


TCapa.Contenedor = function ()
{
	if (TCapa.aVentanas.length) return TCapa.aVentanas [TCapa.aVentanas.length - 1]._oContenedor;
	return null;
}


TCapa.prototype._Contenedor = function ()
{
	this._oCargando   = document.createElement ('img');
	this._oContenedor = document.createElement ('div');
	
	this._oContenedor.className      = this._Clase + 'Contenedor';
	this._oContenedor.style.clear    = 'both';	
	this._oContenedor.style.overflow = this.Scrolls ? 'auto' : 'hidden';
	
	this._oCargando.style.padding = '10px 40px';
	this._oCargando.src           = '/js/cargando.gif';
	this._oContenedor.appendChild (this._oCargando);
	this._oVentana.appendChild (this._oContenedor);
	this._Centrar ();
}


TCapa.prototype._Create = function () 
{
	this._aDim     = PageSize ();
	this._oVelo    = document.createElement ('div');
	this._oSombra  = document.createElement ('div');
	this._oBorde   = document.createElement ('div');
	this._oVentana = document.createElement ('div');

	this._Capa (this._oVelo,    this._Clase + 'Velo', '100%', this._aDim [1] + 'px', this.Alpha, 0);
	this._Capa (this._oSombra,  this._Clase + 'Sombra', '0px', '0px', this.AlphaSombra, 1);
	this._Capa (this._oBorde,   this._Clase + 'Borde', '0px', '0px', 0, 2);
	this._Capa (this._oVentana, this._Clase + 'Ventana', '', '', 0, 3);
	this._MouseEventsVelo ();
	document.body.appendChild (this._oVentana);

	this._doCreate ();
	this._Contenedor ();
}


TCapa.prototype._doCargarOn = function (iAncho)
{
}


TCapa.prototype._doCreate = function ()
{
}


TCapa.prototype._MouseEventsVelo = function ()
{
	this._oVelo.oncontextmenu = function (oEvent) { if (oEvent) oEvent.preventDefault (); else window.event.returnValue = false; }
	if (this instanceof TPopUp)
	{	if (this._oVelo.addEventListener) this._oVelo.addEventListener ('click', function () { TCapa.Cerrar (); }, false);
		else this._oVelo.attachEvent ('onclick', function () { TCapa.Cerrar (); });
	}
}


TCapa.prototype._PosBorde = function (bRedimensionar)
{
	this._oBorde.style.left   = (this._oVentana.offsetLeft - 1) + 'px';
	this._oBorde.style.top    = (this._oVentana.offsetTop - 1) + 'px';
	if (bRedimensionar)
	{	this._oBorde.style.width  = (this._oVentana.clientWidth + 2) + 'px';
		this._oBorde.style.height = (this._oVentana.clientHeight + 2) + 'px';
	}
}


TCapa.prototype._PosSombra = function (bRedimensionar)
{
	this._oSombra.style.left = (this._oVentana.offsetLeft + 3) + 'px';
	this._oSombra.style.top  = (this._oVentana.offsetTop + 3) + 'px';
	if (bRedimensionar)
	{	this._oSombra.style.width  = (this._oVentana.clientWidth + 3) + 'px';
		this._oSombra.style.height = (this._oVentana.clientHeight + 3) + 'px';
	}
}


TCapa.prototype._setAlpha = function (Elemento, Alpha)
{
	Elemento.style.filter       = 'alpha(opacity=' + Alpha + ')';
	Alpha /= 100;
	Elemento.style.opacity      = Alpha;
	Elemento.style.MozOpacity   = Alpha;
	Elemento.style.KHTMLOpacity = Alpha;
}


TCapa.prototype._Show = function ()
{	
	TCapa.aVentanas.push (this);
	this._Create ();
}


//////////////////
//  TPopUp
//////////////////

TPopUp.prototype             = new TCapa ();
TPopUp.prototype.constructor = TPopUp;
TPopUp.prototype._Clase      = 'TPopUp';

function TPopUp ()
{
	TCapa.call (this);
	this.Alpha   = 1;
	this.Scrolls = false;
}


TPopUp.prototype._doCargarOn = function ()
{
	this._PosBorde (true);
	this._PosSombra (true);
}


//////////////////
//  TWindow
//////////////////

TWindow.prototype             = new TCapa ();
TWindow.prototype.constructor = TWindow;
TWindow.prototype._Clase      = 'TWindow';

TWindow._Activa    = null;
TWindow._XDif     = 0;
TWindow._YDif     = 0;

TWindow.prototype.Titulo    = '';
TWindow.prototype.VerTitulo = true;       

TWindow.prototype._oIconos = null;
TWindow.prototype._oTitulo = null;
TWindow.prototype._oTop    = null;


function TWindow ()
{
	TCapa.call (this);
}


TWindow.Cerrar = function () { TCapa.Cerrar (); }


TWindow.prototype._doCargarOn = function ()
{
	if (this.VerTitulo)
	{	this._oTop.style.width    = this._iAncho + 'px';
		this._oTitulo.style.width = (this._oTop.offsetWidth - this._oIconos.offsetWidth - 14) + 'px';
	}
	this._Centrar ();
}


TWindow.prototype._doCreate = function ()
{
	if (this.VerTitulo)
	{	this._oIconos = document.createElement ('div');
		this._oTop    = document.createElement ('div');	
		this._oTop.className   = 'TWindowTop';
		this._oTop.style.width = '100px';
		this._oVentana.appendChild (this._oTop);
		this._Iconos ();
		this._Titulo ();
	}
}


TWindow.prototype._Iconos = function ()
{
	var Icono  = document.createElement ('div');

	Icono.className = 'TWindowCerrar';
	this._oIconos.appendChild (Icono);
	this._oIconos.className        = 'TWindowIconos';
	this._oIconos.style.styleFloat = 'right';
	this._oIconos.style.cssFloat   = 'right';
	if (this._oIconos.addEventListener) this._oIconos.addEventListener ('click', TCapa.Cerrar, false);
	else if (this._oIconos.attachEvent) this._oIconos.attachEvent ('onclick', TCapa.Cerrar);
	this._oTop.appendChild (this._oIconos);
}


TWindow.prototype._Titulo = function ()
{
	var self   = this;
	this._oTitulo = document.createElement ('div');

	this._oTitulo.className        = 'TWindowTitulo';
	this._oTitulo.style.cssFloat   = 'left';
	this._oTitulo.style.styleFloat = 'left';
	this._oTitulo.style.overflow   = 'hidden';
	this._oTitulo.style.padding    = '3px 3px 0px 3px';
	this._oTitulo.style.whiteSpace = 'nowrap'; 
	this._oTitulo.style.width      = (this._oTop.offsetWidth - this._oIconos.offsetWidth - 14) + 'px';
	this._oTitulo.innerHTML        = this.Titulo ? this.Titulo : '&nbsp;';

	if (this._oTitulo.addEventListener)
		this._oTitulo.addEventListener ('mousedown', function (event) { self._TituloMouseDown (self, event); }, false);
	else if (this._oTitulo.attachEvent)
		this._oTitulo.attachEvent ('onmousedown', function () { self._TituloMouseDown (self, window.event); });
	this._oTop.appendChild (this._oTitulo);
}


TWindow.prototype._TituloMouseDown = function (oVentana, oEvento)
{
	if (oEvento.button == 0 || oEvento.button == 1)
	{	TWindow._Activa = oVentana;
		TWindow._XDif  = oVentana._oVentana.offsetLeft - oEvento.clientX;
		TWindow._YDif  = oVentana._oVentana.offsetTop - oEvento.clientY;
		if (document.body.addEventListener) 
		{	document.body.addEventListener ('mouseup', TWindow.NoMover, false);
			document.body.addEventListener ('mousemove', TWindow.Mover, false);
		}	else if (document.body.attachEvent)
		{	document.body.attachEvent ('onmouseup', TWindow.NoMover);
			document.body.attachEvent ('onmousemove', TWindow.Mover);
		}
	}
}


TWindow.Mover = function (oEvento)
{
	TWindow._Activa._oVentana.style.left = (oEvento.clientX + TWindow._XDif) + 'px';	
	TWindow._Activa._oVentana.style.top  = (oEvento.clientY + TWindow._YDif) + 'px';	
	TWindow._Activa._PosBorde (false);
	TWindow._Activa._PosSombra (false);
}


TWindow.NoMover = function (oEvento)
{
	if (document.body.removeEventListener)
	{	document.body.removeEventListener ('mousemove', TWindow.Mover, false);
		document.body.removeEventListener ('mouseup', TWindow.NoMover, false);
	} else if (document.body.detachEvent)
	{	document.body.detachEvent ('onmousemove', TWindow.Mover);
		document.body.detachEvent ('onmouseup', TWindow.NoMover);
	}
}


TWindow.prototype._TituloMouseDown = function (oVentana, oEvento)
{
	if (oEvento.button == 0 || oEvento.button == 1)
	{	TWindow._Activa = oVentana;
		TWindow._XDif  = oVentana._oVentana.offsetLeft - oEvento.clientX;
		TWindow._YDif  = oVentana._oVentana.offsetTop - oEvento.clientY;
		if (document.body.addEventListener) 
		{	document.body.addEventListener ('mouseup', TWindow.NoMover, false);
			document.body.addEventListener ('mousemove', TWindow.Mover, false);
		}	else if (document.body.attachEvent)
		{	document.body.attachEvent ('onmouseup', TWindow.NoMover);
			document.body.attachEvent ('onmousemove', TWindow.Mover);
		}
	}
}


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

TCapa.prototype._Centrar = function ()
{
	var ScrollTop = window.pageYOffset ? window.pageYOffset : Math.max(document.body.scrollTop, document.documentElement.scrollTop);

	this._oVentana.style.left = (this.XY && this.XY [0] ? this.XY [0] : ((this._aDim [0] - this._oVentana.clientWidth) / 2)) + 'px';
	this._oVentana.style.top  = (this.XY && this.XY [1] ? this.XY [1] : ((this._aDim [3] - this._oVentana.clientHeight) / 2) + ScrollTop) + 'px';
	this._PosBorde (true);
	this._PosSombra (true);
}

