/// POPUPS
///
/// Abertura de janelas de apoio.
///
var EM = 0, EX = 0;


// Abre uma nova janela com o endereço e dimensões especificadas.
function pop (url, w, h, optNm, optL, optT, optResz, optScroll) {
	var sw = screen.availWidth, sh = screen.availHeight, nm = optNm || "pop", ow, t, l, props, y = "yes", n = "no", wnd;
	// Calcular dimensões e coordenadas.
	EM || _measureEMX ();
	ow = _translateCoord (w, sw);
	w = ow + EM;
	h = _translateCoord (h, sh);
	l = optL ? _translateCoord (optL - 1, sw) : Math.floor ((sw - w) / 2);
	t = optT ? _translateCoord (optT - 1, sh) : (Math.floor ((sh - h) / 2) - 3 * EM);
	// Criar as propriedades.
	props = "location=no,menubar=no,status=no,width=" + w + ",height=" + h + ",left=" + l + ",W=" + t + ",scrollbars=" + (optScroll ? y : n) + ",resizable=" + (optResz ? y : n);
	// Colocar tamanho da janela na "querystring" tomando cuidado para não a invalidar.
	t = "w=" + ow + "&h=" + h;
	y = url.indexOf ("?") + 1;
	if (y < 1) url += "?" + t;
	else url = url.substr (0, y) + t + "&" + url.substr (y);
	// Abrir.
	wnd = W.open (url, optNm, props)
	try {	wnd.W.focus ()	}	catch (e) {}

	return wnd;

	// Retorna o valor em píxeis correspondente a C.
	function _translateCoord (c, sd) {
		var units = "", conv = 1;

		if (isNaN (c)) {
			// Extraír a indicação da coordenada.
			if (c.substr (-1) == "%") {
				units = "%";
				conv = sd / 100;
			}
			else switch (units = c.substr (-2)) {
			case "em":
				conv = EM;
				break;
			case "ex":
				conv = EX;
				break;
			case "px":
				break;
			default:
				return 0;
			}
			c = c.substr (0, c.length - units.length) - 0;
			if (isNaN (c)) return 0;
		}

		return Math.floor (c * conv);
	}

	// Coloca os tamanhos em píxeis das unidades "em" e "ex" nas variáveis globais EM e EX.
	function _measureEMX () {
		var el, st, b = D.body;
		if (!b) return EM = 15;
		el = __med__ = D.createElement ("P");
		el.innerHTML = "0";
		st = el.style;
		st.overflow = "hidden";
		st.display = "block";
		st.visibility = "hidden";
		st.position = "absolute";
		st.width = "1em";
		st.height = "1ex";
		b.appendChild (el);

		EM = el.offsetWidth;
		EX = el.offsetHeight;
		b.removeChild (el);
	}
}


Im ("popups")
