addEvent(window, "load", external_init);

// The popup window
var EXTERNAL_WINDOW;

var EXTERNAL_LINK_SOURCE;
var EXTERNAL_LINK_URL;
var EXTERNAL_LINK_TARGET;

/*
	Modify behavior, title, and style of 'rel="external"' links.
*/
function external_init() {
	if (!document.getElementsByTagName) return;	
	// Get all 'a' tags; look for those with 'rel' == 'external'
	
	oElems = document.getElementsByTagName('a');

	for (var i = 0; i < oElems.length; i++) {
		var oElem = oElems[i];
		if (attribute(oElem, 'href') && attribute(oElem, 'rel') == 'external') {
			// Set title, target, add CSS class
			oElem.target = '_blank';
			oElem.title = 'Opens in a new window';
			
			var fWinFunct = null;
			
			// Check to see if the wrapped item is an image or a link to an image; if is, treat it differently
			if (getTag(oElem.firstChild) == 'img') {
				oElem.title = 'Enlarge this picture';
				
				fWinFunct = function(e) {
				
					// Target of click is the image. Get the parent node, the anchor.
					var oHref = getTarget(e).parentNode;
					var sHref = attribute(oHref, 'href');
					
					EXTERNAL_WINDOW = openWindow('/tools/image-viewer/image-viewer.html?i=' + sHref.urlEncode(), null, null, 0, 0, 0, 1, null, null, 2, 0, 0);
					
					if (e) {
						e.returnValue = false;
						if (e.preventDefault) {
							e.preventDefault();
						}
					} else {
						window.event.returnValue = false;
					}
					
					if (navigator.userAgent.indexOf('Safari') > 0) {
						EXTERNAL_LINK_SOURCE = oHref;
						EXTERNAL_LINK_URL = oHref.href;
						EXTERNAL_LINK_TARGET = oHref.target;
						
						oHref.href = '#';
						oHref.target = '_self';
						
						setTimeout('EXTERNAL_LINK_SOURCE.href = EXTERNAL_LINK_URL; EXTERNAL_LINK_SOURCE.target = EXTERNAL_LINK_TARGET;', 1);
					}
				}

			} else if ((getTag(oElem) == 'a' && attribute(oElem, 'class') != null && attribute(oElem, 'class').indexOf('fit-image') != -1)) {
				addCSSClass(oElem, 'a-popup');
				
				fWinFunct = function(e) {
				
					// Target of click is the image. Get the parent node, the anchor.
					var oHref = getTarget(e);
					var sHref = attribute(oHref, 'href');
					
					EXTERNAL_WINDOW = openWindow('/tools/image-viewer/image-viewer.html?i=' + sHref.urlEncode(), null, null, 0, 0, 0, 1, null, null, 2, 0, 0);
					
					if (e) {
						e.returnValue = false;
						if (e.preventDefault) {
							e.preventDefault();
						}
					} else {
						window.event.returnValue = false;
					}
					
					if (navigator.userAgent.indexOf('Safari') > 0) {
						EXTERNAL_LINK_SOURCE = oHref;
						EXTERNAL_LINK_URL = oHref.href;
						EXTERNAL_LINK_TARGET = oHref.target;
						
						oHref.href = '#';
						oHref.target = '_self';
						
						setTimeout('EXTERNAL_LINK_SOURCE.href = EXTERNAL_LINK_URL; EXTERNAL_LINK_SOURCE.target = EXTERNAL_LINK_TARGET;', 1);
					}
				}
			} else {
				addCSSClass(oElem, 'a-popup');
				
				if (oElem.className.indexOf('sized-popup') != -1) {
					fWinFunct = function(e) {
				
						// Target of click is the anchor.
						var oHref = getTarget(e);
						var sHref = attribute(oHref, 'href');
						
						EXTERNAL_WINDOW = openWindow(sHref, null, null, 0, 0, 0, 1, null, null, 2, 0, 0);
						
						if (e) {
							e.returnValue = false;
							if (e.preventDefault) {
								e.preventDefault();
							}
						} else {
							window.event.returnValue = false;
						}
						
						if (navigator.userAgent.indexOf('Safari') > 0) {
							EXTERNAL_LINK_SOURCE = oHref;
							EXTERNAL_LINK_URL = oHref.href;
							EXTERNAL_LINK_TARGET = oHref.target;
							
							oHref.href = '#';
							oHref.target = '_self';
							
							setTimeout('EXTERNAL_LINK_SOURCE.href = EXTERNAL_LINK_URL; EXTERNAL_LINK_SOURCE.target = EXTERNAL_LINK_TARGET;', 1);
						}
					}
				}
			}
			
			if (fWinFunct != null) {
				addEvent(oElem, 'click', fWinFunct);
			}
		}
    }
}
