///////
// gaAddons.js
// 	Author: StÃ©phane Hamel - shamel67@gmail.com - http://immeria.net
//  Contributors:
//	- Andy Edmonds - http://alwaysbetesting.com
//	- Damon Gudaitis - http://www.damongudaitis.com/
//	
///////

var gaA_fileTypes,bUseEventForDownload,bUseEventForOutbound;
function gaEvents(){
	bUseEventForOutbound = true; // Set to false to use trackPageView for outbount links
	bUseEventForDownload = true; // Set to false to use trackPageView for downloads
	gaA_fileTypes = new RegExp(/\.(docx*|xlsx*|pptx*|exe|zip|pdf|xpi)$/i);
	
	if (document.getElementsByTagName) {
		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", trackDocuments);
			else 
				if (hrefs[l].hostname != location.hostname) 
					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 _gaq != "object") 
        return;
    bUseEventForDownload ? _gaq.push(['_trackEvent',"download", "click", (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname]) : _gaq.push(['_trackEvent',"/download/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname]);
}

function trackExternalLinks(evnt){
    if (typeof _gaq != "object") 
        return;
    var elmnt = evnt.srcElement;
    if (elmnt) {
		while (elmnt.tagName != "A") 
			elmnt = elmnt.parentNode;
		if (/http/.test(elmnt.protocol))
			bUseEventForOutbound ? _gaq.push(['_trackEvent',"outbound", "click", elmnt.hostname + "/" + elmnt.pathname + elmnt.search]) : _gaq.push(['_trackEvent',"/outbound/" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search]);
		if (elmnt.protocol == "mailto:") 
			bUseEventForOutbound ? _gaq.push(['_trackEvent',"mailto", "click", elmnt.href.replace(/mailto:/,"")]) : _gaq.push(['_trackEvent',"/mailto/" + elmnt.href.replace(/mailto:/,"")]);
	}
	else {
		if (/http/.test(this.protocol))
			bUseEventForOutbound ? _gaq.push(['_trackEvent',"outbound", "click", this.hostname + this.pathname + this.search]) : _gaq.push(['_trackEvent',"/outbound/", this.hostname + this.pathname + this.search]);
		if (this.protocol == "mailto:")
			bUseEventForOutbound ? _gaq.push(['_trackEvent',"mailto", "click", this.href.replace(/mailto:/,"")]) : _gaq.push(['_trackEvent',"/mailto/", this.href.replace(/mailto:/,"")]);
	}
}
