//	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
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// 	Modified by Pritesh Wadhia
// 	* Changed onclick event code to the new style analytics javascript. (code from gataglinks.js)
//	* Added friendly text to front of URL (internal/document, external/document, etc.) for easy identification.
//	* Changed the URL for internal documents - From: URL of page it was downloaded from. To: the URL of file location. 
//		Reason: This way the assignment number will always be displayed in the URL as long as the document is in a folder named the assignment number.
//	* Added target="_blank" to all document and external link anchor tags.
//	* Added tracking for mail:to links (code from gataglinks.js)
//	* Added tracking for links to external sites. (code from gataglinks.js)

reg_exp = /\.(doc|pdf|xls|ppt|zip|txt|vsd|js|css|vxd|rar|exe|wma|mov|avi|wmv|mp3)$/;

var hrefs = document.getElementsByTagName("a");
var link_path = "";
for (var l = 0; l < hrefs.length; l++) {
//alert("location.host=" + location.host + "hrefs[l].hostname=" + test);
	try {
		var link_path = hrefs[l].href;
		var link_location = String(hrefs[l]);			
			//if internal link...
			if (location.host == hrefs[l].hostname) {
				if (link_path.match(reg_exp)) {
					addtrackerlistener(hrefs[l]);
				}	
			} 
			//if External link...
			else 
			{
				// Check if it's a normal link or mail link
				(link_location.match(/^mailto:/i)) ? addmailtotrackerlistener(hrefs[l]) : 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);
	}
	obj.setAttribute('target', '_blank'); //added for good measure!
}

// 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 = "";
	host_name = (array_element.srcElement) ? array_element.srcElement.hostname : this.hostname;
	path_name = (array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname;
	doc_link = (path_name.match(reg_exp)) ? "document" : "link";
	ext_int = (location.host != host_name) ? "external" : "internal"; 
	file_path = "/" + doc_link + "/" + ext_int  + "/--/" + host_name + path_name;
	//alert(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 url = window.location;
	var mail_path = '/mailto/--'+url+'--/'+email;
	//alert(mail_path);
	pageTracker._trackPageview(mail_path);
}
