/*
 *
 *---- Solo tenemos 3 layers:
 *
 *     ========= TOP ============= >
 *     --------------------------- > CONFIRM
 *     ======= VENTANA INLINE ==== >    ķ (No incluyente es uno u otro layer)
 *     --------------------------- > CONFIRM
 *
 *
 *
 *
 * - El top ni lo movemos
 * - La ventana inline puede ser un iframe o html
 * - El confirm es html
 *
 *
 *
 */

function showConfirm (msg, opciones, tipo)
{
	html  = msg;

	top.confirm_result = false;

	//-- Poner el msg
	//-- Poner las opciones
	//-- Poner el tipo

	if (!opciones) {
		opciones = new Array (new Array ('Aceptar', true), new Array ('Cancelar', false));
	}

	top.opciones_confirm = opciones;

	var select_id = 0;

	for (var count = 0; count < opciones.length; count++) {
		var id = 'id_opcion_'+count;
		html += "<input type='button' id='"+id+"' value='"+opciones [count][0] +"' onclick='hideVentana ();	top.confirm_result = top.opciones_confirm ["+count+"][1];window.catchTeclado = null;'/>";

		if (opciones [count] [1] === true) {
			select_id = count;
		}
	}

	initElementos ();

	gotoGhostUrl (top.document.getElementById("iframe_inline"), 'about:blank');

	//Movemos el inline y el progress atras de la transparencia
	top.document.getElementById ('div_progress').style.zIndex = top.document.getElementById ('transparencia').style.zIndex - 1;
	top.document.getElementById ('div_inline').style.zIndex   = top.document.getElementById ('transparencia').style.zIndex - 1;

    top.showTransparencia ();
    top.document.getElementById ('div_confirm').innerHTML = html;
    top.showElemento ("div_confirm");
    top.disableSelects();

	top.in_confirm = true;

	window.catchTeclado =
		function (tecla) {
			switch (tecla) {
				//Escape
				case 27:
					hideVentana ();
					window.catchTeclado = null;
					break;
				//Arriba, Izquierda
				case 37:
				case 38:
					if (top.confirm_select_id == 0) {
						top.confirm_select_id = top.opciones_confirm.length - 1;
					} else {
						top.confirm_select_id--;
					}
					document.getElementById ('id_opcion_'+top.confirm_select_id).focus ();
					break;
				//Abajo, Derecha, Tab
				case 40:
				case 39:
				case 9:
					if (top.confirm_select_id == (top.opciones_confirm.length - 1)) {
						top.confirm_select_id = 0;
					} else {
						top.confirm_select_id++;
					}

					setTimeout ("document.getElementById ('id_opcion_'+top.confirm_select_id).focus ()", 100);
					break;
			}
		}

	document.getElementById ('id_opcion_'+select_id).focus ();

	top.confirm_select_id = select_id;
}

function showVentana( url, is_html) {

	if (top.inline_window_type == 'iframe') {
		//Tenemos un problema serio de usabilidad
		_w = showPopup (url);
		_w.window_target = top.window_target;
	} else {
		if (is_html && is_html == true) {
			startProgress (url);
   			top.inline_window_type    = 'html';
		} else {
		    top.showTransparencia ();

     		gotoGhostUrl (top.document.getElementById("iframe_inline"), url);

	        var root = (top.document.compatMode == "CSS1Compat" ? top.document.documentElement: top.document.body)
    		top.document.getElementById ("div_inline").style.top = root.scrollTop + "px";

			top.document.getElementById ("div_inline").style.display = '';
	  		top.inline_window_type    = 'iframe';
		}

		top.inline_uri = url;
	}

	window.catchTeclado =
		function (tecla) {
			switch (tecla) {
				//Escape
				case 27:
					hideVentana ();
					window.catchTeclado = null;
					break;
			}
	}


}

function manageUnload (e)
{
	if (top.inline_window_type !== null || top.in_confirm) {
		top.hideVentana ();
		return false;
//		top.window.open(top.location);
	} else {
	//	return true;
	}
}

function showTransparencia ()
{
    initElementos ();
    top.document.getElementById ("transparencia").style.display = '';
    disableSelects();
}

