///////
// gaAddons.js
// Stéphane Hamel - http://immeria.net
// You are free to modify and redistribute this script as long as you keep the reference to the authors
// If you have great ideas, please let me know so I can integrate them for futur releases!
//
// Changelog:
//  v2.1 - Jan. 22nd 2009 - Set bUseEventForOutbout and bUserEventForDownload to toggle use of Events or Page Views
// 	v2.0 - Jan. 2009 - Use Google Analytics Events to track downloads and external links
//  v1.0 - Inspired by the work of Justin Cutroni - Google Analytics Short Cut - http://gashortcut.com/
///////

// Indicate each file extension that needs to be tracked, fileTypes is the regular expression that matches downloadable files
var fileTypes = new RegExp(/\.(docx*|xlsx*|pptx*|exe|doc|zip|pdf|xpi|msi)$/i);
var bUseEventForOutbound = false; // Set to false to use trackPageView for outbount links
var bUseEventForDownload = false; // Set to false to use trackPageView for downloads

///////
/// No need to change anything below this line
// Initialize external link and download handlers
function addLinkerEvents() {
  if (document.getElementsByTagName) {
    var hrefs = document.getElementsByTagName('a');
    for (var l = 0, m = hrefs.length; l < m; l++) 
        if (hrefs[l].hostname == location.host && fileTypes.test(hrefs[l].pathname)) 
            startListening(hrefs[l], "click", trackDocuments);
        else 
            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 trackDocuments(evnt){
    if (typeof(firstTracker) != "object")
        return;
      
    if (bUseEventForDownload)
        firstTracker._trackEvent("download", "click", (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname);
    else 
    {
        var pageViewName = "/download/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname;
        firstTracker._trackPageview(pageViewName);
    }
}

function trackExternalLinks(evnt){
    if (typeof(pageTracker) != "object") 
        return;
    var elmnt = evnt.srcElement;
    if (elmnt) {
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
        bUseEventForOutbound ? firstTracker._trackEvent("outbound", "click", elmnt.hostname + "/" + elmnt.pathname + elmnt.search) : firstTracker._trackPageView("/outbound/" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search);
    }
    else 
        bUseEventForOutbound ? firstTracker._trackEvent("outbound", "click", this.hostname + this.pathname + this.search) : firstTracker._trackPageView("/outbound/", this.hostname + this.pathname + this.search);
}
/// EOF ///