/*
 * 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 website outside of {SITENAME}.</strong></p><p>Thank you for visiting.</p>",
			preLinkMessage: "<div class='setoff'><p>You will now be directed to:<br/>{URL}</p></div>",
			timeOut: 2000,
			overlayId: "ln-blackout",
			messageBoxId: "ln-messageBox",
			messageHolderId: "ln-messageHolder",
			overlayAlpha: 0.3
		};
		var options = $.extend(defaults, opt);
		
		return this.each(function(){
			// AW. 2010. Modification to allow url to be passed as a parameter.
			el = $(this);
			// Check for url as an option.
			if('url' in options){
				var href=options.url;
			} else {
				var href=el.attr('href');			
			}
			// AW. 2010 - Check for samewindow class.
			var samewindow = el.hasClass('samewindow');
			
			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;
				// Check for link title as an option.
				if('linktitle' in options){
					msgContent=preFilteredContent.replace(/\{URL\}/g, '<a href="'+href+'">'+options.linktitle+'</a>');
				} else {
					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>');
					// Set link target based on the samewindow var.
					if(samewindow == true){
						window.location.href=href;
					} else {
						// AW Mod in requirement now must open in new window.
						window.open(href);
					}
					clearTimeout(leaveIn);
					$('#'+options.overlayId+', #'+options.messageHolderId).fadeOut('fast',function(){
						$('#'+options.overlayId+', #'+options.messageHolderId).remove();
					});
					$(document).unbind('keyup');					
				},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);
