/*!
 * Entourage 1.2.0 - Automatic Download Tracking for Asynchronous Google Analytics
 *
 * Copyright (c) 2011 by Tian Valdemar Davis (http://techoctave.com/c7)
 * Additions for email and external domains by Paul Christersson Frend
 * Licensed under the MIT (http://en.wikipedia.org/wiki/MIT_License) license.
 *
 * Learn More: http://techoctave.com/c7/posts/58-entourage-js-automatic-download-tracking-for-asynchronous-google-analytics
 */
(function() {
var entourage = new (function() {
	this.VERSION = "1.2.0";
	
	//Get true FileName from link pathname
	var 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). indexOf returns -1 if "?" can't  be found. so if it returns -1, insert pathname.length, else insert pathname.indexOf("?")
		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;
	};

	var autograph = function() {
		
		//console.log("autograph");
		var whitelist, pathname, match, fileName, associate, link, domains, mailmatch, domainmatch;
		
		whitelist = /\.pdf$|\.zip$|\.od*|\.doc*|\.xls*|\.ppt*|\.exe$|\.dmg$|\.mov$|\.avi$|\.mp3$/i;
		domains = /queenslandtenniscentre\.com\.au|activetribes\.com\.au|uqlifestyleprogram\.com\.au|uqsport\.com\.au|uqs\.com\.au|sport\.uq\.edu\.au/gi;
		link = this.href;
		pathname = this.pathname; //The link object is now available in "this"
		
		match = pathname.match(whitelist);
		mailmatch = link.match(/mailto:/); //checks if mailto is present
		domainmatch = link.match(domains);//checks if link is going to external domain
		
		if (typeof mailmatch !== "undefined" && mailmatch !== null) {
			//Get the email address
			var emailurl = link.substring(7);
			
			//Add file to the Google Analytics Queue
			//console.log("associate: " + associate);
			_gaq.push(['_trackEvent', 'email', 'click', emailurl]);
			
		} else if (typeof domainmatch !== "undefined" && domainmatch == null) {
			
			//Get the external domain
			var domainurl = this.host;
			
			//Add file to the Google Analytics Queue
			//console.log("associate: " + associate);
			_gaq.push(['_trackEvent', 'Outbound links', 'click', domainurl]);
			
		} 
		//Compare the fileType to the whitelist
		else if (typeof match !== "undefined" && match !== null) {
			//Get the file name
			fileName = getFileName(pathname);

			//Add file to the Google Analytics Queue
			associate = '/downloads/' + fileName;
			
			//console.log("associate: " + associate);
			
			_gaq.push(['_trackPageview', associate]);
		}		
		
    };

	this.initialize = function() {
		//console.log("initialize");
		var links = document.links;
		
	    for (var i=0, l=links.length; i<l; i++) {
			//Call Entourage whenever the link is clicked.
	        
			if (typeof links[i].onclick !== "function"){
			links[i].onclick = autograph;
			}
	    }
    };
})(); //Entourage.js


//Add entourage to the global namespace
window.entourage = entourage;

//Execute entourage onload - ensuring links are present in the DOM
window.onload = entourage.initialize;
})();
