/*
 * trackExternalLinks()
 *
 * Setup extended (external/download/mailto) link tracking for Google Analytics
 * This code depends on jQuery's $ object and the Google Analytics pageTracker
 * object being available.  This function should be called from within jQuery's
 * $(document).ready(function() { ... }); or your equivalent of that.
 * 
 * The single parameter that is passed is an object with one or more properties
 * that can override default behavior.
 *
 *   p['cancel'] : indicates the return value for the click handler.  This defaults 
 *                 to true, which results in navigation taking place.  Set this to
 *                 false to cancel navigation when clicking links; useful when
 *                 testing.
 *
 * p['trackers'] : This is an optional parameter.  This should be an array of
 *                 Google Analytics tracker objects.  If you're using the GA
 *                 default "pageTracker" object, you can omit this parameter.
 *                 If you have multiple trackers, add each one to the array
 *                 and each tracker will have a _trackPageView call performed.
 */
function trackExternalLinks(p) {
  var ret_val = (p['cancel'] != null) ? p.cancel : true;
  var trackers = (p['trackers'] != null && p['trackers'].length > 0) ? p['trackers'] : [pageTracker];
  

  // Modify the contents of this array to include the file extensions (URL endings) we will
  // consider downloadable content.  If your downloadable content is served up dynamically
  // via a script, consider appending a query string parameter to induce tracking on that link.
  //
  // e.g. if you server PDFs with something like...
  //
  //    /downloads/serve-document.php?content=12345
  //
  // ...adapt that to something along the lines of...
  //
  //    /downloads/serve-document.php?content=12345&file=something.pdf
  //
  // These array elements will ultimately be used in a regular expression, so feel
  // free to use regex functionality like 'mpe?g' to match either 'mpg' or 'mpeg'.
  //
  var download_extensions = ['pdf','docx?','xls','zip','exe','dmg','ppt','vsd','png','jpe?g','gif','avi','mpe?g','mp3','mp4','wmv','mov','qt','txt','css','js'];
 
  var pattern = '\.(?:' + download_extensions.join('|') + ')($|\\&|\\?)'; // build the pattern
 
  var download_rgx = new RegExp(pattern); // create the regex

  $('a').each(function() { // iterate over each <a> element
    
    if (this.protocol == 'mailto:') { // track mailto: links
      
      $(this).click(function() {
        var track_address = '/MAILTO/' + escape($(this).attr('href').substr(7));
        $.each(trackers, function() {
          this._trackPageview(track_address);
        });
        return ret_val;
      }); // click()
      
    } else if (this.protocol == 'http:' || this.protocol == 'https:') { // track other (http(s)) links
      
      if (this.hostname.toLowerCase() != window.location.hostname.toLowerCase()) { // is this an external link?
        
        $(this).click(function() {
          var link_type = this.protocol == 'http:' ? '' : '-SSL';
          var offset = this.protocol == 'http:' ? 7 : 8; // http:// vs https://
          var track_address = '/EXTERNAL' + link_type + '/' + escape($(this).attr('href').substr(offset));
          $.each(trackers, function() {
            this._trackPageview(track_address);
          });
          return ret_val;
        }); // click()
        
      } else if ($(this).attr('href').match(download_rgx)) { // is this a download link?
        
        $(this).click(function() {
          var slash = $(this).attr('href')[0] == '/' ? '' : '/';
          var track_address = '/DOWNLOAD' + slash + escape($(this).attr('href'));
          $.each(trackers, function() {
            this._trackPageview(track_address);
          });
          return ret_val;
        }); // click()
        
      } // external vs. download?
      
    } // mailto vs. href?
    
  }); // each <a> element
  
} // trackExternalLinks()
