// base on http://swip.codylindley.com/jquery.popupWindow.js

(function($){ 		  
	$.fn.popupWin = function(instanceSettings) {
		
		return this.each(function() {
		
		$(this).click(function(event) {
		
		$.fn.popupWin.defaultSettings = {
			position:'screen', // position relative to browser 'window', absolute on 'screen' or on 'event' coordinates
			xposition:'center', // horizontal position of the window, 'left', 'center', 'right'
			yposition:'center', // vertical position of the window, 'top', 'center', 'bottom'
			xoffset:0, // offset from xposition in px, plus values move to the right, minus values move to the left
			yoffset:0, // offset from yposition in px, plus values move down, minus values move up
			height:500, // sets the height in pixels of the window.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			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)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWin.defaultSettings, instanceSettings || {});
		
		var windowFeatures = 'height=' + settings.height +
					',width=' + settings.width +
					',toolbar=' + settings.toolbar +
					',scrollbars=' + settings.scrollbars +
					',status=' + settings.status + 
					',resizable=' + settings.resizable +
					',location=' + settings.location +
					',menuBar=' + settings.menubar;

		settings.windowName = this.name || settings.windowName;
		settings.windowURL = this.href || settings.windowURL;
		var centeredY,centeredX;
		
		var top, left, height, width;
		
		switch(settings.position) {
		case 'event':
			top = event.screenY;
			left = event.screenX;
			height = 0;
			width = 0;
			break;
		case 'browser':
		case 'window':
		case 'relative':
			if($.browser.msie) { //hacked together for IE browsers
				top = (window.screenTop - 120);
				height = document.documentElement.clientHeight + 120;
				left = window.screenLeft;
				width = document.body.offsetWidth + 20;
			} else {
				top = window.screenY;
				height = window.outerHeight;
				left = window.screenX;
				width = window.outerWidth;
			}
			break;
		case 'screen':
		case 'absolute':
		default:
			top = left = 0;
			height = screen.height;
			width = screen.width;
			break;
		}
		
		var win_top = top + settings.yoffset;
		var win_left = left + settings.xoffset;

		switch(settings.xposition) {
		case 'center':
			win_left += (width - settings.width) / 2;
			break;
		case 'right':
			win_left += width - settings.width;
			break;
		default:  // left
			//win_left += 0;
			break;
		}

		switch(settings.yposition) {
		case 'center':
			win_top += (height - settings.height) / 2;
			break;
		case 'bottom':
			win_top += height - settings.height;
			break;
		default:  // top
			//win_top += 0;
			break;
		}
		
		win_top = Math.max(0, Math.min(screen.height - settings.height, win_top));
		win_left = Math.max(0, Math.min(screen.width - settings.width, win_left));
		
		window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + win_left +',top=' + win_top).focus();
		return false;

		});
		});	
	};
})(jQuery);

