//	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
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

var hrefs = document.getElementsByTagName("a");
var link_path = "";
for (var l = 0; l < hrefs.length; l++) {
		try {
			var link_path = hrefs[l].pathname;
			//alert(location.host + " == " + hrefs[l].hostname + " \n " + link_path )
			if ((hrefs[l].hostname == "www."+location.host) || (hrefs[l].hostname == location.host)) {
				// this link is internal, look for a file type
				
				if (link_path.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/)) {
					// filetype found, add listner
					addtrackerlistener(hrefs[l]);
				}
			} else {
				
				// this link is external, or its undefined
				if (hrefs[l]==null || hrefs[l]==''){
					// its undefined
					
				} else {
					// its not undefined
					var host = hrefs[l].hostname;
					
					
					switch (host){
						
						// dont track exlinks to these domains ...
						case "oascentral.spineuniverse.com":
						case "ad.doubleclick.net":
						case "":
							break;
						
						default:
							//alert(host + " | " + hrefs[l]);
							addtrackerlistener(hrefs[l]);
							break;
					}
				}
			}
		}
		catch(err) { }
}


function addtrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackfiles, true);
		
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackfiles);
		
	}
	//obj.onclick();
	//trackfiles(this);
}

function trackfiles(array_element) {
	file_path = "";
	if ((location.host != this.hostname) && ( this.hostname != "www."+location.host)) {
		file_path = "/exlinks/" + ((array_element.srcElement) ? "/" + array_element.srcElement.hostname : this.hostname);
		//file_path = "/exlinks/" + ((array_element.srcElement) ? "/" + array_element.srcElement : this);
	} //else {
	file_path = file_path + ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);
	//}
	//alert(file_path);
	//trace =  'this.pathname:' + this.pathname + "\n";
	//trace +=  'array_element.srcElement.pathname:' + array_element.srcElement.pathname + "\n";
	//var trace =  'file_path:' + file_path + "\n";
	//alert(trace);
	//return false;
	if(pageTracker!=null){
		pageTracker._trackPageview(file_path);
	}
}