///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
// 	JavaScript Document
// 	Informatica Siglo 21 : Derechos Reservados
// 	Código Generado por Ramiro Gomez Ortiz
// 	Area de Diseño IS21
// 	05 de Octubre de 2006
//	MODIFICADO EL 09 ENERO 2007
// 	Version 0.2
// 	Cualquier tipo de inquietud en la implementacion y en la generación de este script contactarse
// 	con el Area de diseño del IS21 areadiseno@i - siglo21.com
// 	http : // www.i - siglo21.com
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
// 	Crea un tooltip básico de texto y un cadena de HTML
var windowCounter = 0;
var initWin_x;
var initWin_y;
var windowCursor_x = 0;
var windowCursor_y = 0;
var windowObjectActive;
var windowIEComboBlockerActive;
var BlockerIntervalProperties;
var WindowIntervalPosition;
var fadeActions = false;
// STRINGS
var idWindowActive;
// BOOLEAN
var windowActive;
// ARRAYS
var windowsCreated = new Array();
//
var minZindex = -1;
var maxZindex;
var backZindex;
var IframeContent = false;
//
/*****************************************************************************
*METODO QUE CREA UNA VENTANA CON UN IFRAME EN SU INTERIOR PARA ACCEDER A OTRO URLS
******************************************************************************/
function createIS21IframeWindow(evt, name, src, footer, width, height, _x, _y, buttonHlp, buttonMin, buttonColse, block, functionResponse) {
	IframeContent = true;
	var iframewindDiv = createWindow(evt, name, src, footer, width, height, _x, _y, buttonHlp, buttonMin, buttonColse, block, functionResponse);
	return iframewindDiv;
}
/*****************************************************************************
*	METODO QUE CREA UNA VENTANA
*	@ PARAMS evt : EBVENTO
*	@ PARAMS name : NOMBRE DE LA VENTANA
*	@ PARAMS text : TEXTO INTERNO DE LA VENTANA
*	@ PARAMS footer : TEXTO QUE SE DESEA COLOCAR EN EL FOOTER
*	@ PARAMS width : ANCHO DE LA VENTANA
*	@ PARAMS height : ALTO DE LA VENTANA
*	@ PARAMS _x : POSICION EN X
*	@ PARAMS _y : POSICION EN Y
*	@ PARAMS buttonHlp : TRUE | NULL BOTON DE AYUDA
*	@ PARAMS buttonMin : TRUE | NULL BOTON DE MINIMIZAR
*	@ PARAMS buttonColse : TRUE | NULL BOTON DE CERRAR
*	@ PARAMS block : TRUE | FALSE BLOQUEAR EL FONDO DE LA APLICACION
*	@ PARAMS functionResponse : FUNCTION FUNCION DE RESPUESTA A CLICK EN LOS BOTONES CREADOS EN LA VENTANA
******************************************************************************/
function createWindow(evt, name, text, footer, width, height, _x, _y, buttonHlp, buttonMin, buttonColse, block, functionResponse) {
	document.body.style.cursor = 'wait';
	//
	windowCreator = document.createElement("div");
	windowCreator.id = 'window'+(windowsCreated.length);
	if (IE) {
		windowCreator.IEComboBlockerId = 'windowIEComboBlocker'+(windowsCreated.length);
		windowCreator.ModalBlockIEComboBlockerId = 'ModalBlockWindowIEComboBlocker'+(windowsCreated.length);
	}
	windowCreator.blockerId = 'ModalBlockWindow'+(windowsCreated.length);
	windowCreator.block = block;
	if (IframeContent) {
		windowCreator.urlIframe = text;
	}
	windowCreator.unselectable = 'on';
	windowCreator.footerText = footer;
	windowCreator.className = 'window_css';
	windowCreator.style.filter = 'alpha(opacity=0)';
	windowCreator.style.opacity = .0;
	if (width != null) {
		windowCreator.style.width = width+'px';
	} else {
		var w = getBrowserDimensiones()._width;
		windowCreator.style.width = w+'px';
	}
	if (height != null) {
		windowCreator.style.height = height+'px';
	} else {
		var h = getBrowserDimensiones()._height;
		windowCreator.style.height = h+'px';
	}
	//
	windowCreator.style.left = _x+'px';
	windowCreator.style.top = _y+'px';
	windowCreator.style.zIndex = (windowsCreated.length+1);
	windowCreator.active = true;
	//
	maxZindex = windowsCreated.length+1;
	//getCursorBrowserPosition(evt);
	//
	windowActive = true;
	windowObjectActive = windowCreator;
	// 
	if (!document.getElementById('styleWindow')) {
		createStyleWindow();
	}
	/*****************************************************************************
	*PARA INTERNET EXPLORER Y NO TENER PROBLEMAS CON LOS COMBOS TOCA REALIZAR ESTA ACCION ADICIONAL
	******************************************************************************/ 
	if (block) {
		createModalBlockWindow();
	}
	if (IE) {
		windowIEComboBlockerCreator = document.createElement("iframe");
		windowIEComboBlockerCreator.className = 'iframeSelectBlocker';
		windowIEComboBlockerCreator.id = 'windowIEComboBlocker'+(windowsCreated.length);
		windowIEComboBlockerCreator.src = 'blank.html';
		windowIEComboBlockerCreator.unselectable = 'on';
		windowIEComboBlockerCreator.style.left = _x+'px';
		windowIEComboBlockerCreator.style.top = _y+'px';
		windowIEComboBlockerCreator.style.filter = 'alpha(opacity=2)';
		windowIEComboBlockerCreator.style.opacity = 0.02;
		//
		windowIEComboBlockerActive = windowIEComboBlockerCreator;
		//
		document.body.appendChild(windowIEComboBlockerCreator);
		//
		rewriteBlokerIframe(windowIEComboBlockerCreator.id);
	}
	//  
	document.body.appendChild(windowCreator);
	//
	idWindowActive = windowCreator.id;
	windowsCreated.push(windowCreator);
	//
	initWin_x = parseInt(windowObjectActive.style.left);
	initWin_y = parseInt(windowObjectActive.style.top);
	//
	createToolBar();
	createTitleBar(name);
	if (!IframeContent) {
		createContentWindow(text);
	} else {
		IframeContent = false;
		createIS21IframeContentWindow(text);
	}
	createIS21ButtonsBar(buttonHlp, buttonMin, buttonColse, functionResponse);
	createFooterBar();
	//
	resizeContent();
	//
	fadeEnd = 100;
	windowCreator.style.filter = 'alpha(opacity='+fadeEnd+')';
	windowCreator.style.opacity = (fadeEnd/100);
	//
	return windowCreator;
}
/*****************************************************************************
*	METODO QUE AJUSTA EL TAMAÑO DE LA VENTANA
******************************************************************************/
function resizeContent() {
	windowObjectActive.content.style.height = parseInt(windowObjectActive.style.height)+'px';
	if (IE) {
		windowIEComboBlockerActive.style.height = parseInt(windowObjectActive.style.height)+20+'px';
	}
}
/*****************************************************************************
*	METODO QUE TRASLAPA LAS VENTANAS
*	@ PARAMS obj : VENTANA A TRASLAPAR
******************************************************************************/
function swapIndex(obj) {
	id = obj.id;
	var newzIndex = 0;
	for (var i = 0; i<windowsCreated.length; i++) {
		if (windowsCreated[i].id != id) {
			windowsCreated[i].style.zIndex = i;
		} else {
			obj.style.zIndex = windowsCreated.length+1;
		}
	}
}
/*****************************************************************************
*	METODO QUE CREA LA BARRA DE BOTONES DE LA VENTANA
******************************************************************************/
function createToolBar() {
	toolBarCreator = document.createElement("div");
	toolBarCreator.id = windowObjectActive.id+'_toolBar';
	toolBarCreator.style.zIndex = 100;
	toolBarCreator.unselectable = 'on';
	toolBarCreator.className = 'toolBar_css';
	//
	windowObjectActive.toolBar = new Object();
	windowObjectActive.toolBar = toolBarCreator;
	windowObjectActive.toolBar.padre = windowObjectActive;
	//
	windowObjectActive.appendChild(toolBarCreator);
}
/*****************************************************************************
*	METODO QUE CREA LA BARRA DE TITULO DE LA VENTANA
******************************************************************************/
function createTitleBar(name) {
	titleBarCreator = document.createElement("div");
	titleBarCreator.id = windowObjectActive.id+'_titleBar';
	titleBarCreator.unselectable = 'on';
	titleBarCreator.className = 'titleBar_css';
	//
	windowObjectActive.titleBar = new Object();
	windowObjectActive.titleBar = titleBarCreator;
	//
	windowObjectActive.toolBar.appendChild(titleBarCreator);
	//
	document.getElementById(titleBarCreator.id).innerHTML = name;
	windowObjectActive.toolBar.onmousedown = function(event) {
		if (!windowObjectActive.block) {
			swapIndex(this.padre);
		}
		initDrag(this.padre, event);
	};
	windowObjectActive.toolBar.onmouseup = function(event) {
		stopDrag();
	};
	windowObjectActive.toolBar.onmouseover = function(event) {
		windowObjectActive.toolBar.style.cursor = 'move';
	};
}
/*****************************************************************************
*	METODO QUE CREA EL FOOTER DE LA VENTANA
******************************************************************************/
function createFooterBar() {
	footerBarCreator = document.createElement("div");
	footerBarCreator.id = windowObjectActive.id+'_footerBar';
	footerBarCreator.unselectable = 'on';
	footerBarCreator.className = 'footerBar_css';
	footerBarCreator.style.top = windowObjectActive.style.height;
	//
	windowObjectActive.footer = new Object();
	windowObjectActive.footer = footerBarCreator;
	//
	windowObjectActive.appendChild(footerBarCreator);
	//
	document.getElementById(footerBarCreator.id).innerHTML = windowObjectActive.footerText;
	document.body.style.cursor = 'default';
}
/*****************************************************************************
*	METODO QUE CREA LOS BOTONES DE LA VENTANA
******************************************************************************/
function createIS21ButtonsBar(hlp, minimizar, cerrar, functionResponse) {
	buttonsBarCreator = document.createElement("div");
	buttonsBarCreator.id = windowObjectActive.id+'_btn';
	buttonsBarCreator.className = 'buttonBar_css';
	//
	windowObjectActive.buttonBar = new Object();
	windowObjectActive.buttonBar = buttonsBarCreator;
	//
	windowObjectActive.toolBar.appendChild(buttonsBarCreator);
	//
	buttonsBar = document.getElementById(buttonsBarCreator.id);
	//
	if (hlp != null) {
		buttonsHLPCreator = document.createElement("div");
		buttonsHLPCreator.id = windowObjectActive.id+'_HLPbtn';
		buttonsHLPCreator.className = 'HLPbtn_css';
		//
		windowObjectActive.hlpButton = new Object();
		windowObjectActive.hlpButton = buttonsHLPCreator;
		//
		windowObjectActive.hlpButton.onmouseover = function(event) {
			this.style.cursor = 'help';
		};
		windowObjectActive.hlpButton.onclick = function(event) {
			functionResponse();
		};
		//
		buttonsBar.appendChild(buttonsHLPCreator);
	}
	//     
	if (minimizar != null) {
		buttonsMINCreator = document.createElement("div");
		buttonsMINCreator.id = windowObjectActive.id+'_MINbtn';
		buttonsMINCreator.className = 'MINbtn_css';
		//
		windowObjectActive.minButton = new Object();
		windowObjectActive.minButton = buttonsMINCreator;
		//
		windowObjectActive.minButton.onmouseover = function(event) {
			this.style.cursor = 'pointer';
		};
		//
		windowObjectActive.minButton.onclick = minimizarVentana;
		//
		buttonsBar.appendChild(buttonsMINCreator);
	}
	//     
	if (cerrar != null) {
		buttonsCLOSECreator = document.createElement("div");
		buttonsCLOSECreator.id = windowObjectActive.id+'_CLOSEbtn';
		buttonsCLOSECreator.className = 'CLOSEbtn_css';
		//
		windowObjectActive.closeButton = new Object();
		windowObjectActive.closeButton = buttonsCLOSECreator;
		//
		windowObjectActive.closeButton.onmouseover = function(event) {
			this.style.cursor = 'pointer';
		};
		windowObjectActive.closeButton.onclick = function(event) {
			closeWindow(windowObjectActive.id);
			functionResponse();
		};
		//
		buttonsBar.appendChild(buttonsCLOSECreator);
	}
}
/*****************************************************************************
*	METODO QUE MINIMIZA LA VENTANA
******************************************************************************/
function minimizarVentana(event) {
	var aa = getBrowserDimensiones();
	windowObjectActive.style.left = 0;
	windowObjectActive.style.top = aa._height-windowObjectActive.toolBar.style.height;
	if (IE) {
		windowIEComboBlockerActive.style.left = 0;
		windowIEComboBlockerActive.style.top = aa._height-windowObjectActive.toolBar.style.height;
	}
}
/*****************************************************************************
*	METODO QUE CREA EL CONTENIDO DEL IFRAME
******************************************************************************/
function createIS21IframeContentWindow(src) {
	contentCreator = document.createElement("div");
	contentCreator.align = 'center';
	contentCreator.id = windowObjectActive.id+'_content';
	contentCreator.className = 'content_css';
	windowObjectActive.appendChild(contentCreator);
	//
	windowObjectActive.content = new Object();
	windowObjectActive.content = contentCreator;
	//
	contentIframeCreator = document.createElement("iframe");
	contentIframeCreator.id = windowObjectActive.id+'_contentIframe';
	contentIframeCreator.src = src;
	contentIframeCreator.style.width = (parseInt(windowObjectActive.style.width)-20)+'px';
	contentIframeCreator.style.height = windowObjectActive.style.height;
	contentIframeCreator.scrolling = "yes";
	contentIframeCreator.frameborder = "0";
	contentIframeCreator.className = 'contentIframe_css';
	windowObjectActive.content.appendChild(contentIframeCreator);
	//
	//
	windowObjectActive.contentIframe = new Object();
	windowObjectActive.contentIframe = contentIframeCreator;
	//
	document.getElementById(contentCreator.id).innerHTML = document.getElementById(contentCreator.id).innerHTML+'';
}
/*****************************************************************************
*	METODO QUE CREA EL CONTENIDO DE LA VENTANA
******************************************************************************/
function createContentWindow(text) {
	contentCreator = document.createElement("div");
	contentCreator.id = windowObjectActive.id+'_content';
	contentCreator.className = 'content_css';
	windowObjectActive.appendChild(contentCreator);
	//
	windowObjectActive.content = new Object();
	windowObjectActive.content = contentCreator;
	document.getElementById(contentCreator.id).innerHTML = text;
}

