/* -------------------------- GOOGLE TRACKING ----------------------- */


$(function(){
	externalSelector = "a[href^='http'],a[href^='mailto:']";
	$(externalSelector).addClass("externalLink").bind("click", externalLinkHandler);

	/* Internal links that won't get tracked normally */
	$("a[href$='.pdf']").not(".externalLink").bind("click", internalLinkHandler).addClass("internalLink");

});

//Added to links that go to external pages or processes.
function externalLinkHandler() {
	ob = $(this);
	href = ob.attr("href");

	protocol = href.substring(0, href.indexOf( ":" ));
//	url =  href.substring( href.indexOf( ":" )+1, href.length  );

	gTrackingUrl = "/goals/external/" + href;

	urchinTracker( gTrackingUrl );
}

//Added to internal links that would not otherwise get tracked (PDF files, etc)
function internalLinkHandler() {
	ob = $(this);
	href = ob.attr("href");

	//--- If this is already being handled by the externalLinkHandler (ie it contains a protocol), exit.  (jQuery selection bug necessitates this)
	//	Note that this makes absolute links to internal files behave oddly.
	if( href.indexOf(":")>0 ) { return true;}

	//Current page
	targetPage = document.location.pathname;

	//Remove the actual page name (WARNING: will break if current page is default page in folder... )
	targetPage = targetPage.split("/", targetPage.split("/").length-1 ).join("/");

	//Back up as necessary
	while( href.substring(0,3) == "../" ) {
		//Split into an array, but drop the last entry, then rejoin into string.
		targetPage = targetPage.split("/", targetPage.split("/").length-1 ).join("/");
		//drop the parent-dir
		href = href.substring(3, href.length);
	}

	targetPage = targetPage + "/" + href;

//	alert( "Internal: " + targetPage ); return false;

	urchinTracker( targetPage );
}

