
TVentanas.aVentanas = Array ();

TVentanas._AltoPagina   = 0;
TVentanas._AltoVentana  = 0;
TVentanas._AnchoPagina  = 0;
TVentanas._AnchoVentana = 0;
TVentanas._oMover       = null;
TVentanas._iMoverX      = 0;
TVentanas._iMoverY      = 0;
TVentanas._zIndex       = 100;

TVentanas.prototype.Alpha        = 50;
TVentanas.prototype.AlphaSombra  = 50;
TVentanas.prototype.Alto         = 200;
TVentanas.prototype.Ancho        = 400;
TVentanas.prototype.BarraScroll  = false;
TVentanas.prototype.CerrarOnVelo = false;
TVentanas.prototype.ColorSombra  = '';
TVentanas.prototype.ColorVelo    = '';
TVentanas.prototype.Contenedor   = null;
TVentanas.prototype.marginBottom = 20;
TVentanas.prototype.marginLeft   = 20;
TVentanas.prototype.marginRight  = 20;
TVentanas.prototype.marginTop    = 20;
TVentanas.prototype.Tipo         = 1;
TVentanas.prototype.Titulo       = '';
TVentanas.prototype.Vars         = null;


TVentanas.prototype.onCargar = null;
TVentanas.prototype.onCerrar = null;

TVentanas.prototype._oBorde      = null;
TVentanas.prototype._oCargando   = null;
TVentanas.prototype._oIconos     = null;
TVentanas.prototype._oSombra     = null;
TVentanas.prototype._oTitulo     = null;
TVentanas.prototype._oTop        = null;
TVentanas.prototype._oVelo       = null;
TVentanas.prototype._oVentana    = null;



TVentanas.prototype._AjustarTitulo = function ()
{
	if ('\v' == 'v') this._oTop.style.width = (this._oVentana.offsetWidth - 2) + 'px';
	if (this._oTitulo) this._oTitulo.style.width = (this._oVentana.offsetWidth - this._oIconos.offsetWidth - 14) + 'px';
}


TVentanas.prototype._Borde = function ()
{
	if (this.Tipo == 1)
	{	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      = TVentanas._zIndex + 2;
		document.body.appendChild (this._oBorde);
	}
}


TVentanas.prototype._BotonCerrar = function ()
{
	var Boton = document.createElement ('div');
	
	Boton.style.position        = 'absolute';
	Boton.style.top             = '9px';
	Boton.style.left            = (this.Ancho + 1) + 'px';
	Boton.style.width           = '24px';
	Boton.style.height          = '24px';
	Boton.style.cursor          = 'pointer';
	Boton.style.backgroundColor = 'transparent';

	TVentanas._Evento (Boton, 'click', function () { TVentanas.Cerrar (); });	
	this._oVentana.appendChild (Boton);
}	


TVentanas.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.style.position   = 'absolute';
											Div.style.visibility = 'hidden';
											Div.style.top        = '0px';
											Div.style.left       = '0px';
											Div.innerHTML        = oPeticion.asText ();

											document.body.appendChild (Div);

											if (self.oTop)
											{	self._oTop.style.width    = Div.clientWidth + 'px';
												if (self_oTitulo) self._oTitulo.style.width = (self._oTop.offsetWidth - self._oIconos.offsetWidth - 14) + '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);
											self._AjustarTitulo ();
											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);
		this._AjustarTitulo ();
	}
}




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

	this._oVentana.style.left = ((TVentanas._AnchoPagina - this._oVentana.clientWidth) / 2) + 'px';
	this._oVentana.style.top  = (((TVentanas._AltoVentana - this._oVentana.clientHeight) / 2) + ScrollTop) + 'px';
	this._PosBorde (true);
	this._PosSombra (true);
}


TVentanas.Cerrar = function ()
{
	var Ventana = TVentanas.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);
		}
	}
}


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

	this.Contenedor.setAttribute ('Capa', 'Contenedor');
	this.Contenedor.style.backgroundColor = 'transparent';
	this.Contenedor.style.clear           = 'both';
	this.Contenedor.style.overflow        = this.BarraScroll ? 'auto' : 'hidden';
	this.Contenedor.style.marginTop       = this.marginTop + 'px';
	this.Contenedor.style.marginBottom    = this.marginBottom + 'px';
	this.Contenedor.style.marginLeft      = this.marginLeft + 'px';
	this.Contenedor.style.marginRight     = this.marginRight + 'px';
	this.Contenedor.style.width           = (this.Ancho - this.marginLeft - this.marginRight) + 'px';
	this.Contenedor.style.height          = (this.Alto - this.marginTop - this.marginBottom) + 'px';
	this.Contenedor.style.textAlign       = 'center';

	this._oCargando.style.padding = '10px 40px';
	this._oCargando.src           = '/js/imagenes/cargando.gif';
	this.Contenedor.appendChild (this._oCargando);
	this._oVentana.appendChild (this.Contenedor);
	this._AjustarTitulo ();

	this._Centrar ();
}


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

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

	this._Top ();
	this._Contenedor ();
}


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


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

	this._oIconos = document.createElement ('div');

	Icono.style.width         = '18px';
	Icono.style.height        = '18px';
	Icono.style.pointer       = 'cursor';
	Icono.style.background    = 'transparent url(/js/imagenes/cerrarventana.gif) no-repeat 0 center';
	Icono.style.paddingBottom = '3px';

	this._oIconos.appendChild (Icono);
	this._oIconos.style.height     = '18px';
	this._oIconos.style.styleFloat = 'right';
	this._oIconos.style.cssFloat   = 'right';
	TVentanas._Evento (this._oIconos, 'click', function () { TVentanas.Cerrar (); });
	this._oTop.appendChild (this._oIconos);
}


