/**
 * Utilidad para abrir páginas en ventana nueva
 * @param url URL a abrir
 * @param name nombre de la ventana
 * @param width ancho de la ventana
 * @param height alto de la ventana
 * @param scroll flag para indicar si la ventana tiene scroll
 * @param resize flag para indicar si la ventana es redimensionable
 */
function Popup(url, name, width, height, scroll, resize) {

	this.url	 = url;
	this.name	 = name;
	this.width	 = width;
	this.height  = height;
	this.scroll  = scroll;
	this.resize  = resize;
	this.toolbar;
	this.menubar;
	this.status;
	this.location;
	this.directories;
	this.history;
	this.windows = new Array();	// Ventanas abiertas

	/**
	 * Abre el popup
	 * @param returnWin si se pone a true, se devuelve la ventana abierta
	 */
	this.open = function open(returnWin) {
		var width		 = (this.width)?  this.width  : 690;	// ancho por defecto;
		var height		 = (this.height)? this.height : 420;	// alto por defecto;
		var screenWidth	 = screen.width;
		var screenHeight = screen.height - 100; 				//Para subirlo un poco
		var left		 = (screenWidth  - width)  / 2;
		var top			 = (screenHeight - height) / 2;
		var winOpts = "'"
		winOpts += "toolbar="		+ (this.toolbar?		"yes": "no");
		winOpts += ",menubar="		+ (this.menubar?		"yes": "no");
		winOpts += ",location="		+ (this.location?		"yes": "no");
		winOpts += ",directories="	+ (this.directories?	"yes": "no");
		winOpts += ",status="		+ (this.status?			"yes": "no");
		if(! this.scroll ) {
			winOpts += ",scrollbars=no";
		}
		winOpts += ",resizable="	+ (this.resize?			"yes": "no");
		winOpts += ",copyhistory="	+ (this.history?		"yes": "no");
		winOpts += ",left=" + left;
		winOpts += ",top="  + top;
		winOpts += ",width=" + width;
		winOpts += ",height=" + height;
		winOpts += "'";
		var winName = (this.name)? this.name : '';
		var winUrl  = (this.url)?  this.url  : '';
		var win     = window.open(winUrl, winName, winOpts);
		this.windows[this.windows.length] = win;
		if(returnWin) {
			return win;
		} else {
			win.focus();
		}
	}
	
	/**
	 * Cierra todas las ventanas abiertas por este popup
	 */
	this.closeAll = function closeAll() {
		for(var i=0; i < this.windows.length; i++) {
			try {
				this.windows[i].close();
			} catch(e) {}
		}
	}
}

var wPopup = new Popup();

function openPopup(url, name, width, height, scroll, resize) {
	wPopup.url		= url;
	wPopup.name		= name;
	wPopup.width	= width;
	wPopup.height	= height;
	wPopup.scroll	= scroll;
	wPopup.resize	= resize;
	wPopup.open();
}

function closePopups() {
	wPopup.closeAll();
}

