

/***************************************
   Google Links Plugin for Google Analytics Tracking
   @author Karl Swedberg
   @version 1.0 (February 24, 2010)
   @requires jQuery v1.3+
   
   @options 
   these are the defaults. you can change them to whatever you want.
   
   extensions:   'doc,xls,exe,zip,pdf,swf',
   pathPrefix:   '/downloads/',
   emailPrefix:  null,
   google:       true // one of true | false | 'debug'

   }
   @examples
   **Change a default option globally:
        $.fn.links.defaults.google = 'debug';
   
   **Change the text and placement of a hint summary:
     $('#pdf').links({
       pathPrefix: '/somethingelse/',
     });
   
   
 ************************************** */
   

// might have to use next line if also including Prototype/Scriptaculous
jQuery.noConflict();

/***** I'm calling this method at the bottom of the file *****/




/************************************** */


(function($) {

$.fn.links = function(options) {
  var opts = $.extend(true, {}, $.fn.links.defaults, options);
  opts.exts = opts.extensions.split(/\s*,\s*/);
  
  this.each(function(event) {
    var ext = opts.extensions.replace(/\s+/g,'').replace(/,/g,'|'),
        extPattern = new RegExp('\.(' + ext + ')$');
       
        

    var $container = $(this);
      

    // google analytics tracking ....
    if (opts.google) {
      $container.click(function(event) {
        var $tgt = $(event.target).closest('a'),
            tgt = $tgt[0];

        // bail out if this isn't a link
        if (!$tgt.length) { return; }

        var thisPath = trackPath($tgt);

        if ( thisPath ) {
          
          if (opts.google != 'debug') {
            if (typeof pageTracker != 'undefined') {
              pageTracker._trackPageview(thisPath);
            } else if (typeof urchinTracker != 'undefined') {
              urchinTracker(thisPath);
            } else {
              return;
            }
          } else {
            if (window.console && console.log) { console.log(thisPath);}
            event.preventDefault();
          }
          
        }

      });
    }
  });
  
  function trackPath(el) {
    var tgt = el[0];
    var newPath = false;
    var thishref = el.attr('href');
    
    if ( /^mailto/.test(thishref) && opts.emailPrefix) {
      newPath = thishref.replace(/^mailto:([^\?]+)/, '$1');
      newPath = opts.emailPrefix + encodeURIComponent(newPath);
    } else if (tgt.pathname) {
      var linkPath = tgt.pathname.replace(/^\//,''),
          linkExt = linkPath.slice(linkPath.lastIndexOf('.'));
      linkExt = linkExt && linkExt.slice(1);
      if ( $.inArray(linkExt, opts.exts) > -1 ) {
        newPath = opts.pathPrefix + linkPath;
      } else if (location.hostname !== tgt.hostname) {
        newPath = tgt.href;
      }
    }

    return newPath;
  }

  return this;
};

$.fn.links.defaults = {
  extensions:   'doc,xls,exe,zip,pdf,ppt,swf',
  pathPrefix:   '/downloads/',
  emailPrefix:  null,
  google:       true // one of true, false, 'debug'
};



})(jQuery);

jQuery(document).ready(function($) {
  
  $('#container').links({
    extensions:   'doc,xls,zip,pdf,ppt',
    emailPrefix: '/internal_links/email/',
    pathPrefix: '/downloads/'
  });
});

