
//
//	12/05/2010	Se cambia la ubicación de plantilla.php
//	15/05/2010	Se cambia CerrarOnVelo por CerrarEnVelo
//

TVentanas.aVentanas = Array ();

TVentanas._AltoPagina   = 0;
TVentanas._AltoVentana  = 0;
TVentanas._AnchoPagina  = 0;
TVentanas._AnchoVentana = 0;
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.CerrarEnVelo = false;
TVentanas.prototype.ColorSombra  = '';
TVentanas.prototype.ColorVelo    = '';
TVentanas.prototype.Contenedor   = null;
TVentanas.prototype.Tipo         = 1;
TVentanas.prototype.Titulo       = '';
TVentanas.prototype.Vars         = null;


TVentanas.prototype._oMover      = 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._Ajax = function (sURL)
{
	var aVersiones =  ['Microsoft.XMLHttp', 'MSXML2.XMLHttp', 'MSXML2.XMLHttp.3.0',
	                   'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.5.0'];
	var self       = this;
	var Pars       = new Array ();
	var oAjax      = null;

	if (window.XMLHttpRequest) 
  {	try
   	{	oAjax = new XMLHttpRequest ();
   	} catch(e)
   	{	oAjax = null;
   	}
  } else if (window.ActiveXObject) 
	{	for  (var i = aVersiones.length - 1; i >= 0 && ! oAjax; i--)
		{	try
			{	oAjax = new ActiveXObject (aVersiones [i]);
		  } catch (E)
      {	oAjax = null;
    	}
    }
  }

	if (oAjax)
	{	oAjax.onreadystatechange = function ()
		{	switch (oAjax.readyState)
			{	case 0:		// Sin inicializar
					break;
				case 1:		// Cargando
					break;
				case 2:		// Cargado
					break;
				case 3:		// Interactivo
					break;
				case 4:		// Completado
					if (oAjax.status == 200) self._onCargar (oAjax.responseText);	// SI LA PETICION TIENE EXITO
					oAjax = null;
					break;
			}
		}
	}

	for (var Par in this.Vars) Pars.push (escape (Par) + '=' + escape (this.Vars [Par]));

	oAjax.open ('POST', sURL ? sURL : '/lib/ventanas/plantilla.php', true);
	oAjax.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	oAjax.send (Pars.join ('&'));
}


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._BotonesTipo2 = function ()
{
	var Boton = this._Capa (9, this.Ancho + 15, 24, 24);
	Boton.style.cursor = 'pointer';
	TVentanas._Evento (Boton, 'click', function () { TVentanas.Cerrar (); });	
	this._oVentana.appendChild (Boton);
}	


TVentanas.prototype._Capa = function (iTop, iLeft, iAncho, iAlto)
{
	var Boton = document.createElement ('div');
	
	Boton.style.position        = 'absolute';
	Boton.style.top             = iTop + 'px';
	Boton.style.left            = iLeft + 'px';
	Boton.style.width           = iAncho + 'px';
	Boton.style.height          = iAlto + 'px';
	Boton.style.backgroundColor = 'transparent';
//	Boton.style.backgroundColor = 'red'; 'transparent';
//	this._setAlpha (Boton, 50);
	return Boton;
}


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 (oCerrar)
{
	var ventana = TVentanas.aVentanas.length ? TVentanas.aVentanas [TVentanas.aVentanas.length - 1] : null;

	if (ventana)
	{	if (typeof oCerrar == 'undefined') oCerrar = {};
		oCerrar.ventana = ventana;
		oCerrar.cerrar  = true;
		if (ventana.onCerrar) ventana.onCerrar (oCerrar);
		if (oCerrar.cerrar)
		{	TVentanas.aVentanas.pop ();
			ventana._oVentana.style.display = 'none';
			if (ventana._oSombra) ventana._oSombra.style.display = 'none';
			if (ventana._oBorde) ventana._oBorde.style.display = 'none';
			ventana._oVelo.style.display = 'none';
			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.textAlign       = 'center';
	this.Contenedor.style.position        = 'absolute';
	this._Margenes ();

	this._oCargando.src           = '/lib/ventanas/cargando.gif';
	this.Contenedor.appendChild (this._oCargando);
	this._oVentana.appendChild (this.Contenedor);
	this._AjustarTitulo ();

	this._Centrar ();
}


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(/lib/ventanas/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.prototype._Margenes = function ()
{
	if (this.Tipo == 3)
	{	this.Contenedor.style.left   = '14px';
		this.Contenedor.style.top    = '46px';
		this.Contenedor.style.width  = this.Ancho + 'px';
		this.Contenedor.style.height = (this.Alto + 2) + 'px';
	} else
	{	this.Contenedor.style.left   = '25px';
		this.Contenedor.style.top    = '30px';
		this.Contenedor.style.width  = this.Ancho + 'px';
		this.Contenedor.style.height = this.Alto + 'px';
	}
}


TVentanas.prototype._onCargar = function (sContenido)
{
	var Div   = document.createElement ('div');

	Div.style.position   = 'absolute';
	Div.style.visibility = 'hidden';
	Div.style.top        = '0px';
	Div.style.left       = '0px';
	Div.innerHTML        = sContenido;

	document.body.appendChild (Div);
		
	if (this.oTop)
	{	this._oTop.style.width    = Div.clientWidth + 'px';
		if (this._oTitulo) this._oTitulo.style.width = (this._oTop.offsetWidth - this._oIconos.offsetWidth - 14) + 'px';
	}
	this._Centrar ();
	document.body.removeChild (Div);

	Div.style.position   = 'static';
	Div.className        = '';
	Div.style.visibility = 'visible';
	this.Contenedor.removeChild (this._oCargando);
	this.Contenedor.appendChild (Div);
	this._AjustarTitulo ();
	if (this.onCargar) this.onCargar (this);
}


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.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.CerrarEnVelo) TVentanas._Evento (this._oVelo, 'click', function () { TVentanas.Cerrar (); });
}


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


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

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


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


TVentanas.prototype.Cargar = function (sURL)
{
	this._Create ();
	if (sURL || this.Vars.Plantilla) this._Ajax (sURL);
	else
	{	this.Contenedor.removeChild (this._oCargando);
		if (this.onCargar) this.onCargar (this);
		this._AjustarTitulo ();
	}
}

TVentanas.prototype._Create = function ()
{
	if (this.Tipo == 2 && this.Ancho < 100) this.Ancho = 100;
	if (this.Tipo == 2 && this.Alto < 100) this.Alto = 100;
	if (this.Tipo == 3 && this.Ancho < 580) this.Ancho = 580;
	if (this.Tipo == 3 && this.Alto < 130) this.Alto = 130;
	
	TVentanas.aVentanas.push (this);
	TVentanas._TamanoPagina ();

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

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

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.backgroundColor  = 'transparent';
		this._oVentana.style.backgroundRepeat = 'no-repeat';
		this._oVentana.style.backgroundImage  = 'url(/lib/ventanas/fondo.php?Tipo=' + this.Tipo + '&Ancho=' + this.Ancho + '&Alto=' + this.Alto + ')'; 
		this._oVentana.style.width            = (this.Ancho + 25 + 25) + 'px';
		this._oVentana.style.height           = (this.Alto + 30 + 25) + 'px';	
		this._BotonesTipo2 ();
	} else if (this.Tipo == 3)
	{	this._oVentana.style.backgroundColor  = 'transparent';
		this._oVentana.style.backgroundRepeat = 'no-repeat';
		this._oVentana.style.backgroundImage  = 'url(/lib/ventanas/fondo.php?Tipo=' + this.Tipo + '&Ancho=' + this.Ancho + '&Alto=' + this.Alto + ')'; 
		this._oVentana.style.width            = (this.Ancho + 14 + 14) + 'px';
		this._oVentana.style.height           = (this.Alto + 46 + 67) + 'px';	
		this._BotonesTipo3 ();
	}

	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);
}


