/**
 * This jQuery 1.3 plugin changes any anchor tag with a class of 'popup' to
 * open its href attribute in a new window instead of the current frame.
 * This is useful in that it allows a graceful fallback to standard anchor tag
 * behavior in a no-script environment.
 * Author: EAK - 8/13/2009
 */
jQuery(function() {
	/**
	 * jQuery function to popup a resource in a new window.
	 * @param {String} href URI to load in a new window.
	 * @param {Object} options Parameters for window.open().
	 */
	jQuery.fn.popup = function(href, options){
		var defaults = {
			'directories': 'no',
			'height': '600',
			'left': '0',
			'location': 'no',
			'menubar': 'no',
			'resizeable': 'yes',
			'scrollbars': 'yes',
			'status': 'no',
			'toolbar': 'no',
			'top': '0',
			'width': '800'
		}
		var wName = options['target'] || 'window' + (new Date()).getMilliseconds();
		var paramList = '';
		for (var option in defaults) {
			paramList += (paramList.length = 0) ? '' : ','; // Parameter delimiter
			paramList += option + '=' + (options[option] || defaults[option]);
		}
		return window.open(href, wName, paramList);
	}
	
	// Traverse each A tag of class 'popup' and change the click handler.
	$("a.popup").each(function (){
		$(this).click(function(evt) {
			evt.preventDefault(); // Stop orig handler.
			var options = $(this).data("popup") || {}; // Fetch popup parameters
			// Use the target attribute as the window name
			if ($(this).attr("target")) { options.target = $(this).attr("target"); }
			// Pop up the window
			var win = $(this).popup(this.href, options);
			// Save the window object for other code to use
			$(this).data("windowHandle", win);
		});
	});
});