function  hideVentanaUnload (e)
{
	showVars (e);
	hideVentana ();
}


function hideVentana () {

	//Si lo que se quiere es ocultar el confirm

	if (window.name == 'popUpSistema') {
//		self.close ();
	} else {
		if (top.in_confirm) {
			//Tenemos que mostrar la pantalla anterior
			top.hideElemento ("div_confirm");

			top.document.getElementById ('div_progress').style.zIndex = top.document.getElementById ('transparencia').style.zIndex + 1;
			top.document.getElementById ('div_inline').style.zIndex   = top.document.getElementById ('transparencia').style.zIndex + 1;

			if (top.inline_window_type === null) {
				top.hideElemento ("transparencia");
			    top.enableSelects();
			}
			//----
			top.in_confirm = false;
		} else {
			//Si lo que se quiere ocultar no es el confirm entonces vemos que tipo es la ventana actual
			if (top.inline_window_type == 'iframe') {
			    top.enableSelects ();
		    	top.hideElemento ("transparencia");
			    top.hideElemento ("div_inline");
		    	top.document.getElementById('iframe_inline').src = parent.document.getElementById('iframe_inline').src+'&solo_cargando=1';
		    } else {
		    	stopProgress ();
		    }
		    top.inline_window_type = null;
		}
	}
}

window.hideFrame = hideVentana;

function disableEnableSelects(accion) {
    objs = top.document.getElementsByTagName("select");
    for (var i=0; i<objs.length; i++) {
        objs [i].disabled = accion;
    }
}

function startProgress (html) {
    top.hideElemento ("div_inline");
    top.showTransparencia ();
    top.document.getElementById ('div_progress').innerHTML = html;
    top.showElemento ("div_progress");
}

function stopProgress (msg) {
    top.enableSelects ();
    if (msg !== undefined) {
        alert (msg);
    }
    top.hideElemento ("div_progress");
    top.hideElemento ("transparencia");
}

function gotoGhostUrl (iframe, url)
{
    iframe.src = url;
	/*
	FIXME Error de diseņo; el wrapper de gost_back no funciona para todos los casos de uso
	hablar con JGonzalez
	top.gotoURL = true;
	iframe.src = top.to_root + '/../patron/paginas/js/ghost_back.html?src='+escape(url);*/
}

function getPadre () {
}

function enableSelects() {
    return top.disableEnableSelects('');
}

function disableSelects() {
    return top.disableEnableSelects('disabled');
}

function initElementos ()
{
	//Si estan
    if (top.document.getElementById ('transparencia') === null ||
        top.document.getElementById ('iframe_inline') === null) {
        transparencia = top.document.createElement ('div');
        transparencia.style.display = 'none';
        transparencia.id = 'transparencia';
        if (es_ie) {
            transparencia.style.height=document.body.clientHeight+100;
        }
        transparencia.style.zIndex = 100;

        div_inline = top.document.createElement ('div');
        div_inline.style.display = 'none';
        div_inline.id = 'div_inline';
        div_inline.className = 'areaIframe';
        div_inline.style.zIndex = 101;

        div_inline.innerHTML = "<iframe id='iframe_inline' class='ventanaInline' frameborder='0' scrolling='no'></iframe>";

        div_progress = top.document.createElement ('div');
        div_progress.style.display = 'none';
        div_progress.id = 'div_progress';
        div_progress.className = 'areaIframe cen';
        div_progress.style.zIndex = 101;

        div_confirm = top.document.createElement ('div');
        div_confirm.style.display = 'none';
        div_confirm.id = 'div_confirm';
        div_confirm.className = 'areaIframe cen';
        div_confirm.style.zIndex = 101;

        pagina = top.document.body.getElementsByTagName ("div").item (0);

        pagina.insertBefore (div_inline,    pagina.childNodes [0]);
        pagina.insertBefore (transparencia, pagina.childNodes [0]);
        pagina.insertBefore (div_progress,  pagina.childNodes [0]);
        pagina.insertBefore (div_confirm,   pagina.childNodes [0]);

   		top.inline_window_type    = null;
   		top.inline_confirm_parent = null;
   		top.in_confirm 			  = false;
    }
}
