/**
 * JQuery Plugin: "frozPopupLauncher"
 * by: Jerome Coloma (http://www.frozynart.com/)
 *
 * Copyright (c) 2009 Frozynart Designs
 * Licensed under MIT (http://frozynart.com/license/mit)
 *
 * Description: Launches a Pop-up window with Pop-up Blocker detection
 * Dependencies: jQuery 1.2.6 and above
 * Usage Example: jQuery.frozPopupLauncher('http://www.frozynart.com', {
 *						windowParams:{scrollbars:'yes'},
 *						onSuccess:function(){alert('Pop-up Launched')},
 *						onFailure:function(){ alert('Pop-up Blocked!'); },
 *						fullscreen:true
 *				});
 * Version: 1.1, 2009-03-23
 * Version: 1.0, 2009-02-20
 */

(function($) {

	$.frozPopupLauncher = function(url, options) {

		$.frozPopupLauncher.opts = $.extend({}, $.frozPopupLauncher.defaults, options);

		if(!window.opener) {
			$.frozPopupLauncher._showPopUp(url);
			$.frozPopupLauncher._isPopUpLaunched('init');
		}
		
		
		
	};
	
	$.frozPopupLauncher._newWindow  = null;

	$.frozPopupLauncher._isPopUpLaunched = function(option) {

		if('init' === option) {
			setTimeout("$.frozPopupLauncher._isPopUpLaunched('verify')", $.frozPopupLauncher.opts.timeout);
			return;
		}

		if('verify' === option) {
			if($.frozPopupLauncher._newWindow && !$.frozPopupLauncher._newWindow.closed) {
				
				// onSuccess
				if($.isFunction($.frozPopupLauncher.opts.onSuccess)) {
					$.frozPopupLauncher.opts.onSuccess();
				}
				
			
				
				// onClose
				if($.frozPopupLauncher._newWindow && !$.frozPopupLauncher._newWindow.closed) {
					
					if($.isFunction($.frozPopupLauncher.opts.onClose)) {

						var myInterval = window.setInterval(function () {
							if($.frozPopupLauncher._newWindow.closed) {
								$.frozPopupLauncher.opts.onClose();
								clearInterval(myInterval);
							}
						},$.frozPopupLauncher.opts.onCloseTimerInterval);
							
					}
					
				}
				
				// Make sure window hasn't been closed by onSuccess Method before setting the focus
				if($.frozPopupLauncher._newWindow && !$.frozPopupLauncher._newWindow.closed && $.frozPopupLauncher._newWindow.focus && $.frozPopupLauncher.opts.keepFocus) {
					$.frozPopupLauncher._newWindow.focus();
				}
		
				
			} else {
				// onFailure
				if($.isFunction($.frozPopupLauncher.opts.onFailure)) {
					$.frozPopupLauncher.opts.onFailure();
				}
			}
			return;
		}


	};


	$.frozPopupLauncher._showPopUp = function(url) {

		var _p     = $.frozPopupLauncher.opts.windowParams;
		var params = '';

		if(true === $.frozPopupLauncher.opts.fullscreen) {
			params += 'width='+screen.width;
			params += ', height='+screen.height;
		} else {
			params += 'width='+_p.width;
			params += ', height='+_p.height;
		}
		
		if(true === $.frozPopupLauncher.opts.isCenter && !$.frozPopupLauncher.opts.fullscreen) {
			params += ', top='+ ((screen.height-_p.height)/2) +', left='+ ((screen.width-_p.width)/2);
		} else {
			params += ', top='+ _p.top +', left='+ _p.left;
		}
		
		
		
		
		
		params += ', fullscreen=no';
		params += ', toolbar='+ _p.toolbar;
		params += ', location='+ _p.location;
		params += ', directories='+ _p.directories;
		params += ', status='+ _p.status;
		params += ', menubar='+ _p.menubar;
		params += ', scrollbars='+ _p.scrollbars;
		params += ', resizable='+ _p.resizable;


		$.frozPopupLauncher._newWindow = window.open(url,$.frozPopupLauncher.opts.windowName, params);

		return false;
	};


	$.frozPopupLauncher.defaults = {
		onSuccess: null,
 		onFailure: null,
		onClose:null,
		onCloseTimerInterval:2000,
		windowName: 'frozPopupLauncher',
		fullscreen:false,
		timeout:1000,
		keepFocus:true,
		isCenter:false,
		windowParams:{
			width:screen.width,
			height:screen.height,
			top: 0,
			left: 0,
			toolbar: 'no',
			location: 'no',
			directories: 'no',
			status: 'no',
			menubar: 'no',
			scrollbars: 'no',
			resizable: 'no'
		}
	};


})(jQuery);


