///////
// gaAddons.js
// 	Author: Stéphane Hamel
//  Contributors:
//	- Andy Edmonds
//	- Damon Gudaitis
//	
///////
// License: Version: MPL 1.1
// The contents of this file are subject to the Mozilla Public License Version
// 1.1 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
///////

var bUseEventForOutbound = true; // Set to false to use trackPageview for outbount links
var bUseEventForDownload = true; // Set to false to use trackPageview for downloads
var gaA_fileTypes = new RegExp(/\.(docx*|xlsx*|pptx*|exe|zip|pdf|xpi)$/i);
// Indicate each file extension that needs to be tracked, gaA_fileTypes is the regular expression that matches downloadable files
var gaA_pageTracker = pageTracker; // Should be set to the name of your tracker variable

var gaAddons = function(){

    var startListening = function(obj, evnt, func){
        if (obj.addEventListener) 
            obj.addEventListener(evnt, func, false);
        else 
            if (obj.attachEvent) 
                obj.attachEvent("on" + evnt, func);
    }

    var trackDocument = function(evnt){
        bUseEventForDownload ? gaA_pageTracker._trackEvent("download", "click", (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname) : gaA_pageTracker._trackPageview("/download/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname);
    }

    var trackExternalLink = function(evnt){
        var elmnt = evnt.srcElement;
        if (elmnt) {
            while (elmnt.tagName != "A") 
                elmnt = elmnt.parentNode;
            if (/http/.test(elmnt.protocol)) 
                bUseEventForOutbound ? gaA_pageTracker._trackEvent("outbound", "click", elmnt.hostname + "/" + elmnt.pathname + elmnt.search) : gaA_pageTracker._trackPageview("/outbound/" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search);
            if (elmnt.protocol == "mailto:") 
                bUseEventForOutbound ? gaA_pageTracker._trackEvent("mailto", "click", elmnt.href.replace(/mailto:/, "")) : gaA_pageTracker._trackPageview("/mailto/" + elmnt.href.replace(/mailto:/));
        }
        else {
            if (/http/.test(this.protocol)) 
                bUseEventForOutbound ? gaA_pageTracker._trackEvent("outbound", "click", this.hostname + this.pathname + this.search) : gaA_pageTracker._trackPageview("/outbound/" + this.hostname + this.pathname + this.search);
            if (this.protocol == "mailto:") 
                bUseEventForOutbound ? gaA_pageTracker._trackEvent("mailto", "click", this.href.replace(/mailto:/, "")) : gaA_pageTracker._trackPageview("/mailto/" + this.href.replace(/mailto:/));
        }
    }

    if (document.getElementsByTagName && typeof gaA_pageTracker == "object") {
        var hrefs = document.getElementsByTagName('a');
        for (var l = 0, m = hrefs.length; l < m; l++) 
            if (gaA_fileTypes.test(hrefs[l].pathname)) 
                startListening(hrefs[l], "click", trackDocument);
            else 
                if (hrefs[l].hostname != location.hostname) 
                    startListening(hrefs[l], "click", trackExternalLink);
    }
}

if (window.addEventListener) 
    window.addEventListener('load', function(){
        gaAddons()
    }, false);
else 
    if (window.attachEvent)
        window.attachEvent('onload', function(){
            gaAddons()
        });
