$(document).ready(function(){
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

	//use jQuery to call the Google Analytics JavaScript
	$.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
	
		//tell Analytics about the current page load using standard _trackPageview method
		try {
			var pageTracker = _gat._getTracker("UA-5307007-7");
			pageTracker._trackPageview();
		} catch(err) {}
		
		//loop though each anchor element
		$('a').each(function() {			
			var href = $(this).attr('href');
			var filetypes = /\.(pdf|doc*|eps|jpg|jpeg)$/i;
			
			//check for links starting with http or https, making sure that links to our own domain are excluded
			if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
				$(this).click(function() {
					var extLink = href.replace(/^https?\:\/\//i, '');

					//check if we are on the charity listing or restaurant listing pages
					if ( $("body.charity-list").length > 0 || $("body.rest-list").length > 0 )
					{
						//Retrieve the details of the page and the link that has been clicked
						var location = $("#leftcol .topbar .yellowbar h1").text().replace(/^restaurants in /i, '').replace(/^charities in /i, '');
						var pageType = $("#leftcol .topbar .yellowbar h1").text().replace(/ in [^\.]+.$/i, '');				
						var linkTitle = $(this).attr('title').replace(/ website\.$/i, '').replace(/^visit the /i, '');
						
						//add some error handling as we do not ever want the possibility of javascript errors appear on user clicks						
						try {
							pageTracker._trackPageview('/outgoing/' + location + '/' + pageType + '/' + linkTitle + '/' + extLink);
						} catch(err) {}
					}
					else
					{
						//add some error handling as we do not ever want the possibility of javascript errors appear on user clicks						
						try {
							pageTracker._trackPageview('/outgoing/' + extLink);
						} catch(err) {}
					}
				});
			}
			//check for links starting with mailto:
			else if (href.match(/^mailto\:/i)){
				$(this).click(function() {
					var mailLink = href.replace(/^mailto\:/i, '');
					
					//add some error handling as we do not ever want the possibility of javascript errors appear on user clicks						
					try {
						pageTracker._trackPageview('/outgoing/email/' + mailLink);
					} catch(err) {}
				});
			}
			//check for links with file extension that match the filetypes regular expression:
			else if (href.match(filetypes)){
				$(this).click(function() {
  					var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
					var filePath = href.replace(/^https?\:\/\//i, '');
					
					//add some error handling as we do not ever want the possibility of javascript errors appear on user clicks						
					try {
						pageTracker._trackPageview('/outgoing/download/' + filePath);
					} catch(err) {}					
				});
			}
		});		
	});
});

function testClickTracker ( data )
{
	alert("testClickTracker :: '" + data + "'");
}