/**
 * jquery::popupWindow()
 * 
 * Creates popup window according
 * to a variety of parameters 
 * 
 * @author Stelian.Mocanita <smocanita@Ocenture.com>
 * @category jQuery General Plugin
 */

(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			/** Center window over Browser Window
			 *	Overrides top and left 
			 **/
			centerBrowser:0,
			
			/** Center window over the entire screen 
			 *  Overrides top and left
			 */
			centerScreen:0,
			
			/** The height of the window **/
			height:500, 
			
			/** Left positioning of the window **/
			left:0, 
			
			/** Determines if the address bar is displayed **/
			location:0,
			
			/** Determines if menu bar is displayed **/
			menubar:0, 
			
			/** Determines if window can be resized **/
			resizable:0, 
			
			/** Determines if scrollbars are displayed **/
			scrollbars:0, 
			
			/** Determines if status bar is displayed **/
			status:0,
			
			/** The width of the window **/
			width:500, 
			
			/** The window name **/
			windowName:null, 
			
			/** The url of the window **/
			windowURL:null,
			
			/** The top positioning of the window **/
			top:0,
			
			/** Determines if the window has toolbars or not **/
			toolbar:0 
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		/** Check if width or height are procentual **/
		
		userHeight = "" + settings.height;
		
		if( userHeight.indexOf( '%' ) !== -1 ) {			
			settings.height = userHeight.replace( '%', '' );
			settings.height = Math.round( parseInt( settings.height ) / 100 *  document.body.clientHeight );
		}
		
		userWidth = "" + settings.width;
		if( userWidth.indexOf( '%' ) !== -1 ) {
			settings.width = userWidth.replace( '%', '' );
			settings.width = Math.round( parseInt( settings.height ) / 100 *  document.body.clientWidth );
		}
		
		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;
			
				if(settings.centerBrowser){
					
					/** Some tiny changes for IE bars **/
					if ($.browser.msie) {
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					} else {
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}	else if(settings.centerScreen)	{
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}	else	{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);
