var jtk = new JoookToolkit();


/*
	Simple window class used by the window manager
	
	Note: Window instances are unaware of their own stacking index
*/
function ManagedWindow(){}
ManagedWindow.prototype.id = null;
ManagedWindow.prototype.clickSrc = null;
ManagedWindow.prototype.options = null;
ManagedWindow.prototype.toString = function(){return this.id}


var WindowManager = {
	minDepth: 1,
	maxDepth: 999,
	displayVisible: "block",
	displayHidden: "none",
	opacityActive: 100,
	opacityInactive: 90,
	openWindows: new Array(),
	closing: false,
	
	// Move the selected window to front
	moveToFront: function(win){
		if(!this.closing){
			this.openWindows.sort();
			var currentDepth = this.minDepth;
			try{
				for(var i = 0; i < this.openWindows.length; i++){
					if(this.openWindows[i].id == win.id){
						jtk.select("#" + this.openWindows[i].id)[0].style.zIndex = this.maxDepth;
						jtk.setOpacity(jtk.select("#" + this.openWindows[i].id)[0], this.opacityActive);
					}
					else{
						jtk.select("#" + this.openWindows[i].id)[0].style.zIndex = currentDepth;
						jtk.setOpacity(jtk.select("#" + this.openWindows[i].id)[0], this.opacityInactive);
						currentDepth++;
					}
				}
			}
			catch(e){}
		}
	},
	
	
	getWin: function(id){
		for(var i = 0; i < this.openWindows.length; i++){
			if(this.openWindows[i].id == id){
				return this.openWindows[i];
			}
		}
		
		return null;
	},
	
	
	
	removeWin: function(win){
		var tempArray = this.openWindows.slice();
		this.openWindows = new Array();
		for(var i = 0; i < tempArray.length; i++){
			if(tempArray[i].id != win.id){
				this.openWindows.push(tempArray[i])
			}
		}
	},
	
	
	positionWindow: function(win){
		var opts = win.options;
		var emt = jtk.select("#" + win.id)[0];
		
		if(opts.absX != null || opts.absY != null){
			emt.style.left = (opts.absX != null) ? opts.absX + "px" : "0";
			emt.style.top = (opts.absY != null) ? opts.absY + "px" : "0";
		}
		else{
			var srcPos = jtk.getPosition(win.clickSrc);
			emt.style.left = (srcPos.left + opts.relX) + "px";
			emt.style.top = (srcPos.top + opts.relY) + "px";
		}
	},
	
	
	// Open a pop-under window (if not already open) and move it to front in the stacking order
	openWin: function(id, clickSrc, optObj){
		var win = new ManagedWindow();
		win.id = id;
		win.clickSrc = clickSrc;
				
		var opts = {
			relX: 0,
			relY: 0,
			absX: null,
			absY: null
			}
		
		if(typeof(optObj) == 'object'){
			if(typeof(optObj.relX) != 'undefined')
				opts.relX = optObj.relX;
				
			if(typeof(optObj.relY) != 'undefined')
				opts.relY = optObj.relY;
			
			if(typeof(optObj.absX) != 'undefined')
				opts.absX = optObj.absX;
				
			if(typeof(optObj.absY) != 'undefined')
				opts.absY = optObj.absY;
		}
		
		win.options = opts;
		
		// Do not push a new window instance into the stack if a similar instance already exists
		if(this.getWin(id) == null){
			this.positionWindow(win);
			
			jtk.select("#" + win.id)[0].style.display = this.displayVisible;
			this.openWindows.push(win);	
		}
		
		this.moveToFront(win);
	},
	
	
	
	
	closeWin: function(id){
		this.closing = true;
		var win = this.getWin(id);
		if(win != null){
			this.removeWin(win);
		}
		
		jtk.select("#" + win.id)[0].style.display = this.displayHidden;
		this.closing = false;
	},
	
	
	
	
	popup: function(url, optObj){
		var specs = new Array();
		var name = null;
		
		if(typeof(optObj) == 'object'){
			if(typeof(optObj.name) != 'undefined')
				name = optObj.name;
				
			if(typeof(optObj.width) != 'undefined')
				specs.push("width=" + optObj.width);
				
			if(typeof(optObj.height) != 'undefined')
				specs.push("height=" + optObj.height);
			
			if(typeof(optObj.left) != 'undefined')
				specs.push("left=" + optObj.left);
			
			if(typeof(optObj.top) != 'undefined')
				specs.push("top=" + optObj.top);
			
			if(typeof(optObj.location) != 'undefined')
				specs.push("location=" + optObj.location);
			
			if(typeof(optObj.menubar) != 'undefined')
				specs.push("menubar=" + optObj.menubar);
				
			if(typeof(optObj.resizable) != 'undefined')
				specs.push("resizable=" + optObj.resizable);
			
			if(typeof(optObj.scrollbars) != 'undefined')
				specs.push("scrollbars=" + optObj.scrollbars);
			
			if(typeof(optObj.status) != 'undefined')
				specs.push("status=" + optObj.status);
			
			if(typeof(optObj.titlebar) != 'undefined')
				specs.push("titlebar=" + optObj.titlebar);
				
			if(typeof(optObj.toolbar) != 'undefined')
				specs.push("toolbar=" + optObj.toolbar);
		}
		
		var specsStr = null;
		
		if(specs.length > 0){
			specsStr = specs.join(",");
		}
		
		window.open(url, name, specsStr);
	}
}