﻿function initExternalLinks() {
 // Gather list of all links marked with class 'external'
 var i, a;
 var imageURL = '/images/css_images/new_window3.gif';
 for (i=0; (a=document.getElementsByTagName('a')[i]); i++) {
  
  if (a.getAttribute('class') == 'external' || a.className == 'external' || a.getAttribute('class') == 'popup' || a.className == 'popup') {
   // set onclick to open a new window with the href rather than in the same window
   a.onclick = function(e) {
    openWin(this.href);
    return false;
   };
   // add the image, hidden text and title attribute
   if (document.createElement) {
    if(a.getAttribute('class') == 'external' || a.className == 'external') {  
     if (!(a.hasChildNodes() && a.firstChild.nodeName=='IMG')) {  
      var elem = document.createElement("img");
      elem.setAttribute('src', imageURL);
      elem.setAttribute('alt', 'External link icon');
      elem.setAttribute('title', 'External link');
      var newElem = a.parentNode.insertBefore(elem, a);
      newElem.className = 'externalImage';
     }
    }
   }
   // insert a hidden span element after the link, explaining that the links opens in a new window
 var elem = document.createElement("span");
 var textelem = document.createTextNode(' (opens a new window)');
 elem.appendChild(textelem);
 insertAfter(elem, a);
 elem.setAttribute('class', 'hidden');
 elem.className = 'hidden';
 
 // insert a title attribute
 var newtitle = (a.getAttribute('title') == null || a.getAttribute('title') == '') ? 'Opens a new window' : a.getAttribute('title') + ' (opens a new window)';
 a.setAttribute('title', newtitle);
  } // end if class == external || popup
 } // end for loop through all links
} // end function initExternalLinks()
function openWin(url) {
 var externalWin = window.open(url);
 externalWin.focus();
}
/* insertAfter method - thanks to Jeremy Keith */
function insertAfter(newElement, targetElement) {
 var parent = targetElement.parentNode;
 if (parent.lastChild == targetElement) {
  parent.appendChild(newElement);
 } else {
  parent.insertBefore(newElement, targetElement.nextSibling);
 }
}