var Innonet = Innonet || {};
Innonet.gaTrackerAttach = function(context) {
  context = context || document;

  $('a', context).click( function() {
    // Expression to check for absolute internal links.
    var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
    // Expression to check for special links like gotwo.module /go/* links.
    var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
    // Expression to check for download links.
    var isDownload = new RegExp("\\.(7z|aac|avi|csv|doc*|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt*|rar|sit|tar|torrent|txt|wma|wmv|xls*|xml|zip|ods|odt|odp|rtf|wpd)$", "i");

    try {
      // Is the clicked URL internal?
      if (isInternal.test(this.href)) {
        // Is download tracking activated and the file extension configured for download tracking?
        if (isDownload.test(this.href)) {
          // Download link clicked.
          var extension = isDownload.exec(this.href);
          pageTracker._trackEvent("Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
        }
        else if (isInternalSpecial.test(this.href)) {
          pageTracker._trackPageview(this.href.replace(isInternal, ''));
        }
      }
      else {
        if ($(this).is("a[@href^=mailto:]")) {
          // Mailto link clicked.
          pageTracker._trackEvent("Mails", "Click", this.href.substring(7));
        }
        else {
          // External link clicked. Clean and track the URL.
          pageTracker._trackEvent("Outgoing links", "Click", this.href);
        }
      }
    } catch(err) {}
  });
};

$(document).ready(function() {
  Innonet.gaTrackerAttach(this);
});

