////////////////////////////////////////Funciones para el manejo de ventanas /////////////////////////////////////
var gbFlagMove = false;
var gcWindow = null;
var gnStartX = 0; gnStartY = 0;

function createWindow( cWinId, cTitle , cContenido , nWidth , nHeight , nLeft , nTop, bModal )
	{
		oContenedor = document.getElementById(cWinId) ;
		// Si ya existe
		if ( oContenedor )
		{
			destroyWindow(cWinId);
			//return showWindow(cWinId);
		}
		// Obtiene variables del entorno
		var ie=document.all && !window.opera;
		var nDomclientWidth=document.documentElement && parseInt(document.documentElement.clientWidth) || 100000;
		oBody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body ;
		nScrollTop=(ie)? oBody.scrollTop : window.pageYOffset;
		nScrollLeft=(ie)? oBody.scrollLeft : window.pageXOffset;
		nDocWidth=(ie)? oBody.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(nDomclientWidth, window.innerWidth-16);
		nDocHeight=(ie)? oBody.clientHeight: window.innerHeight;
		nMaxheight=Math.max( oBody.offsetHeight , oBody.scrollHeight );

		nHeightTitulo = 17;
		nWidthBotonera = 15;
		if ( nWidth == undefined )  nWidth = 300;
		if ( nHeight  == undefined )  nHeight  = 300;
		// Si no viene el left /top => lo centra
		if ( nLeft == undefined ) nLeft = (nDocWidth - nWidth) / 2;
		if ( nTop == undefined ) nTop = nScrollTop  + (nDocHeight - nHeight ) / 2;
		// Crea el contenedor
		oContenedor = document.createElement('div');
		oContenedor.id =cWinId ;
		oContenedor.className = "window";
		oContenedor.style.width = nWidth+ "px";
		oContenedor.style.height = nHeight+ "px";
		oContenedor.style.left = nLeft + "px";
		oContenedor.style.top = nTop + "px";
		// Crea el titulo

		oTitulo = document.createElement('div');
		oTitulo.className = "title";
		oTitulo.style.width = (nWidth - nWidthBotonera)+ "px";
		oTitulo.style.height = nHeightTitulo+ "px";
		if (oTitulo.addEventListener)  oTitulo.addEventListener('mouseup',  function (e) { mouseUpWindow() }, true);
		else if (oTitulo.attachEvent) oTitulo.attachEvent('onmouseup',  function (e) { mouseUpWindow() });
		if (oTitulo.addEventListener)  oTitulo.addEventListener('mousedown', function (e) { return mouseDownWindow(e, cWinId) }, true);
		else if (oTitulo.attachEvent) oTitulo.attachEvent('onmousedown', function (e) { return mouseDownWindow(e, cWinId) });
		oTitulo.innerHTML = cTitle ;
		oContenedor.appendChild( oTitulo );
		// Crea la Botonera
		oBotonera = document.createElement('div');
		oBotonera.className = "buttons";
		oBotonera.style.width = (nWidthBotonera) + "px";
		oBotonera.style.height = nHeightTitulo+ "px";
		oBotonera.innerHTML = '<a href="javascript:void(0);" onclick="hideWindow(\'' + cWinId + '\');" >x</a></div>';
		oContenedor.appendChild( oBotonera );
		// Crea el body
		oBody = document.createElement('div');
		oBody.className = "inside";
		oBody.style.width = nWidth + "px";
		oBody.style.height = (nHeight - nHeightTitulo) + "px";
		oBody.innerHTML = cContenido;
		oContenedor.appendChild( oBody );

		if ( bModal  )
		{
			// Crea el contenedor
			oFondo = document.createElement('div');
			oFondo.id =cWinId + "_fondo" ;
			oFondo.className = "fondo";
			oFondo.style.height = nMaxheight + "px";

			//oFondo.appendChild( oContenedor );
			document.body.appendChild(oFondo);
		} else
		{

		}
		document.body.appendChild(oContenedor);
		//agregar un close con el Esc
		if (document.addEventListener)  document.addEventListener('keypress', function (e) { if (e.keyCode == 27) hideWindow( cWinId); }, true);
		else if (document.attachEvent) document.attachEvent('onkeypress', function (e) { if(e.keyCode == 27) hideWindow( cWinId); });
		// agrega los eventos al body
		if (document.body.addEventListener && !document.body.getAttribute('onmouseup')  )  document.body.addEventListener('mouseup',  function (e) { mouseUpWindow() }, true);
		else if (document.body.attachEvent) document.body.attachEvent('onmouseup',  function (e) { mouseUpWindow() });
		if (document.body.addEventListener && !document.body.getAttribute('onmousemove')  )  document.body.addEventListener('mousemove', function (e) { return mouseMoveWindow(e) }, true);
		else if (document.body.attachEvent) document.body.attachEvent('onmousemove', function (e) { return mouseMoveWindow(e) });
		if (document.body.addEventListener && !document.body.getAttribute('onmousedown')  )  document.body.addEventListener('mousedown', function (e) { if ( gbFlagMove == true && typeof e.preventDefault != 'undefined') {e.preventDefault(); } }, true);
		else if (document.body.attachEvent) document.body.attachEvent('onmousedown', function (e) { if ( gbFlagMove == true  && typeof e.preventDefault != 'undefined') {e.preventDefault();} });
	}
function showWindow(winId)
{
	oWindow = document.getElementById( winId );
	oWindow.style.visibility = 'visible';
}
function destroyWindow(winId)
{
	oFondo = document.getElementById(winId + "_fondo") ;
	if ( oFondo )
	{
		document.body.removeChild(oFondo);
	}
	oContenedor = document.getElementById(winId) ;
	if ( oContenedor )
	{
		document.body.removeChild(oContenedor);
	}
	gbFlagMove = false;
	gcWindow = null
}
function hideWindow(winId)
{
	oFondo = document.getElementById( winId + "_fondo");
	if ( oFondo ) oFondo.style.visibility = 'hidden';
	oWindow = document.getElementById( winId );
	if ( oWindow ) oWindow.style.visibility = 'hidden';
	gbFlagMove = false;
	gcWindow = null
}
function mouseDownWindow(e, winId)
{
	if (!e) var e = window.event;

	gbFlagMove = true;
	gcWindow = document.getElementById( winId );
	gnStartX = e.clientX - gcWindow.offsetLeft ;
	gnStartY = e.clientY - gcWindow.offsetTop;
}
function mouseUpWindow()
{
	gbFlagMove = false;
	gcWindow = null
}
function mouseMoveWindow(e)
{
	if ( !gbFlagMove || gcWindow == undefined ) return false;
	if (!e) var e = window.event;
	newX = e.clientX - gnStartX;
	newY = e.clientY - gnStartY;
	gcWindow.style.left = newX+ "px";
	gcWindow.style.top = newY + "px";
	return true;
}
