
	(function($){
		$(document).ready(function() {
			var notice = $("#leaveNotice");
			var pattern = /^http(?:s?):\/\/([^\/]*)/;
			//________________________________________________________________________
			function noticeShow(element, url) {
				element = $(element);
				element.data('on', true);
				element.mousemove(noticeMove);
				element.mouseout(noticeHide);
				$.ajax({
					url: url,
					success: function(data) {
						if(element.data('on')) {
							notice.html(data);
							notice.fadeIn(250);
						}
					}
				});
			}
			//________________________________________________________________________
			function noticeMove(event) {
				var X = 0;
				var Y = 0;
				var width = notice.width();
				var height = notice.height();
				if((event.pageX + width) > document.width) {
					X = event.pageX - width - 25;
				} else {
					X = event.pageX + 25;
				}
				if((event.pageY + height) > document.height) {
					Y = event.pageY - height - 25;
				} else {
					Y = event.pageY + 25;
				}
				notice.css({top: Y, left: X});
			}
			//________________________________________________________________________
			function noticeHide(event) {
				var self = $(this);
				self.data('on', false);
				notice.hide();
			}
			//________________________________________________________________________
			$("a").each(function(){
				var self = $(this);
				var href = self.attr('href');
				if(href) {
					var matches = href.match(pattern);
					if((matches && matches[1] != __domain_name) || self.hasClass('external')) {
						self.mouseover(function() {
							noticeShow(self, '/notice-leaving');
						})
						self.click(function(event) {
							event.preventDefault();
							if(self.hasClass('dead') == false) {
								window.open(href);
							}
							return false;
						});
					} else if(self.attr('href') == '/sign-up') {
						self.mouseover(function() {
							noticeShow(self, '/notice-baby-centre');
						})
					} else if(self.attr('href').match('/our-world\/wipe-out-waste-terracycle-sign-up')) {
						self.mouseover(function() {
							noticeShow(self, '/notice-terracycle');
						})
					}
				}
			});
			//________________________________________________________________________
		});

	function popuponclick() {
		/*
		THIS IS A TERRIBLE IMPLEMENTATION - THIS SHOULD
		BE ABSTRACTED INTO A NAMESPACED ROUTINE AND ATTACHED
		BY DOM EVENTS ACCORDINGLY.
		*/
		return;
		//window.open('https://www.surveymonkey.com/jsPopInvite.aspx?sm=StWWWdoPAhRl5hCztQiWTQ%3d%3d', 'invitationwindow', 'height=350,width=500,scrollbars=yes,resizable=yes,status=yes');
	}
	
	})(jQuery);
