//	Javascript to tag file downloads, external links and mailto links in Google Analytics
//	To use, reference to this file should be placed at the bottom of all pages, 
//	just above the Google Analytics tracking code.
//	All outbound links, mailto links and links to non-html files should now be automatically tracked.
//
// 	Be aware that this tracks events using the new Google Analytics _trackPageview code. Need to make changes if using old Google Analytics code.
//

// addEvent manager - http://www.twinhelix.com/javascript/addevent/
if(typeof addEvent!='function'){var addEvent=function(o,t,f,l){var d='addEventListener',n='on'+t,rO=o,rT=t,rF=f,rL=l;if(o[d]&&!l)return o[d](t,f,false);if(!o._evts)o._evts={};if(!o._evts[t]){o._evts[t]=o[n]?{b:o[n]}:{};o[n]=new Function('e','var r=true,o=this,a=o._evts["'+t+'"],i;for(i in a){o._f=a[i];r=o._f(e||window.event)!=false&&r;o._f=null}return r');if(t!='unload')addEvent(window,'unload',function(){removeEvent(rO,rT,rF,rL)})}if(!f._i)f._i=addEvent._i++;o._evts[t][f._i]=f};addEvent._i=1;var removeEvent=function(o,t,f,l){var d='removeEventListener';if(o[d]&&!l)return o[d](t,f,false);if(o._evts&&o._evts[t]&&f._i)delete o._evts[t][f._i]}}

var hrefs = document.getElementsByTagName("a");
var link_path = "";

for (var l = 0; l < hrefs.length; l++) {
	try {
		var link_path = hrefs[l].pathname;
		if (location.host == hrefs[l].hostname) {
			if (link_path.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/i)) {
                addEvent(hrefs[l], 'click', trackfiles, true);
			}         	
		} else {
            addEvent(hrefs[l], 'click', trackfiles, true);
		}
	}
	catch(err) { }
}

function trackfiles(array_element) {
	file_path = "";
	if (this.protocol == "mailto:") {
		file_path = "/" + this.href;
	} else {
		if (location.host != this.hostname) {
			file_path = "/exlinks/" + ((array_element.srcElement) ? "/" + array_element.srcElement.hostname : this.hostname);
		}
		file_path = file_path + ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);
	}
	pageTracker._trackPageview(file_path);
}
