var windows = new Array();

function getYesNo(b){
	if(b)
		return "yes";
	return false;
}

function WindowManager(wnd){
	if(wnd == null)
		return false;
	
	this.wnd = wnd;
	
	this.x = 0;
	this.setX = function(x){this.x = x;}
	
	this.y = 0;
	this.setY = function(y){this.y = y;}
	
	this.width = 0;
	this.setWidth = function(w){this.width = w;}
	
	this.height = 0;
	this.setHeight = function(w){this.height = h;}
	
	this.centerAt = function(w, h){
		this.width = w;
		this.height = h;
		this.x = (this.wnd.screen.width / 2) - (w / 2);
		this.y = (this.wnd.screen.height / 2) - (h / 2);
	}

	this.resizable=true;
	this.setResizable = function(b){this.resizable=b;}
	
	this.menubar=true;
	this.setMenubar = function(b){this.menubar=b;}
	
	this.location=false;
	this.setLocation = function(b){this.location=b;}
	
	this.toolbar=false;
	this.setToolbar = function(b){this.toolbar=b;}
	
	this.scrollbars=true;
	this.setScrollbars = function(b){this.scrollbars=b;}
	
	//do not return anything in this function,
	//ie will prompt the user if they want to 
	//leave the page.
	this.onUnload=function(){
		if(windows != null && windows.length){
			var i = 0;
			for(i = 0; i < windows.length; i++){
				if(!windows[i].closed)
					windows[i].close();
				windows[i] = null;
			}
		}
		windows = null;
		this.wnd = null;
	}
	
	this.wnd.onunload=this.onUnload;
	this.wnd.onbeforeunload=this.onUnload;
	
	this.getWindow = function(id){
		for(var i = 0; i < windows.length; i++)
			if(!windows[i].closed && windows[i].name == id)
				return windows[i];
		return null;
	}
	
	//This function adds a window to the internal array.
	//The id must be supplied and it must be unique.
	//closeEventListener: Optional. Method to listen to window close events.
	//width and height are optional. If they are set, then centerAt will be called.
	this.showWindow = function(url, id, width, height){
		var w = this.getWindow(id);
		if(w != null){
			w.focus();
			w.document.location = url;
			return false;
		}
		
		if(width != null && height != null)
			this.centerAt(width, height);
		
		var options = "";
		options += "width=" + this.width + ",height=" + this.height + ",screenX=" + this.x + ",screenY=" + this.y + ",left=" + this.x + ",top=" + this.y + ",";
		options += "resizable=" + getYesNo(this.resizable) + ",";
		options += "menubar=" + getYesNo(this.menubar) + ",";
		options += "location=" + getYesNo(this.location) + ",";
		options += "toolbar=" + getYesNo(this.toolbar) + ",";
		options += "scrollbars=" + getYesNo(this.scrollbars);
		
		w = this.wnd.open(url, id, options);
		if(w != null)
			windows[windows.length] = w;
		else
			alert('Failed to open ' + url + '.\nCheck to see if you have a popup blocker turned on.');
		return false;
	}
	
	this.closeWindow = function(id){
		var w = this.getWindow(id);
		if(w)
			w.close();
		return false;
	}
	return false;
}