(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, 
			centerScreen:0,
			height:500,
			left:0,
			location:0,
			menubar:0,
			resizable:0,
			scrollbars:0,
			status:0,
			width:500, 
			windowName:null,
			windowURL:null, 
			top:0,
			toolbar:0 
		};
		
		settings = $.extend({}, $.fn.popupWindow.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;
			
				if(settings.centerBrowser){
					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);

/*
 * onUserExit jQuery Plugin (http://www.userfirstinteractive.com/)
 * @author Scott D. Brooks 
 * @created by UserFirst Interactive (creations@userfirstinteractive.com)
 */

var movingWithinSite 	= false;
var codeToExecute		= function() {};

function userMovingWithinSite() {
	movingWithinSite = true;
}

var ctrlKeyIsDown = false;
function interceptKeyUp(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else 
			return;
	}
	
	keyCode = e.keyCode;	
	if (keyCode == 17){
		ctrlKeyIsDown = false;
	}
}

function interceptKeyDown(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else
			return;
	}
	
	keyCode = e.keyCode;
	// F5 detected
	if ( keyCode == 116 ) {
		userMovingWithinSite();
	}
	
	if (keyCode == 17){
		ctrlKeyIsDown = true;
	}

	if (ctrlKeyIsDown && keyCode == 82){	
		userMovingWithinSite();
	}	
}

function interceptKeyPress(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else
			return;
	}

	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : void 0;
	if(e.charCode == null || e.charCode == 0 ) {
		if ( keyCode == 116 ) {
			userMovingWithinSite();
		}
	}
}

function attachEventListener( obj, type, func, capture ) {
	if(window.addEventListener) {
		obj.addEventListener( type, func, capture );
	} else {
		obj.attachEvent( 'on' + type, func );
	}
}

(function($){	
	$.fn.onUserExit = function(options) {		
		var defaults = {
			execute:			"",
			internalURLs:		""
		};
		var options 			= $.extend(defaults, options);
		
		if (options.execute == "") {
			alert("The onUserExit jQuery Plugin has been misconfigured.  Please add the function you wish to execute.");
		}
		if (options.internalURLs == "") {
			alert("The onUserExit jQuery Plugin has been misconfigured.  Please add internal URLs so it know when the user is navigating internally.");
		}
		codeToExecute = options.execute;

		$("a").each(function() {
			var obj = $(this); 
			var linkIsInternal = false;
			
			var myInternalURLs = options.internalURLs.split("|");

			for (i = 0; i < myInternalURLs.length; i++) {
				if (obj.attr("href").indexOf(myInternalURLs[i]) !== -1) {
					linkIsInternal = true;
				}

				if (obj.attr("href").indexOf("http://") == -1) {
					linkIsInternal = true;
				}
			}

			if (linkIsInternal == true) {				
				obj.bind("click", function(){
					userMovingWithinSite();
    			});
			}
		});

		$("form").each(function() {
			var obj = $(this);
			currentonSubmit = obj.attr("onSubmit");	
			if (currentonSubmit === undefined) {
				currentonSubmit = "";
			}
			obj.attr("onSubmit", currentonSubmit + " userMovingWithinSite();");
		});		

		attachEventListener(document,"keydown",interceptKeyDown,true);
		attachEventListener(document,"keyup",interceptKeyUp,true);
		attachEventListener(document,"keypress",interceptKeyPress,true);	
		
	};
	
	$(window).unload(function() { 

		if (movingWithinSite == false) {
			codeToExecute();
		}
	});
	
		
})(jQuery);