TVentanas._Mover = function (oEvento)
{
	TVentanas._oMover._oVentana.style.left = (oEvento.clientX + TVentanas._iMoverX) + 'px';
	TVentanas._oMover._oVentana.style.top  = (oEvento.clientY + TVentanas._iMoverY) + 'px';
	TVentanas._oMover._PosBorde (false);
	TVentanas._oMover._PosSombra (false);
}


TVentanas._NoMover = function (oEvento)
{
	if (document.body.removeEventListener)
	{	document.body.removeEventListener ('mousemove', TVentanas._Mover, false);
		document.body.removeEventListener ('mouseup', TVentanas._NoMover, false);
	} else if (document.body.detachEvent)
	{	document.body.detachEvent ('onmousemove', TVentanas._Mover);
		document.body.detachEvent ('onmouseup', TVentanas._NoMover);
	}
}


TVentanas.prototype._PosBorde = function (bRedimensionar)
{
	if (this._oBorde)
	{	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';
		}
	}
}


TVentanas.prototype._PosSombra = function (bRedimensionar)
{
	if (this._oSombra)
	{	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';
		}
	}
}


TVentanas.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;
}


TVentanas.prototype._Sombra = function ()
{
	if (this.Tipo == 1)
	{	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          = TVentanas._zIndex + 1;
		document.body.appendChild (this._oSombra);
	}
}


TVentanas._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) TVentanas._AnchoVentana = document.documentElement.clientWidth;
		else TVentanas._AnchoVentana = self.innerWidth;
		TVentanas._AltoVentana = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight)
	{	TVentanas._AnchoVentana = document.documentElement.clientWidth;
		TVentanas._AltoVentana = document.documentElement.clientHeight;
	} else if (document.body)
	{ TVentanas._AnchoVentana = document.body.clientWidth;
		TVentanas._AltoVentana = document.body.clientHeight;
	}

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

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


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

	this._oTitulo.style.color      = '#FFFFFF';
	this._oTitulo.style.fontSize   = '12px';
	this._oTitulo.style.fontFamily = 'tahoma,arial,verdana,sans-serif';
	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.innerHTML        = this.Titulo ? this.Titulo : '&nbsp;';

	TVentanas._Evento (this._oTitulo, 'mousedown', function (event) { TVentanas._TituloMouseDown (self, event); })
	this._oTop.appendChild (this._oTitulo);
}


TVentanas._TituloMouseDown = function (oVentana, oEvento)
{
	if (! oEvento) oEvento = window.event;

	if (oEvento.button == 0 || oEvento.button == 1)
	{	TVentanas._oMover  = oVentana;
		TVentanas._iMoverX = oVentana._oVentana.offsetLeft - oEvento.clientX;
		TVentanas._iMoverY = oVentana._oVentana.offsetTop - oEvento.clientY;

		if (document.body.addEventListener)
		{	document.body.addEventListener ('mouseup', TVentanas._NoMover, false);
			document.body.addEventListener ('mousemove', TVentanas._Mover, false);
		}	else if (document.body.attachEvent)
		{	document.body.attachEvent ('onmouseup', TVentanas._NoMover);
			document.body.attachEvent ('onmousemove', TVentanas._Mover);
		}

	}
}


TVentanas.prototype._Top = function ()
{
	var self = this;

	this._oTop = document.createElement ('div');

	this._oTop.setAttribute ('Capa', 'Top');
	this._oTop.style.height = '18px';
	this._oTop.style.width  = '\v' == 'v' ? '100px' : '100%';
	this._oTop.style.cursor = 'default';
	this._oVentana.appendChild (this._oTop);

	if (this.Tipo == 1)
	{ this._oTop.style.paddingBottom   = '3px';
		this._oTop.style.backgroundColor = '#666666';
		this._Iconos ();
		this._Titulo ();
	} else if (this.Tipo == 2)
	{	this._oTop.style.backgroundColor = 'transparent';
		TVentanas._Evento (this._oTop, 'mousedown', function (event) { TVentanas._TituloMouseDown (self, event); })
	}
}


TVentanas.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          = TVentanas._AltoPagina + 'px';
	this._oVelo.style.zIndex          = TVentanas._zIndex;
	document.body.appendChild (this._oVelo);

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


TVentanas.Ventana = function ()
{
	if (TVentanas.aVentanas.length) return TVentanas.aVentanas [TVentanas.aVentanas.length - 1];
	return null;
}


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

	this._oVentana.setAttribute ('Capa', 'Ventana');
	if (this.Tipo == 1)
	{	this._oVentana.style.borderWidth     = '1px';
		this._oVentana.style.borderStyle     = 'solid';
		this._oVentana.style.borderColor     = '#FFFFFF';
		this._oVentana.style.backgroundColor = '#FFFFFF';
		this._oVentana.style.width            = this.Ancho + 'px';
		this._oVentana.style.height           = (this.Alto + 18) + 'px';	
	} else if (this.Tipo == 2)
	{	this._oVentana.style.paddingLeft       = '18px';
		this._oVentana.style.backgroundColor  = 'transparent';
		this._oVentana.style.backgroundRepeat = 'no-repeat';
		this._oVentana.style.backgroundImage  = 'url(/js/php/fondo.php?Tipo=' + this.Tipo + '&Ancho=' + this.Ancho + '&Alto=' + this.Alto + ')'; 
		this._oVentana.style.width            = (this.Ancho + 36) + 'px';
		this._oVentana.style.height           = (this.Alto + 36) + 'px';	


		this._BotonCerrar ();
	}

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

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


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

	this.ColorVelo   = '#E5E5E5';
	this.ColorSombra = '#A1A1A1';
}


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

