/* ----------------------------------------------------------------------

	GA Download Tracker beta 2.0
	by Ian Whitcomb
	
	http://www.xynthetik.com
	
	Simply add this javascript file and the Google Analytics tracking
	code to any page and this script will allow you to track file
	downloads for the specified filetypes.
	
	The default filetypes that get tracked are:
	PDF, XLS, MP3, ZIP, DOC, and BRF

   ---------------------------------------------------------------------- */

// Edit this array to set which file types are to be tracked.
var filetypes_to_track = Array("pdf", "xls", "mp3", "zip", "doc", "brf");

function getDomainInfo(url){
	var domain = new Object;
	
	domain.path = "";
	
	var protocol = url.split("://");
	var path = protocol[1].split("/");
	
	domain.protocol = protocol[0];
	domain.domain = path[0];
	for(i = 1; i < path.length; i++){
		domain.path = domain.path+"/"+path[i];
	}
	
	return domain;
}


function in_array(needle, haystack){
	for(i = 0; i < haystack.length; i++){
		if(haystack[i] == needle){
			return true;
		}
	}
	return false;
}

function initClicks(){

	var links = document.getElementsByTagName('a');
	
	for(i = 0; i < links.length; i++){
		links[i].onclick = function(){
			if(this.getAttribute("href")){
				var fileInfo = this.getAttribute("href").split(".");
				if(in_array(fileInfo[fileInfo.length-1], filetypes_to_track) == true){
					var thisPath = getDomainInfo(this.getAttribute("href"));
					pageTracker._trackPageview(thisPath.path); // Google Analytics Tracking Function
				}
			}
		}
	}

}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

addEvent(window, 'load', initClicks);