var PopupControl = new function(){
	this.windows = new Array();
	this.blackout = new Array();
	
	this.getSettings = function(settings){
		var obj = new Object();
		obj.width = 500;
		obj.height = 200;
		
		if(tmp != ""){
			var tmp = settings.split(",");
			if(tmp.length > 0){
				for(var i=0; i < tmp.length; i++){
					var setting = tmp[i].split("=");
					if(setting.length == 2){
						obj[setting[0].toLowerCase()] = setting[1];
					}
				}			
			}
		}
		return obj;
	}
	
	this.openPopup = function(url, name, settings){
		//var win = window.open(url, name, settings);
		//this.windows.push(win);
		
		this.closeWindowById("window_" + name);
		
		var bdy = document.getElementsByTagName("body")[0];
		
		settings = this.getSettings(settings);
		
		var bw = parseInt(DOMFunctions.getBrowserWidth());
		var bh = parseInt(DOMFunctions.getBrowserHeight());
		
		var wh = parseInt(settings.height);
		var ww = parseInt(settings.width);
		
		var top = 0;// parseInt(bh/2) - parseInt(wh/2);
		if(top < 0)
			top = 0;
		var left = parseInt(bw/2) - parseInt(ww/2);
		if(left < 0)
			left = 0;
			
		var divBlackout = document.createElement("div");
		bdy.appendChild(divBlackout);
		divBlackout.style.position = "absolute";
		divBlackout.style.backgroundColor = "black";
		divBlackout.style.height = bh + "px";
		divBlackout.style.width = bw + "px";
		divBlackout.style.top = "0px";
		divBlackout.style.left = "0px";
		divBlackout.style.zIndex = "1000";
		DOMFunctions.setOpacity(divBlackout, 60);
		this.blackout.push(divBlackout);
						
		var div = document.createElement("div");
		bdy.appendChild(div);
		div.id = "window_" + name;
		div.style.backgroundColor = "white";
		div.style.height = wh + "px";
		div.style.width = ww + "px";
		div.style.top = top + "px";
		div.style.left = left + "px";
		div.style.position = "absolute";
		div.style.border = "1px solid black";
		div.style.zIndex = "10001";
		
		var divClose = document.createElement("div");
		div.appendChild(divClose);
		divClose.style.textAlign = "right";
		divClose.style.height = "24px";
		divClose.style.borderBottom = "1px solid black";
		
		
		var btn = DOMFunctions.createInputElement("button", "btnClose", "X");
		divClose.appendChild(btn);
		btn.id = "btnClose_" + div.id
		btn.onclick = function(){
			var id = this.id.replace("btnClose_", "");
			PopupControl.closeWindowById(id);
		}
		btn.style.backgroundColor = "#e0e0e0";
		btn.style.margin = "2px";
		btn.style.padding = "2px";
		btn.style.fontSize = "10px";
		btn.style.border = "1px solid #404040";
		
		var iframe = document.createElement("iframe");
		div.appendChild(iframe);
		iframe.id = "frame_" + div.id
		iframe.name = "frame_" + div.id
		iframe.style.backgroundColor = "white";
		iframe.style.width = ww + "px";
		iframe.style.height = (wh - parseInt(divClose.style.height) - 3	) + "px";
		iframe.src = url;
		iframe.border = 0;
		iframe.style.borderWidth = "0px";
		
		this.windows.push(div);
		
		
	}
	
	this.openPopupImagePreview = function(name, url, height, width, caption){
		//var win = window.open(url, name, settings);
		//this.windows.push(win);
		
		var div = document.getElementById("window_" + name)
		if(!div){
		    return;
		}
		
		
		var bdy = document.getElementsByTagName("body")[0];
		
		var bw = parseInt(DOMFunctions.getBrowserWidth());
		var bh = parseInt(DOMFunctions.getBrowserHeight());
		
		
		var letters = caption.length;
		var pad = letters * parseInt(height)/parseInt(width);
		pad = parseInt(pad / 6);
		
		var wh = parseInt(height) + 100 + pad;
		var ww = parseInt(width) + 40;
		
		var top = 200;// parseInt(bh/2) - parseInt(wh/2);
		if(top < 0)
			top = 0;
		var left = parseInt(bw/2) - parseInt(ww/2);
		if(left < 0)
			left = 0;
			
		
		var divBlackout = document.getElementById("blackout_" + name);
		if(divBlackout){
		    divBlackout.style.position = "absolute";
		    divBlackout.style.backgroundColor = "black";
		    divBlackout.style.height = bh + "px";
		    divBlackout.style.width = bw + "px";
		    divBlackout.style.top = "0px";
		    divBlackout.style.left = "0px";
		    divBlackout.style.zIndex = "1000";
		    divBlackout.style.display = "";
		    DOMFunctions.setOpacity(divBlackout, 60);
		}
						
		div.style.height = wh + "px";
		div.style.width = ww + "px";
		div.style.top = top + "px";
		div.style.left = left + "px";
		div.style.position = "absolute";
		div.style.zIndex = "10001";
		div.style.display = "";
		
		var imgFull = document.getElementById("imgFull");
		//imgFull.height = height + "";
		//imgFull.width = width + "";
		imgFull.src = "/lib/image-functions/resize.aspx?ImageUrl=" + url + "&MaxWidth=" + width + "&MaxHeight=" + height;
		
		var spnCaption = document.getElementById("spnCaption");
		spnCaption.innerHTML = caption;
		
		var div = document.getElementById("ctl00_h1PageTitle");
		if(div);
			div.style.display = "none";
		
	}
	
	this.closeImagePreview = function(name){	    
		var div = document.getElementById("window_" + name)
		if(div){
		    div.style.display = "none";
		}
		var divBlackout = document.getElementById("blackout_" + name);
		if(divBlackout){
		    divBlackout.style.display = "none";
		}
				var div = document.getElementById("ctl00_h1PageTitle");
		if(div);
			div.style.display = "";

	}
	
	this.closeWindowById = function(id){
		for(var i=0; i < this.windows.length; i++){
			if(id == this.windows[i].id){
				return this.closeWindowByNumber(i);
			}
		}
		return false
	}
	
	this.closeWindowByNumber = function(num){
		var bdy = document.getElementsByTagName("body")[0];		
		if(num >= 0 && num < this.windows.length){
			var div = this.windows[num];
			bdy.removeChild(div);
			this.windows.splice(num,1);
			
			var div = this.blackout[num];
			bdy.removeChild(div);
			this.blackout.splice(num,1);
			
			
			return true;
		}
		return false;
	}

/*	
	this.closeWindowByNumber = function(num){
		if(num > 0 && num < this.windows.length){
			this.windows[num].close();
			this.windows.splice(num,1);
			return true;
		}
		return false;
	}
	this.closeWindow = function(name){
		for(var i=0; i < this.windows.length; i++){
			if(url == this.windows.name){
				return this.closeWindowByNumber(i);
			}
		}
		return false;
	}
	this.closeAll = function (){
		for(var i=0; i < this.windows.length; i++){
			this.closeWindowByNumber(i);
		}
		return true;
	}
	*/
}
