﻿///////
// Credits for parts of code:
// Stéphane Hamel - gaAddons.js - http://immeria.net
//  Inspired by the work of Justin Cutroni - Google Analytics Short Cut - http://gashortcut.com/

//Should these events be tracked at all?
var bDoTrackOutbound = false; //set to true to track outbound events
var bDoTrackDownloads = true; //set to true to track download events
var bDoTrackViews = true; //set to true to track view events

//Should it be tracked as an event or pageview
var bUseEventForOutbound = true; // Set to false to use trackPageView for outbount links (if bDoTrackOutbound = true)
var bUseEventForDownload = true; // Set to false to use trackPageView for downloads (if bDoTrackDownloads = true)
var bUseEventForView = true; // Set to false to use trackPageView for downloads (if bDoTrackViews = true)
// Indicate each file extension that needs to be tracked, downloadTypes
// is the regular expression that matches downloadable files, viewTypes is the reg expr that matches
// viewed images
var downloadTypes = new RegExp(/\.(docx*|xlsx*|pptx*|zip|pdf|jpg)$/i);
var viewTypes = new RegExp(/\.(gif|png|jpg)$/i);

///////
/// No need to change anything below this line
// Initialize external link and download handlers
if (document.getElementsByTagName) {
    var hrefs = document.getElementsByTagName('a');
    for (var l = 0, m = hrefs.length; l < m; l++) {
        if (hrefs[l].hostname == location.host) {
            if (bDoTrackDownloads == true && downloadTypes.test(hrefs[l].pathname)) startListening(hrefs[l], "click", trackDownloads);
            if (bDoTrackViews == true && viewTypes.test(hrefs[l].pathname)) startListening(hrefs[1], "click", trackViews);
        }
        else {
            if (bDoTrackOutbound == true) startListening(hrefs[l], "click", trackExternalLinks);
        }
    }// end for

        /*if (hrefs[l].hostname == location.host && downloadTypes.test(hrefs[l].pathname))
            if (bDoTrackDownloads == true) startListening(hrefs[l], "click", trackDownloads);
        else if (hrefs[l].hostname == location.host && viewTypes.test(hrefs[l].pathname))
            if (bDoTrackViews == true) startListening(hrefs[1], "click", trackViews);
        else
            if (bDoTrackOutbound == true) startListening(hrefs[l], "click", trackExternalLinks);
        */
}
           
function startListening(obj, evnt, func){
    if (obj.addEventListener) 
        obj.addEventListener(evnt, func, false);
    else 
        if (obj.attachEvent) 
            obj.attachEvent("on" + evnt, func);
}

function trackDownloads(evnt) {
    if (typeof(pageTracker) != "object")
        return;
    bUseEventForDownload ? pageTracker._trackEvent("Download", "Download", (evnt.srcElement) ? "/Download/" + evnt.srcElement.pathname : this.pathname) : pageTracker._trackPageView("/download/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname);
}

function trackViews(evnt){
    if (typeof(pageTracker) != "object")
        return;
    bUseEventForDownload ? pageTracker._trackEvent("View", "View", (evnt.srcElement) ? "/View/" + evnt.srcElement.pathname : this.pathname) : pageTracker._trackPageView("/view/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname);
}

function trackExternalLinks(evnt){
    if (typeof(pageTracker) != "object")
        return;
    var elmnt = evnt.srcElement;
    if (elmnt) {
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
        bUseEventForOutbound ? pageTracker._trackEvent("outbound", "click", elmnt.hostname + "/" + elmnt.pathname + elmnt.search) : pageTracker._trackPageView("/outbound/" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search);
    }
    else 
        bUseEventForOutbound ? pageTracker._trackEvent("outbound", "click", this.hostname + this.pathname + this.search) : pageTracker._trackPageView("/outbound/", this.hostname + this.pathname + this.search);
}/* */