// We dont want the try adding the tracking code until the page links are loaded
if (document.addEventListener) { 
document.addEventListener("DOMContentLoaded", addEvents, null); // Firefox
} else {
addEvents(); // IE : Call the function immediately because the script is referenced with the defer attribute supported by IE
}

function addEvents()
{
// quit if this function has already been called 
if (arguments.callee.done) return; 
// flag this function so we don't do the same thing twice 
arguments.callee.done = true;
for (i=0; i <document.links.length; i++) 
{
var x = document.links[i];
// Only attach tracking code to specific file types
var extensions = new RegExp(".+\.(zip|pdf|xls|doc|docx|txt|ppt|pptx|rar|exe|xml|rtf)$");
var doc = x.href.toLowerCase().match(extensions);

   //track external links
   var ref = externalRef(x.href);
   // If external...
   if (ref != null) {
      if (x.attachEvent)
      {
         x.attachEvent('onclick', function () {TrackItExt(window.event.srcElement)}); // IE
      } 
      else {
         x.addEventListener('click', function () {TrackItExt(this)}, false); // Firefox
      }
   }
   else {
   //track download links
      if (doc)
      {
         if (x.attachEvent)
         {
            x.attachEvent('onclick', function () {TrackIt(window.event.srcElement)}); // IE
         } 
         else {
            x.addEventListener('click', function () {TrackIt(this)}, false); // Firefox
         }
      }
   }
}
}


function TrackIt(link)
{
// Remove the conversion to Lowercase if you are on a Case sensitive web server
strUrl = link.href.toLowerCase();
strUrl = strUrl.replace("http://","");
strUrl = strUrl.replace("https://","");
pageTracker._trackPageview('/download/'+strUrl);
//urchinTracker(link.href.toLowerCase());
}

function TrackItExt(link)
{
   var ref = externalRef(link.href);
   pageTracker._trackPageview(ref);
   //urchinTracker(link);
}


function externalRef(href) {
  var ret = null;
  // Look for URL's that start with http:// or https://
  var re = /^https?:\/\/(.*)$/;
  var matches = re.exec(href);
  // If the URL looks external...
  if (matches != null) {
    var url = matches[1];
    // Look for fully qualified inbound links
    
    re = /^www.think5.de(\/.*)?/;

    // If not inbound...
    if (re.exec(url) == null) {
      // Set the reference
      ret = "/outbound/" + url.toLowerCase();
    }
  }
  return ret;
}

