// Set of Google Analytics extensions
// Works with the asynchronous Google Analytics
// Requires jQuery and jQuery Cookies, if not present, it will load them automatically

$(document).ready(function() {
	//loop though each anchor element
	$('a[href]').each(function () {
	
		var href = $(this).attr('href');
		var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|rar|7z)$/i;
	
		//check for links starting with http or https, making sure that links to our own domain are excluded
		if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			$(this).click(function () {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'outbound', extLink, location.href]);
			});
		}
		//check for links starting with mailto:
		else if (href.match(/^mailto\:/i)) {
			$(this).click(function () {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'mailto', mailLink, location.href]);
			});
		}
		//check for links with file extension that match the filetypes regular expression:
		else if (href.match(filetypes)) {
			$(this).click(function () {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
				_gaq.push(['_trackEvent', 'download-' + extension, filePath, location.href]);
			});
		}
	});
});

