/*
 * Attach to anchor elements with a popup attribute. The popup attribute can
 * specify custom window.open options.
 */
$.fn.popup = function() {
	function _popup(evt) {
		var e = $(evt.target).closest("A[popup]");
		var customOptions = e.attr("popup");

		var options = {
			width: screen.width / 2,
			height: 2 * screen.height / 3,
			scrollbars: 0,
			menubar: 0
		};
		if(/width=(\d+)/.test(customOptions)) options.width = RegExp.$1;
		if(/height=(\d+)/.test(customOptions)) options.height = RegExp.$1;
		if(/scrollbars=(\d+)/.test(customOptions)) options.scrollbars = RegExp.$1;
		if(/menubar=(\d+)/.test(customOptions)) options.menubar = RegExp.$1;

		options.left = (screen.width - options.width) / 2;
		options.top = (screen.height - options.height) / 3;
		var props = [];
		for(var p in options) props[props.length] = p + "=" + options[p]

		window.open(e.attr("href"), e.attr("target"), props.join(","));
	}

	return this.each(function() {
		$(this).click(_popup);
	});
}

$(document).ready(function() {
	$("A[popup]").popup();
});

