(function($) {
	$.fn.extend({
		newWindowLink: function(message) {
			// Based on JSTarget function by Roger Johansson, http://456bereastreet.com/

			if (message) {
				var messageHTML = ' <em>' + message + '</em>';
			} else {
				var messageHTML = '';
			}

			this.filter('a[href]').append(messageHTML).bind('click.newWindow', function(event) {
				if (!(event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)) {
					// Can't blur after new window/tab has been opened
					$(this).trigger('blur');
					var newWindow = window.open($(this).attr('href'), '_blank');
					if (newWindow) {
						if (newWindow.focus) newWindow.focus();
						event.preventDefault();
					} else {
						newWindow = null;
					}
				}
			});

			return this;
		},
		unNewWindowLink: function() {
			return this.unbind('click.newWindow');
		}
	});
}) (jQuery);

jQuery(function($) {
	// Mark local (internal) links
	$('a[href^=/], a[href^=#], a[href^=?], a[href^=http://' + window.location.hostname + '], a[href^=javascript:]').livequery(function() {
		$(this).addClass('local-link');
	}, function() {
		$(this).removeClass('local-link');
	});

	// Mark external links
	$('a[href]:not(.local-link)').livequery(function() {
		$(this).attr('rel', 'external').addClass('external-link');
	}, function() {
		$(this).removeClass('external-link').attr('rel',
			$(this).attr('rel').replace('external', '')
		);
	});

	// External links open in a new tab/window
	$('.external-link').livequery(function() {
		$(this).newWindowLink().addClass('new-window-link');
	}, function() {
		$(this).unNewWindowLink().removeClass('new-window-link');
	});
});

