//	Javascript to tag file downloads and external links in Google Analytics
//	To use, place reference to this file should be placed at the bottom of all pages, 
//	just above the Google Analytics tracking code.
//	All outbound links and links to non-html files should now be automatically tracked.
//
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	Created by: 	Colm McBarron, colm.mcbarron@iqcontent.com
//	Last updated: 	12-Feb-2006
//
//	Updated by:		Niamh Phelan niamh.phelan@iqcontent.com
//	On:				22-Jul-2008
//	For:			Upgrade to ga.js	
//
//	Updated by:		Peter McKenna peter.mckenna@iqcontent.com
//	On:				07-Nov-2008
//	For:			Track mailto: links and restructure how virtual
//          		pageviews are structured
//
//	Updated by:		Peter McKenna peter.mckenna@iqcontent.com
//	On:				19-Feb-2009
//	For:			Fixed up some problems with how Internet 
//					Explorer 6 was tracking links, and some minor
//					Firefox issues.
//
//	Updated by:		Peter McKenna peter.mckenna@iqcontent.com
//	On:				23-Oct-2009
//	For:			Fixed IE issue when link is wrapped around an image
//
//
//	Updated by:		Dave Smith smithd7@tcd.ie
//	On:				18-Nov-2011
//	For:			Made code Async Snippet compatibile and kept the Traditional Snippet code available when used.
//                  Added Office 2007 extensions docx, xlsx, pptx; and m4v
//                  Fixed browser compatibility issue that was introduced when IE wrapped image issue was fixed.
//
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  GA Tag Links: Version 1.2.2
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var hrefs = document.getElementsByTagName("a");
var link_path = "";

for (var l = 0; l < hrefs.length; l++) {
	try {
		// Add the hostname and link location into variables
		var link_path = hrefs[l].pathname;
		var link_location = String(hrefs[l]);

		// Check if it's a mail link
		if (link_location.match(/^mailto:/i)) {
			addmailtotrackerlistener(hrefs[l]);
		}
		// Check to see if the link is an internal link
		else if (location.host == hrefs[l].hostname) {
			if(link_path.match(/\.(doc|docx|pdf|xls|xlsx|ppt|pptx|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|m4v|avi|wmv|mp3)$/)) {
				addtrackerlistener(hrefs[l]);
			}
		}
		else {
			addtrackerlistener(hrefs[l]);
		}
	}
	catch(err) { }
}

// Add a listener to matching file links
function addtrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackfiles, true);
	}
	else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackfiles);
	}
}

// Add a special listener to mailto: links
function addmailtotrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackmailto, true);
	}
	else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackmailto);
	}
}

// Track file links
function trackfiles(e) {
	e = e || window.event; // Compatibility.
	// Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
	if (!e.target ) {
		e.target = e.srcElement || document;
	}
	// Target should not be a text node (#504, Safari)
	if (e.target.nodeType === 3 ) {
		e.target = e.target.parentNode;
	}
	if (e.target.tagName.toLowerCase() !== 'img') {
		var file_path = "";
		// Track an external link
		var destination_host = (e.srcElement) ? e.srcElement.hostname : this.hostname;
		if (location.host != destination_host) {
			file_path = "/virtual/exlink/" + cleanURL(window.location, true) + '/' + ((e.srcElement) ? e.srcElement.hostname : this.hostname);
			file_path = file_path + ((e.srcElement) ? "/" + cleanURL(e.srcElement.pathname, false) : this.pathname);
		}
		// Track an internal link
		else {
			file_path = ((e.srcElement) ? "/" + e.srcElement.pathname : this.pathname);	
			var file_details = file_path.split('/');
			file_path = cleanURL(window.location, true) + '/' + file_details[(file_details.length-1)];
			file_path =  "/virtual/download/" + file_path;
		}
		// Traditional Snippet.
		if (typeof pageTracker !== 'undefined') {
			pageTracker._trackPageview(file_path);
		}
		// Async Snippet.
		if (typeof _gaq !== 'undefined') {
			_gaq.push(['_trackPageview', file_path]);
		}
	}
}

// Generate page view for a mailto: link
function trackmailto(e) {
	// Event Compatibility
	e = e || window.event;
	// Fix target property, if necessary (IE 6/7/8 & Safari2)
	if (!e.target ) {
		e.target = e.srcElement || document;
	}
	// Target should not be a text node (Safari)
	if (e.target.nodeType === 3 ) {
		e.target = e.target.parentNode;
	}
	if (e.target.tagName.toLowerCase() !== 'img') {
		var email = ((e.srcElement) ? e.srcElement.href : this.href).substring(7);
		var url = cleanURL(window.location, true);
		var mail_path = '/virtual/mailto/'+url+'/'+email;
		
		// Traditional Snippet.
		if (typeof pageTracker !== 'undefined') {
			pageTracker._trackPageview(mail_path);
		}
		// Async Snippet.
		if (typeof _gaq !== 'undefined') {
			_gaq.push(['_trackPageview', mail_path]);
		}
	}
}

// Clean leading & trailing slashes
function cleanURL (url, end) {
	var url = url.toString();
	var urlLen = url.length;
	
	if (end) {
		if (url.charAt((urlLen-1))=='/')
			url = url.substring(0,(urlLen-1));
	}
	else {
		if (url.charAt(0)=='/')
			url = url.substring(1,urlLen);
	}
	return url;
}
