/*
 * External link 1.0 (2007-04-23)
 *
 * Copyright (c) 2007 Felix Langfeldt (http://www.phixel.org/)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.phixel.org/
 *
 * Built upon jQuery 1.1.1 (http://jquery.com)
 */

(function($){
 	$.fn.externalLink = function() {
		$(this).each(function(){
			if (this.href && this.rel == "external") {
				$(this).click(function(event){
					return launchWindow(this, event);
				}).keypress(function(event){
					return launchWindow(this, event);
				});
					
				var objCurrent = this.firstChild;
				if (objCurrent) {
					if (objCurrent.nodeType == 3) { // Text node
						this.title = (this.title != "") ? this.title + " (opens in a new window)" : objCurrent.data + " opens in a new window";
					} else if (objCurrent.alt != undefined) { // Current element is an image
						objReplacement = objCurrent;
						objReplacement.alt = (objCurrent.alt != "") ? objCurrent.alt + " (opens in a new window)" : "Opens in a new window";
						try {
							this.replaceChild(objReplacement, objCurrent);
						} catch(e){}
					}
				}
			}
		});
		return this;
	};
	function launchWindow(objAnchor, objEvent) {
		var iKeyCode;

		if (objEvent && objEvent.type == "keypress") {
			if (objEvent.keyCode) {
				iKeyCode = objEvent.keyCode;
			} else if (objEvent.which) {
				iKeyCode = objEvent.which;
			}

			if (iKeyCode != 13 && iKeyCode != 32) {
				return true;
			}
		}

		return !window.open(objAnchor);
	}
})(jQuery);

$(function() {
	$('a').externalLink();
});
