// add google tracking to static files
$(document).ready(function() {
	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
	$('a').each(function(){
		var href = $(this).attr('href');
		var rel = $(this).attr('rel');
		
		var relRegExp = /^ga\s([^\s]+)/i
		if (rel != undefined && (relRegExp.test(rel))) {
			$(this).click(function() {
				var cid = relRegExp.exec(rel);
				_gaq.push(['_trackEvent', 'AdWords', 'Conversion', cid[1]]);
			});
		}

		if (href != undefined && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			$(this).click(function() {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'Link', 'External', extLink]);
			});
		}
		else if (href != undefined && href.match(/^mailto\:/i)){
			$(this).click(function() {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'Link', 'Email', mailLink]);
			});
		}
		else if (href != undefined && href.match(filetypes)){
			$(this).click(function() {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var filePath = href.replace(/^https?\:\/\/(www.)nottingham\.ac\.uk\//i, '');
				_gaq.push(['_trackEvent', 'Link', 'Download-' + extension, filePath]);
			});
		}
	});
});
