var currentPopup 	   = null;
var defaultPopupWidth  = 576;
var defaultPopupHeight = 512;

function openPopupWindow(url, options) {
	if (url) {
		
		// Build a default set of options if none are given
		options = options || {
			width:  	defaultPopupWidth  || window.screen.width,
			height: 	defaultPopupHeight || window.screen.height,
			toolbar:    "no",
			scrollbars: "yes",
			resizable:  "no"
		};
		
		// If no placement was given, center the window
	 	options.left = options.left || (window.screen.width  - options.width)  / 2;
		options.top  = options.top  || (window.screen.height - options.height) / 2;
	
		// Build a standard features string
		var features = "";
		for (var i in options) {
			features += i + "=" + options[i] + ",";
		}
		
		// Remove the trailing comma
		features = features.substr(0, features.length - 1);
	
		// Create the popup and focus it
		currentPopup = window.open(url, '__popup__', features);
		if (currentPopup)
			currentPopup.focus();
	
		// Ensure that the popup window closes when the parent page does
		var delegate = window.onunload;
		window.onunload = function() {
			if (currentPopup)
				currentPopup.close();
			if (delegate) 
				delegate.call();
		}
	}
}
