var PopupContainer = null;
var PopupOverlay = null;
var PopupFrame = null;
var PopupScroll = null;
var PopupOnClose = null;

var PopupBarHeight = 20;
var PopupDefaultOpacity = 0.8;

var PopupFrameBarHTML = 
	"<a href=\"javascript:PopupClose();\">Sluiten</a>";

function PopupShow(url, options)
{
	if ((typeof options.container) == "undefined")
		options.container = document.getElementsByTagName("body")[0];
	
	if ((typeof options.onClose) == "undefined")
		PopupOnClose = null;
	else
		PopupOnClose = options.onClose;
	
	var viewport = document.viewport.getDimensions();
	
	var width = viewport.width;
	if ((typeof options.width) != "undefined")
		width = Math.min(viewport.width, options.width);
	
	var height = viewport.height;
	if ((typeof options.height) != "undefined")
		height = Math.min(viewport.height, options.height);
	
	var opacity = PopupDefaultOpacity;
	if ((typeof options.opacity) != "undefined")
		opacity = options.opacity;
	
	PopupScroll = [window.scrollX, window.scrollY];
	window.scroll(0, 0);
	
	PopupContainer = $(options.container);
	
	PopupOverlay = new Element("div");
	PopupOverlay.addClassName("PopupOverlay");
	PopupOverlay.setOpacity(opacity);
	
	PopupFrame = new Element("div");
	PopupFrame.addClassName("PopupFrame");
	
	PopupFrame.style.width = width + "px";
	PopupFrame.style.height = (height + PopupBarHeight) + "px";
	PopupFrame.style.marginLeft = parseInt(width * -0.5) + "px";
	PopupFrame.style.marginTop = parseInt(height * -0.5) + "px";
	
	var bar = new Element("div");
	PopupFrame.appendChild(bar);
	bar.update(PopupFrameBarHTML);
	
	var img = new Element("img");
	PopupFrame.appendChild(img);
	img.src = url;
	img.style.width = width + "px";
	img.style.height = height + "px";
	
	img.onclick = PopupClose;
	
	PopupContainer.insertBefore(PopupFrame, PopupContainer.firstChild);
	PopupContainer.insertBefore(PopupOverlay, PopupContainer.firstChild);
}

function PopupClose()
{
	PopupContainer.removeChild(PopupOverlay);
	PopupContainer.removeChild(PopupFrame);
	
	window.scroll(PopupScroll[0], PopupScroll[1]);
	
	if (PopupOnClose != null)
		PopupOnClose();
}