/*****************************************************************************
*	METODO QUE DESIGNA LA POSICION DE LA VENTANA
******************************************************************************/
function setWindowPosition(e) {
	if (windowActive) {
		getPositionToolTip(e);
		var newPosX = getCursorBrowserPosition(e)._x;
		var newPosY = getCursorBrowserPosition(e)._y;
		//
		objectToMove = windowObjectActive;
		ancho = objectToMove.style.width;
		alto = objectToMove.style.height;
		limiteAncho = getBrowserDimensiones()._width;
		limiteAlto = getBrowserDimensiones()._height;
		objectToMove.style.left = (newPosX+15)+'px';
		objectToMove.style.top = newPosY+'px';
	}
	return true;
}
/*****************************************************************************
*	METODO QUE CREA LA VENTANA MODAL
******************************************************************************/
function createModalBlockWindow() {
	BlockModalDiv = document.createElement("div");
	BlockModalDiv.id = 'ModalBlockWindow'+(windowsCreated.length);
	BlockModalDiv.className = 'ModalBlockWindow';
	BlockModalDiv.style.zIndex = windowsCreated.length;
	if (IE) {
		BlockModalIframeDiv = document.createElement("iframe");
		BlockModalIframeDiv.id = 'ModalBlockWindowIEComboBlocker'+(windowsCreated.length);
		BlockModalIframeDiv.className = 'ModalBlockWindow';
		BlockModalIframeDiv.src = 'blank.html';
		BlockModalIframeDiv.style.filter = 'alpha(opacity=2)';
		BlockModalIframeDiv.style.opacity = .02;
		BlockModalIframeDiv.style.zIndex = windowsCreated.length;
		document.body.appendChild(BlockModalIframeDiv);
		//
		rewriteBlokerIframe(BlockModalIframeDiv.id);
	}
	document.body.appendChild(BlockModalDiv);
	//
	resizeModalBlockWindow(BlockModalDiv.id);
	if (IE) {
		resizeModalBlockWindow(BlockModalIframeDiv.id);
	}
	//  
	BlockerIntervalProperties = setInterval(resizeModalBlockWindow, 10);
}
/*****************************************************************************
*	METODO QUE CREA EL CONTENIDO DEL IFRAME COMO MODAL
******************************************************************************/
function rewriteBlokerIframe(id) {
	var oRTE = document.getElementById(id).contentWindow.document;
	var html = 'Informatica Siglo 21: BlokerIframe to WindowDiv Component : V 0.2 <a href="http://www.i-siglo21.com">WEBSITE : informática Siglo 21</a>';
	oRTE.open();
	oRTE.write(html);
	oRTE.close();
}
/**************************************************************************************
* Metodo que Reestablece las dimensiones del Block a las dimensiones del 
* area disponible por el browser
**************************************************************************************/
function resizeModalBlockWindow() {
	document.getElementById(BlockModalDiv.id).style.height = getBrowserDimensiones()._height+'px';
	document.getElementById(BlockModalDiv.id).style.width = getBrowserDimensiones()._width+'px';
	document.getElementById(BlockModalDiv.id).style.left = '0px';
	document.getElementById(BlockModalDiv.id).style.top = '0px';
	//
	if (IE) {
		document.getElementById(BlockModalIframeDiv.id).style.height = getBrowserDimensiones()._height+'px';
		document.getElementById(BlockModalIframeDiv.id).style.width = getBrowserDimensiones()._width+'px';
		document.getElementById(BlockModalIframeDiv.id).style.left = '0px';
		document.getElementById(BlockModalIframeDiv.id).style.top = '0px';
	}
}
/**************************************************************************************
* Metodo que crea un estilo default para la ventana que se esta creando
**************************************************************************************/
function createStyleWindow() {
	//
	estilo = document.createElement("span");
	estilo.id = 'styleWindow';
	estilo.style.display = 'none';
	var stylus = '&nbsp;ShowStyle<style>.titleBar{z-index:1;}.window_css {position:absolute;}.ModalBlockWindow{position:absolute;z-index:1;}</style>';
	estilo.innerHTML = stylus;
	document.body.appendChild(estilo);
}
/**************************************************************************************
* Metodo que inicia el Drag de la ventana
**************************************************************************************/
function initDrag(obj, e) {
	windowObjectActive = obj;
	if (IE) {
		windowIEComboBlockerActive = document.getElementById(obj.IEComboBlockerId);
	}
	initWin_x = getCursorBrowserPosition(e)._x-parseInt(windowObjectActive.style.left);
	initWin_y = getCursorBrowserPosition(e)._y-parseInt(windowObjectActive.style.top);
	//
	obj.onmousemove = startDrag
	obj.onmouseup = stopDrag;
}
/**************************************************************************************
* Metodo que inicia el Drag de la ventana
**************************************************************************************/
function startDrag(e) {
	newPosX = getCursorBrowserPosition(e)._x-initWin_x;
	newPosY = getCursorBrowserPosition(e)._y-initWin_y;
	if (!IE) {
		windowObjectActive.style.opacity = .6;
	}
	//  
	if (newPosX>=0) {
		if (IE) {
			windowIEComboBlockerActive.style.left = newPosX+'px';
		}
		windowObjectActive.style.left = newPosX+'px';
	} else {
		if (IE) {
			windowIEComboBlockerActive.style.left = '0px';
		}
		windowObjectActive.style.left = '0px';
	}
	if (newPosY>=0) {
		if (IE) {
			windowIEComboBlockerActive.style.top = newPosY+'px';
		}
		windowObjectActive.style.top = newPosY+'px';
	} else {
		if (IE) {
			windowIEComboBlockerActive.style.top = '0px';
		}
		windowObjectActive.style.top = '0px';
	}
}
/**************************************************************************************
* Metodo que setiene el Drag de la ventana.
**************************************************************************************/
function stopDrag() {
	windowObjectActive.onmousemove = '';
	windowObjectActive.style.filter = 'alpha(opacity=100)';
	windowObjectActive.style.opacity = 1;
	windowObjectActive.footer.innerHTML = windowObjectActive.footerText;
}
/**************************************************************************************
* Metodo que cierra la ventana haya sido creada en el DOM
**************************************************************************************/
function closeWindow(id) {
	var objEliminar = document.getElementById(id);
	objEliminar.style.filter = 'alpha(opacity='+fadeEnd+')';
	objEliminar.style.opacity = (fadeEnd/100);
	deleteWindow(id);
}
/**************************************************************************************
* Metodo que elimina la ventana que haya sido creada en el DOM
**************************************************************************************/
function deleteWindow(id) {
	clearInterval(BlockerIntervalProperties);
	if (id == null) {
		id = windowObjectActive.id;
	}
	if (document.getElementById(id) && document.getElementById('styleWindow')) {
		document.getElementById(id).innerHTML = '';
		var removeChildItem = document.getElementById(id);
		document.body.removeChild(removeChildItem);
		if (IE) {
			var removeChildIEIframeBlockerItem = document.getElementById(removeChildItem.IEComboBlockerId);
			document.body.removeChild(removeChildIEIframeBlockerItem);
		}
		//   
		if (document.getElementById(removeChildItem.blockerId)) {
			var removeBlockItem = document.getElementById(removeChildItem.blockerId);
			document.body.removeChild(removeBlockItem);
			//
			if (IE) {
				var removeChildIframeBlock = document.getElementById(removeChildItem.ModalBlockIEComboBlockerId);
				document.body.removeChild(removeChildIframeBlock);
			}
			//  
		}
	}
	windowsCreatedTmp = new Array();
	for (var i = 0; i<windowsCreated.length; i++) {
		if (windowsCreated[i].id != id) {
			windowsCreatedTmp.push(windowsCreated[i]);
		}
	}
	windowsCreated = new Array();
	windowsCreated = windowsCreatedTmp;
	if (windowsCreated.length>0) {
		var longitud = windowsCreated.length-1;
		windowObjectActive = windowsCreated[longitud];
		idWindowActive = windowsCreated[longitud].id;
	} else {
		var removeStyleItem = document.getElementById('styleWindow');
		document.body.removeChild(removeStyleItem);
	}
}
