$(function(){
  
  var external;

  //Define action and string to send to GA
  var gaCategories = [
    {act:'Download',cat:'.pdf'},
    {act:'Download',cat:'.xls'},
    {act:'Download',cat:'.doc'},
    {act:'Click',cat:'mailto:'}
  ];
 
  //Loop through download categories and get tracking info
  $.each(gaCategories, function(i,val){
 
      //When link contains category
      $("a[href*=" + val.cat + "]").click(function(e){
       
        //Stop the link returning while we gather tracking information
        e.preventDefault();
       
        //Store tracking details
        var category = val.cat;
        var action = val.act;
        var href = $(this).attr("href");
        
        //Check if link is external
        if($(this).attr('target') == "_blank"){
          external = true;
        }
        
        //Pass these details to our google tracking code
        gaEventTracking(category, action, href);
        
      });
     
  });
 
  function gaEventTracking(category, action, href) {
   
    //Push the event to Google Analytics
    _gaq.push(['_trackEvent', category, action, href]);
   
    //Return the original href
    if(external == true){
      window.open(href, '_blank');
    }else{
      window.location = href;
    }
    
  };
  
});
