/*************************
Georgetown University
Google Analytics Async Enhancements
*************************/

/********** gaAddons **********/
/*
* gaAddons FREE v1.0, Copyright 2011, Stéphane Hamel - http://gaAddons.com
* Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* Code fixes and enhancements by Ignatius Hsu
* This code is provided as is, without guarantee or support.
*/

// removed regex helper fn (jQuery native)
// rewrote Track Downloads
// rewrote Track Outbound Links
// added additional master tracker
// revised init - $(document).ready(funtion())
// deprecated code - changed .live('click',...); to .click(...);

$(document).ready(function() {

    // TRACK DOWNLOADS
     $('a').filter(function() {
            return this.href.match(/\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|txt)/i);
          }).click(function() {
            alert('Download Test, file URL: ' + this.href);
            _gaq.push(['_trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')]);
            _gaq.push(['master._trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')]);
         });

    // TRACK ERRORS
    // 404 File Not Found         
     if (document.title.search(/File Not Found/i) !== -1) {
        alert('Error 404 Test, Page Title: ' + document.title);
        _gaq.push(['_trackPageview', '/virtualpageview/404/' + location.host + location.pathname + '?from=' + document.referrer]);
        _gaq.push(['master._trackPageview', '/virtualpageview/404/' + location.host + location.pathname + '?from=' + document.referrer]);
    }    

    // TRACK MAILTO    
    $('a[href^="mailto"]').click(function() {
        alert('mailto Test, Email: ' + this.href);
        _gaq.push(['_trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
        _gaq.push(['master._trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
    });         

    // TRACK OUTBOUND LINKS
    // external to georgetown.edu
    $('a').not(function() {
            return this.href.match(/^mailto:|(https?:\/\/[a-z0-9\-\.]{0,26}georgetown\.edu)/i);
    }).click(function(event) {
          alert('Outbound Link Test URL:' + this.href);
        _gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
        _gaq.push(['master._trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
    });
    
});
