/*

Google Analytics Tracker

Adds explicit tracking calls to all external sites and non-webpage document links.
Additionally, if Google's site overlay is detected, it dynamically rewrites the actual destination
of those links, so the overlay shows accurate stats (keep in mind that site overlay doesn't work
with sites served as application/xhtml+xml or similar).
Forgive the verbose "typeof(...)" juggling, but this prevents IE from throwing unsightly errors.


Usage:	simply load this script into your pages before or after the generic Google Analytics code
		it does not support the old urchin.js tracking
		
		<script type="text/javascript" src="/[PATH]/[TO]/google_analytics_tracker.js"></script>


Debug:	the script contains a Firebug http://getfirebug.com call to the console.log() function, but
		for alternative quick and dirty testing, comment out your regular Google Analytics code
		and replace	the default pageTracker object and trackPageview method with a simple javascript alert

		var pageTracker = {	_trackPageview: function(url) {	alert(url);	} }


Patrick H. Lauke / www.splintered.co.uk / Created 15 April 2008 / Last modification 21 September 2008

Valuable inspiration, code review and suggestions by Stuart Langridge / www.kryogenix.org

Released under Creative Commons Attribution-Share Alike 3.0 / http://creativecommons.org/licenses/by-sa/3.0/

*/

var gatracker = {
	
	init: function() {

		/* not running Google Analytics? */
		if (!((typeof(pageTracker) == 'object') && (typeof(pageTracker._trackPageview) != 'undefined'))) { return false; }

		/* customise these as needed */
		var prefix_outbound = '/stats/external/'; // "fake" directory where all links to external sites will show up in analytics
		var prefix_document = '/'; // if '/', document links are tracked in their "native" location. otherwise, these will be tracked in a separate "fake" directory
		/* list of file extensions to log as "document" links - yes, it's horrible...just take out the ones you know for sure you don't need to track */
		var reg = /^.*\.(7z|aco|app|avi|bmp|bzip|cr2|deb|dmg|dng|doc|docx|dot|eps|fla|flv|gif|gzip|iso|jpg|jpeg|js|mov|mpeg|mp3|mp4|mpg|odt|otf|pde|pdf|pdp|phps|pkg|png|ppt|ps|psd|xls|xpi|rar|raw|rtf|sit|sitx|swf|tar|tga|tif|torrent|ttf|txt|wma|wmv|zip)$/i;
		
		/* is Google's site overlay running? Check if the overlay's typical <script ... id="_gasojs"> is present */
		var siteOverlay = document.getElementById('_gasojs') !== null;

		var links = document.getElementsByTagName('a');
		for (var i=0,l=links.length;i<l;i++) {
			if (links[i].protocol+links[i].hostname != window.location.protocol+window.location.hostname) {
				/* outbound link */
				if (siteOverlay) {
					/* trick Google's site overlay to display correct stats */
					links[i].href = prefix_outbound+links[i].href.replace(/:\//g,'');
					/* prevent the now tricked-out link from working */
					gatracker.addEvent(links[i],'click', gatracker.preventDefault);
				} else {
					/* add tracker call to link's click event */
					links[i].tracker_url = prefix_outbound+links[i].href.replace(/:\//g,'');
					gatracker.addEvent(links[i],'click',gatracker.call_tracker);
				}
			} else if (reg.exec(links[i].href)) {
				/* document link */
				if (siteOverlay) {
					/* trick Google's site overlay to display correct stats */
					links[i].href = prefix_document+links[i].pathname.replace(/^\//g,'');
					/* prevent the now tricked-out link from working */
					gatracker.addEvent(links[i],'click', gatracker.preventDefault);
				} else {
					/* add tracker call to link's click event */
					links[i].tracker_url = prefix_document+links[i].pathname.replace(/^\//g,'');
					gatracker.addEvent(links[i],'click',gatracker.call_tracker);
				}
			}
		}
		return true;
	},
	
	addEvent: function(obj,type,fn) {
		/* quick and dirty cross-browser way to attach events */
		if (obj.attachEvent) {
			obj.attachEvent('on'+type,fn);
		} else {
			obj.addEventListener(type,fn,false);
		}
	},
	
	preventDefault: function(e) {
		if (e.preventDefault) {
			e.preventDefault();
		} else {
			/* of course, IE likes it a little different... */
			e.returnValue = false;
		}
	},

	
	call_tracker: function(e) {
		if (window.event) { e = window.event.srcElement; } else { e = e.target;	}
		/* fix to handle image links */
		if (e.nodeName.toLowerCase()!='a') { e = e.parentNode; }
		pageTracker._trackPageview(e.tracker_url);
		/* Firebug debug message - uncomment if needed */
		// if ((typeof(console) == 'object') && (typeof(console.log) != 'undefined')) { console.log('google tracker: '+e.tracker_url); }
	}

};

/* use the object's own addEvent to attach its init function to the window's onload event */
gatracker.addEvent(window,'load',gatracker.init); 
