// Version 1.1 - September 17, 2007
// Requires http://jquery.com version 1.2
// Expects links or elements that contain links
(function($) {
	$.fn.popwindow = function(options) {
		// Reference to popped window
		var popped = null;
		
		// Default settings
		var settings = {
			height:400, // sets the height in pixels of the window.
			width:500, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menubar is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:0, // left position when the window appears.
			top:0, // top position when the window appears.
			center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			popclass:'willpop' // class to add to <a> when event is added
		};
		if(options) {
			$.extend(settings, options);
		}
		
		// Close existing popped window
		var close = function(popped){
		if (popped != null){
			if(!popped.closed)
				popped.close();
				}
		};

		
		// Provided links
		$(this).add($(this).not('a').find('a[href]')).filter('a[href]').each(function(i){
			// center the window
			if (settings.center == 1)
			{
				settings.top = (screen.height-(settings.height + 110))/2;
				settings.left = (screen.width-settings.width)/2;
			}
			var parameters = "height=" + settings.height + ",width=" + settings.width + ",menubar=" + settings.menubar + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
			
			$(this).addClass(settings.popclass).bind('click', function(){
				close($.fn.popwindow.popped);
				$.fn.popwindow.popped = window.open(this.href, 'pop', parameters);
				$.fn.popwindow.popped.focus();
				return false;
			});
		});
		return this;
	};
})(jQuery);


