// Google analytics extension to track downloads and external links
var google_analytics_ext = {
	initialize: function(){
		// Fire up the link analyzing functions -> window.onload
		google_analytics_ext.initDownloadTracking();
		google_analytics_ext.initExternalLinkTracking();
		
	},
	
	// Tell Google to track this string as a "hit"
	track_link: function(tracking_string){
		if ((typeof pageTracker!='undefined')) { 
			pageTracker._trackPageview(tracking_string);
		}
	},
	
	// Look for file downloads and track them
	initDownloadTracking: function(){
		google_analytics_ext.links = document.getElementsByTagName('a');
		for (var i=0; i < google_analytics_ext.links.length; i++) {
			var current_link = google_analytics_ext.links[i];
			// Check anchor with regular expression to determine if it is a file download type
			if(current_link.href.toLowerCase().match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/)){
				// Piggyback the click and Pass anchor string to tracker
				addEvent(current_link, 'click', function(){google_analytics_ext.track_link(this.href); return true;});
			}
		}		
	},
	
	// Look for external links to other sites, and track them.
	initExternalLinkTracking: function(){
		google_analytics_ext.links = document.getElementsByTagName('a');
		for (var i=0; i < google_analytics_ext.links.length; i++) {
			var current_link = google_analytics_ext.links[i];
			// Check anchor against the current host to determine external urls.
			if(window.location.host != current_link.hostname){
				// Piggyback the click and Pass anchor string to tracker
				addEvent(current_link, 'click', function(){google_analytics_ext.track_link(this.href); return true;});
			}
		}
	}
	
}
// End extension object.



// Just in case we don't have prototype or whatever
// Some utility code to add events
 function addEvent( obj, type, fn ) {
   if ( obj.attachEvent ) {
     obj['e'+type+fn] = fn;
     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
     obj.attachEvent( 'on'+type, obj[type+fn] );
   } else
     obj.addEventListener( type, fn, false );
 }
 function removeEvent( obj, type, fn ) {
   if ( obj.detachEvent ) {
     obj.detachEvent( 'on'+type, obj[type+fn] );
     obj[type+fn] = null;
   } else
     obj.removeEventListener( type, fn, false );
 }

// Start the tracking on window load
addEvent(window, "load", google_analytics_ext.initialize);