/*

Google Analytics Tracker - adds explicit tracking calls to all outbound and document links in a page.
Additionally, if Google's site overlay is detected, it dynamically rewrites the actual destination
of those links, so the overlay shows accurate stats.

Patrick H. Lauke / www.splintered.co.uk / 15 April 2008

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

*/

var gatracker = {
	
	init: function() {

		/* customise these as needed */
		var prefix_outbound = '/stats/external/';
		var prefix_document = '/stats/documents/';
		
		/* is Google's site overlay running? */
		var siteOverlay = false;
		if(document.getElementById('_gasojs')) siteOverlay = true;

		if (!pageTracker) return false;
		var links = document.getElementsByTagName('a');
		for (i=0,l=links.length;i<l;i++) {
			if (links[i].protocol+links[i].hostname != window.location.protocol+window.location.hostname) {
				if (siteOverlay) {
					/* trick Google's site overlay to display correct outbound links*/
					links[i].href = prefix_outbound+(links[i].href.replace(/:\//g,''));
				} else {
					links[i].tracker_url = prefix_outbound+(links[i].href.replace(/:\//g,''));
					gatracker.addEvent(links[i],'click',gatracker.call_tracker);
				}
			} else if (/^.*\.(pdf|doc|docx|xls|xls|mp3|ppt|pptx|txt|mdb|exe|zip|jar|iso|xml|rm|wmv|odp|odt|ods|odg)$/.exec(links[i].href)) {
				if (siteOverlay) {
					/* trick Google's site overlay to display correct document links */
					links[i].href = prefix_document+(links[i].pathname.replace(/^\//g,''));
				} else {
					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) {
		if (obj.attachEvent) {
			obj.attachEvent('on'+type,fn);
		} else {
			obj.addEventListener(type,fn,false);
		}
	},
	
	call_tracker: function(e) {
		if (window.event) { e = window.event.srcElement; } else { e = e.target;	}
		/* fix to handle image links */
		if (e.nodeName!='A') e = e.parentNode;
		pageTracker._trackPageview(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);

