function WindowMaster() {
	var windows = new Array();

	this.addWindow = function(w) {
		windows[windows.length] = w;
	}

	this.removeWindow = function(w) {
		var newWindows = new Array();
		for(var i = 0; i < windows.length; i++) {
			if(windows[i] != w) {
				newWindows[newWindows.length] = windows[i];
			}
		}
		windows = newWindows;
	}

	this.hideAllWindows = function() {
		for(var i = 0; i < windows.length; i++) {
			windows[i].hideWindowPane();
		}
	}

	this.showAllWindows = function() {
		for(var i = 0; i < windows.length; i++) {
			windows[i].showWindowPane();
		}
	}

	this.toggleWindows = function() {
		for(var i = 0; i < windows.length; i++) {
			windows[i].toggleVisibility();
		}
	}

	this.onResize = function() {
		for(var i = 0; i < windows.length; i++) {
			windows[i].onResize();
		}
	}

	this.selectWindow = function(w) {
		w.getPane().style.zIndex = 1000000;
		updateWindowsStacking();
	}
	
	function sortLayersByIndex(arr) {
		for(var i = 0; i < arr.length; i++) {
			for(var j = i; j < arr.length; j++) {
				if(parseInt(arr[i].getPane().style.zIndex) > parseInt(arr[j].getPane().style.zIndex)) {
					var swap = arr[j];
					arr[j] = arr[i];
					arr[i] = swap;
				}
			}
		}
		return arr;
	}

	function updateWindowsStacking() {
		var sortedWindows = sortLayersByIndex(windows);
		for(var i = 0; i < sortedWindows.length; i++) {
			sortedWindows[i].getPane().style.zIndex = i + DEFAULT_WINDOW_INDEX;
		}
	}

}
