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|pdf|docx|xls|ppt|zip|wma|mov|avi|wmv|mp3)$/)) 
				{
					addtrackerlistener(hrefs[l]);
				}
				else if(link_path.match(/\/media-centre\/documents\//)) 
				{
					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(array_element) {
	var file_path = "";
	// Track an external link
	var destination_host = (array_element.srcElement) ? array_element.srcElement.hostname : this.hostname;
	if (location.host != destination_host){
	file_path = "/virtual/exlink/" + ((array_element.srcElement) ? array_element.srcElement.hostname : this.hostname);
	file_path = file_path + ((array_element.srcElement) ? "/" + cleanURL(array_element.srcElement.pathname, false) : this.pathname);
	}
	// Track an internal link
	else {
		file_path = ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);	
		var file_details = file_path.split('/');
		file_path = file_details[(file_details.length-1)];
		file_path =  (("/virtual/download/") + file_path);
	}
	pageTracker._trackPageview(file_path);
}

// Generate page view for a mailto: link
function trackmailto(array_element) {
	var email = ((array_element.srcElement) ? array_element.srcElement.href : this.href).substring(7);
	var mail_path = '/virtual/mailto/'+email;
	pageTracker._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;
}