// script downloaded from http://www.iso-100.com/blog/post/updated-script-for-tracking-outbound-links-in-google-analytics-with-jquery/ 
// modified by Tom Atkinson to be backwards/forwards compatible with both synchronous and asynchronous tracker

if(typeof jQuery == 'function') { /* use jQuery if it exists because it is a more elegant solution */
	//alert("running jquery code");
	jQuery(function () {
		jQuery('a:not([href*="' + document.domain + '"])').click(function () {	
																		   
			// Here is 3 copies of the tracking, backwarsd and forwards compatible with 
			// both regular, rollup, and asynchronous tracking. it can fire to all trackers if present.
			try { // synchronous tracking:
				if (pageTracker) {
					pageTracker._trackPageview('/outgoing/' + jQuery(this).attr('href'));
					//alert("just called pageTracker");
				}
			} catch(e) {}
			try { // rollup synchronous tracking:	
				if (rollupTracker) {
					rollupTracker._trackPageview('/outgoing/' + jQuery(this).attr('href'));
					//alert("just called rollupTracker");
				}
			} catch(e) {}
			try { // asynchronous tracking:
				if (_gaq) {
					_gaq.push([ '_trackPageview', '/outgoing/' + jQuery(this).attr('href') ]);
					//alert("just called _gaq");
				} 
			} catch(e) {}	
			// end core tracking block
			
		});
	});
}
else { /* use regular Javascript if jQuery does not exist */
	window.onload = function () {
		var links = document.getElementsByTagName('a');
		//alert("running non jquery code");
		for (var x=0; x < links.length; x++) {
			links[x].onclick = function () {
				var mydomain = new RegExp(document.domain, 'i');
				if(!mydomain.test(this.getAttribute('href'))) {	
				
					// Here is 3 copies of the tracking, backwarsd and forwards compatible with 
					// both regular, rollup, and asynchronous tracking. it can fire to all trackers if present.
					try { // synchronous tracking:
						if (pageTracker) {
							pageTracker._trackPageview('/outgoing/' + this.getAttribute('href'));
							//alert("just called pageTracker");
						}
					} catch(e) {}
					try { // rollup synchronous tracking:	
						if (rollupTracker) {
							rollupTracker._trackPageview('/outgoing/' + this.getAttribute('href'));
							//alert("just called rollupTracker");
						}
					} catch(e) {}
					try { // asynchronous tracking:
						if (_gaq) {
							_gaq.push([ '_trackPageview', '/outgoing/' + this.getAttribute('href') ]);
							//alert("just called _gaq");
						} 
					} catch(e) {}
					// end core tracking block

				}
			};
		}
	};
}