TVentanas.prototype._BotonesTipo3 = function ()
{
	var Boton = this._Capa (18, this.Ancho -12, 24, 24);
	
	Boton.style.cursor = 'pointer';
	TVentanas._Evento (Boton, 'click', function () { TVentanas.Cerrar (); });	
	this._oVentana.appendChild (Boton);
	
	Boton    = this._Capa (this.Alto + 61, this.Ancho - 126, 128, 28);
	Boton.id = 'id3Capturar';
	Boton.style.cursor = 'pointer';
	this._oVentana.appendChild (Boton);

	Boton    = this._Capa (this.Alto + 57, this.Ancho - 290, 162, 37);
	Boton.id = 'id3Cargar';
	Boton.style.cursor = 'pointer';
	this._oVentana.appendChild (Boton);
	
	Boton    = this._Capa (this.Alto + 61, 45, 151, 32);
	Boton.id = 'id3Zoom';
	this._oVentana.appendChild (Boton);
}	


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._EventosMover ();
	} else if (this.Tipo == 3)
	{	this._oTop.style.backgroundColor = 'transparent';
		this._oTop.style.height = '46px';
//		TVentanas._EventosMover ();
	}
}

/*
TVentanas._EventosMover = function ()
{

if ($ ('idCesar')) $ ('idCesar').innerHTML += ' EMover';	

	if (document.addEventListener)
	{	
//		document.addEventListener ('mousemove', TVentanas.MouseMove, false);
		document.addEventListener ('mousedown', TVentanas.MouseDown, false);
		document.addEventListener ('mouseup',   TVentanas.MouseUp, false);
	} else if (document.attachEvent)
	{	
//		document.attachEvent ('onmousemove', TVentanas.MouseMove);
		document.attachEvent ('onmousedown', TVentanas.MouseDown);
		document.attachEvent ('onmouseup',   TVentanas.MouseUp);
	}
}


TVentanas.MouseDown = function (oEvento)
{
//	if (! oEvento) oEvento = window.event;
//	var oElemento = oEvento.target || oEvento.srcElement;
//	var Ventana   = TVentanas.Ventana ();

$ ('idCesar').innerHTML += ' DOWN';	


//	if (oElemento == oVentana._oTop)
//	{	oVentana._oMover = oVentana;

	if ((oEvento.button == 0 || oEvento.button == 1) && oElemento == Ventana._oTop)
//	if (oEvento.button == 0 || oEvento.button == 1)
	{	

		
		Ventana._oMover  = Ventana;

		TVentanas._iMoverX = Ventana._oVentana.offsetLeft - oEvento.clientX;
		TVentanas._iMoverY = Ventana._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.MouseMove = function (oEvento)
{

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

}

TVentanas.MouseUp = function (oEvento)
{
//	var Ventana = TVentanas.Ventana ();

$ ('idCesar').innerHTML += ' UP';	

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

*/

