/**
 * Para marcar un enlace como popup, es necesario utilizar el atributo "rel" de
 * los enlaces (etiquetas "<a ... >...</a>"), con la siguiente estructura:
 * <a href="[url]" rel="popup [tipo] [ancho] [alto]" ... >...</a>
 *  - [url]: Dirección que se va a abrir en la ventana del popup
 *  - [tipo]: Tipo de popup (fullscreen, standard, console, fixed -por defecto-)
 *      > fullscreen: ventana a pantalla completa
 *      > standard: ventana normal del navegador
 *      > console: ventana redimensionable sin barras de herramientas
 *      > fixed: ventana de tamaño fijo sin barras de herramientas ni de estado
 *  - [ancho]: Ancho, en píxeles, del contenido que se va a abrir en el popup
 *  - [alto]: Alto, en píxeles, del contenido que se va a abrir en el popup
 */

// Activa el comportamiento de los popups
activePopup = true;
var newWindow = null;

// Cierra la ventana del popup si está abierta
function closeWin() {
	if (newWindow != null) {
		if (!newWindow.closed) newWindow.close();
	}
}

// Abre una ventana con el popup correspondiente, en función de los parámetros
function popUpWin(url, type, strWidth, strHeight) {
    // Calcula el tamaño disponible de ventana
    if (BrowserDetect.browser == "Explorer") {
        avlWidth = document.body.clientWidth;
        avlHeight = document.body.clientHeight;
    }
    else {
        avlWidth = window.innerWidth;
        avlHeight = window.innerHeight;
    }
	// Ajusta el alto de la ventana para albergar la cabecera y el pie
	strHeight = parseInt(strHeight, 10) + 32 + 32;
	// Añade un margen
	strWidth = parseInt(strWidth, 10) + 24;
	strHeight = parseInt(strHeight, 10) + 24;
	// Cierra la ventana de popup si ya estaba abierta
	closeWin();
	// Establece las propiedades de la ventana
	type = type.toLowerCase();
	var tools = "";
    if (type == "fullscreen") {
        strWidth = avlWidth;
        strHeight = avlHeight;
        leftPos = Math.round((screen.availWidth - avlWidth) / 2);
        topPos = Math.round((screen.availHeight - avlHeight) / 2);
    }
    else {
        if (strWidth > avlWidth) strWidth = avlWidth;
        if (strHeight > avlHeight) strHeight = avlHeight;
    	leftPos = Math.round((screen.availWidth - avlWidth) / 2) + Math.round((avlWidth - strWidth) / 2);
    	topPos = Math.round((screen.availHeight - avlHeight) / 2) + Math.round((avlHeight - strHeight) / 2);
    }
	if (type == "fullscreen") {
		tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+avlWidth+",height="+avlHeight+",top="+topPos+",left="+leftPos;
	}
	if (type == "standard") {
		tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top="+topPos+",left="+leftPos;
	}
	if (type == "console") {
		tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",top="+topPos+",left="+leftPos;
	}
	if (type == "fixed") {
		tools = "resizable=no,toolbar=no,location=no,scrollbars=no,status=no,width="+strWidth+",height="+strHeight+",top="+topPos+",left="+leftPos;
	}
	if (type == "scrollbars") {
		strWidth = "680";
		strHeight = "450";
		topPos = "0";
		leftPos = "0";
		tools = "resizable=no,toolbar=no,location=no,scrollbars=yes,status=no,width="+strWidth+",height="+strHeight+",top="+topPos+",left="+leftPos;
	}
	// Crea la nueva ventana
    if (url.indexOf('?') != -1) {
        url += '&popw='+strWidth+'&poph='+strHeight;
    }
    else {
        url += '?popw='+strWidth+'&poph='+strHeight;
    }
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

// Captura el evento click de un enlace de tipo popup y crea la ventana
function doPopUp(e) {
	// Anula la acción del enlace
	if (window.event) {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
	else if (e) {
		e.stopPropagation();
		e.preventDefault();
	}
	// Ajustes por defecto
	var t = "fixed";
	var w = "640";
	var h = "480";
	// Procesa los parámetros establecidos en el enlace
	attribs = this.rel.split(" ");
	if (attribs[1] != null) {t = attribs[1];}
	if (attribs[2] != null) {w = attribs[2];}
	if (attribs[3] != null) {h = attribs[3];}
	// Crea la ventana
	popUpWin(this.href, t, w, h);
}

// Busca los enlaces marcados para asignarles el capturador de eventos de popup
function findPopUps() {
	// Lista de enlaces incluidos en la página
	var popups = document.getElementsByTagName("a");
	for (i = 0; i < popups.length; i++) {
		// Enlace marcado como popup
		if (popups[i].rel.indexOf("popup") != -1) {
			popups[i].onclick = doPopUp;
		}
	}
}

// Imprime el contenido de un popup
function printContenidoPopup() {
	var printVentana = window.open("/popup/popupPrint.html", "printVentana", "width=1,height=1,status=no,toolbar=no,scrollbars=no,resizable=no,location=no,directories=no,menubar=no,titlebar=no");
}

