/*
 * LeaveNotice - plug in to notify users of leaving your site
 * Examples and documentation at: http://rewdy.com/tools/leavenotice-jquery-plugin
 * Version: 1.0.0 (09/15/2009)
 * Copyright (c) 2009 Andrew Meyer
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.2+
*/

(function($) {
	$.fn.leaveNotice = function(opt){
		
		// define default parameters
		var defaults = {
			siteName: window.location.href,
			exitMessage: "<p><strong>You have requested a Web site outside of {SITENAME}.</strong></p>" +
			    "<p>The appearance of external hyperlinks does not constitute endorsement by the U.S. " +
			    "Army of this Web site or the information, products, or services contained therein. For " +
			    "other than authorized activities such as military exchanges and MWR sites, the U.S. Army " +
			    "does not exercise any editorial control over the information you may find at these " +
			    "locations. Such links are provided consistent with the stated purpose of this Web site.</p>",
			preLinkMessage: "<div class='setoff'><p>You will now be directed to:<br/>{URL}</p></div>",
			timeOut: 4000,
			overlayId: "ln-blackout",
			messageBoxId: "ln-messageBox",
			messageHolderId: "ln-messageHolder",
			overlayAlpha: 0.5
		};
		var options = $.extend(defaults, opt);
		
		return this.each(function(){
			el = $(this);
			var href=el.attr('href');
			el.click(function(){
				//Append overlay box
				$('body').append('<div id="' + options.overlayId + '"></div>');
				//Append message holder and message boxes
				$('body').append('<div id="' + options.messageHolderId + '"><div id="' + options.messageBoxId + '"></div></div>');
				//If not turned off in the options, set the opacity on the overlay box to the overlayAlpha option. This is the default while opacity is not supported as a CSS property in all the browsers. In the future, the opacity should be handled from the CSS.
				if (options.overlayAlpha!==false) {
					$('#'+options.overlayId).css('opacity',options.overlayAlpha);
				}
				
				//Put all the HTML together from the options and replace the keywords with their appropriate data.
				preFilteredContent=options.exitMessage + options.preLinkMessage;
				msgContent=preFilteredContent.replace(/\{URL\}/g, '<a href="'+href+'">'+href+'</a>');
				msgContent=msgContent.replace(/\{SITENAME\}/g, options.siteName);
				//Add the close controls to the HTML
				msgContent+='<p id="ln-cancelMessage"><a href="#close" id="ln-cancelLink">Cancel</a> or press the ESC key.</p>';
				//Append the HTML to the message box
				$('#'+options.messageBoxId).append(msgContent);
				
				//Set the timer to follow link after desired time.
				leaveIn=setTimeout(function(){
					$('#ln-cancelMessage').html('<em>Loading...</em>');
					window.open(href, 'outsideWin');
					$('#'+options.overlayId+', #'+options.messageHolderId).fadeOut('fast',function(){
						$('#'+options.overlayId+', #'+options.messageHolderId).remove();
					});
				},options.timeOut);
				
				//Apply event handler to pressing the close link
				$('#ln-cancelLink').click(function(){
					clearTimeout(leaveIn);
					$('#'+options.overlayId+', #'+options.messageHolderId).fadeOut('fast',function(){
						$('#'+options.overlayId+', #'+options.messageHolderId).remove();
					});
					$(document).unbind('keyup');
					return false;
				});
				
				//Set up event handler for the ESC key
				$(document).bind('keyup', function(e){
					if (e.which==27) {
						clearTimeout(leaveIn);
						$('#'+options.overlayId+', #'+options.messageHolderId).fadeOut('fast',function(){
						$('#'+options.overlayId+', #'+options.messageHolderId).remove();
						$(document).unbind('keyup');
					});
					}
				});
				return false;
			});
		});
	};
})(jQuery);
