/*!
 * Entourage 1.0.0 - Automatic Download Tracking for Asynchronous Google Analytics
 *
 * Copyright (c) 2011 by Tian Valdemar Davis (http://techoctave.com/c7)
 * Licensed under the MIT (http://en.wikipedia.org/wiki/MIT_License) license.
 */
(function() {
onload = function() {
	//Setup an onclick event handler for each link
    var links = document.links;
    for(var i = 0; i < links.length; i++) {
		//Call Entourage whenever the link is clicked
        links[i].onclick = Entourage;
    }
};

Entourage = function() {
	var fileTypes = /\.doc$|\.eps$|\.jpg$|\.png$|\.svg$|\.xls$|\.ppt$|\.pdf$|\.xls$|\.zip$|\.txt$|\.vsd$|\.vxd$|\.js$|\.css$|\.rar$|\.exe$|\.dmg$|\.wma$|\.mov$|\.avi$|\.wmv$|\.mp3$/i;
	var pathname = this.pathname; //The link object is now available in "this"
	var fileName;
	var autograph;
	
	//File type match found
	if(pathname.match(fileTypes)) {
		//Get the file name
		fileName = GetFileName(pathname);
		
		//Add file to the Google Analytics Queue
		autograph = '/download/' + fileName;
		_gaq.push(['_trackPageview', autograph]);
	}
};

//Get true FileName from link pathname
GetFileName = function(pathname) {
	//Remove the anchor at the end (if one exists)
	pathname = pathname.substring(0, (pathname.indexOf("#") == -1) ? pathname.length : pathname.indexOf("#"));
	
	//Removes the query after the file pathname (if one exists)
	pathname = pathname.substring(0, (pathname.indexOf("?") == -1) ? pathname.length : pathname.indexOf("?"));
	
	//Removes everything before the last slash in the path
	pathname = pathname.substring(pathname.lastIndexOf("/") + 1, pathname.length);
	
	return pathname;
};
})();
