(function($)
{

	$.fn.exitBox = function(settings)
	{
		var config = {'cookieName': 'exitBoxCheck',
					  'cookieDuration': 2 //days, decimals allowed
					 };
		
		if (settings) $.extend(config, settings);
		
		return this.each(function()
		{
			
			var obj = $(this);
			var lastMouseX;
			var lastMouseY;
			var sideBorderWidth = .1*$(window).width();
			var endBorderHeight = .1*$(window).height();
			var showing = false;
			var leftBorderCheck =
				rightBorderCheck =
				topBorderCheck =
				bottomBorderCheck = false;
			
			if (readCookie(config.cookieName) == null)
			{
				$(obj).css({
					'display' : 'none',
					'position' : 'absolute',
					'zIndex' : '5000',
					'cursor' : 'pointer'
				});
	
				$(window).resize(function() {
					setBorderWidth();
					
					if (showing) {
						positionBox();
					}
				})
				
				$(window).scroll(function() {
					if (showing) {
						positionBox();
					}
				});
	
	
				$(document).mousemove(function(e){
					lastMouseX = e.pageX - $(window).scrollLeft();
					lastMouseY = e.pageY - $(window).scrollTop();
				});
				
				
				$(document).mouseover(function(){
					//monitoring mouse				 
				}).mouseleave(function(){
					if (checkMouse() && checkScreenSize())
					{
						showBox();	
					}
				})
				
				function checkScreenSize()
				{
					if ($(window).width() > 300 && $(window).height() > 300) {
						return true;	
					} else {
						return false;	
					}
				}
				
				function setBorderWidth()
				{
					sideBorderWidth = .1*$(window).width();
					endBorderHeight = .1*$(window).height();
				}
	
				function positionBox()
				{
					if (showing) {
						$(obj).css({
							'top' : ( ($(window).height()*.5+$(document).scrollTop()) - $(obj).height()*.5 )+'px',
							'left' : ( $(window).width()*.5 - $(obj).width()*.5 )+'px'
						});
					}
				}
				
				function checkMouse()
				{					
					leftBorderCheck =
					rightBorderCheck =
					topBorderCheck =
					bottomBorderCheck = false;
					
					leftBorderCheck = (lastMouseX < sideBorderWidth) ? true : false;
					rightBorderCheck = (lastMouseX > ($(window).width()-sideBorderWidth)) ? true : false;
					topBorderCheck = (lastMouseY < endBorderHeight) ? true : false;
					bottomBorderCheck = (lastMouseY > ($(window).height()-endBorderHeight)) ? true : false;
					
					if ($(document).height() > $(window).height()) {
						//there's a scroll bar, don't fire on right side
						if ( (leftBorderCheck || topBorderCheck || bottomBorderCheck) && (!rightBorderCheck) ){
							return true;
						} else {
							return false;
						}
					} else {
						if ( leftBorderCheck || topBorderCheck || bottomBorderCheck || rightBorderCheck ){
							return true;	
						} else {
							return false;	
						}
					}
					
					
				}
				
				function showBox()
				{					
					if (!showing) {
						showing = true;
						createCookie(config.cookieName,1,config.cookieDuration);
						positionBox();
						$(obj).css('display', 'block');
						
						$(obj).click(function() {
							$(obj).remove();
						});
					}
				}
			} else {
				$(obj).remove();
			}
			
			function createCookie(name,value,days) {
				if (days) {
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				}
				else var expires = "";
				document.cookie = name+"="+value+expires+"; path=/";
			}
			
			function readCookie(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
				}
				return null;
			}
			
			function eraseCookie(name) {
				createCookie(name,"",-1);
			}			
			
		});		
	};

})(jQuery);