//externallinks.js
// See google explanation: http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55527

/*
For example, to log every click on a particular link to www.example.com as a 
pageview for "/outgoing/example_com" you would add the following attribute to the link's tag:

<a href="http://www.example.com" onClick="javascript: pageTracker._trackPageview('/outgoing/example_com');">

It is a good idea to log all of your outbound links into a logical directory structure as shown in the example. 
This way, you will be able to easily identify what pages visitors clicked on to leave your site.

*/
//*************************************
// GoogleRecordExternalLinks
//*************************************
function GoogleRecordExternalLinks()
{
	// for each link that has an href associated with it...
	$("a[href]").each(function (i) {
        
		// fetch the href value 
		var sHref = $(this).attr("href");
		if (sHref.length < 1) return;
		
		// and convert to lower case
		var sHrefLowerCase = sHref.toLowerCase();
				
		// is this an external link?
		var bIsExternalLink=true;
		
		//alert("examining: " + sHrefLowerCase);
		
		// if on test site, exit
		//if (sHrefLowerCase.indexOf("minisage.com")!=-1) return;
		
		// if it starts with the live site prefix, it's not an external link
		if (sHrefLowerCase.indexOf("http://www.globalpartnerships.org")==0) bIsExternalLink=false;
		if (sHrefLowerCase.indexOf("http://globalpartnerships.org")==0) bIsExternalLink=false;
		if (sHrefLowerCase.indexOf("https://www374.safesecureweb.com/gpwebsite/")==0) bIsExternalLink=false;
		
		// at this point, if it doesn't begin with http, it's not an external link
		if (sHrefLowerCase.indexOf("http")!=0) bIsExternalLink=false;
		
		// if this is NOT an external link, examine the next occurrence by returning FALSE
		if (!bIsExternalLink) return true;  // (return false would break out of the each loop entirely)
		
		// add the google tracking code to this external link
		
		// fetch the onclick value
		var sOnClick = $(this).attr("onclick");
		if (sOnClick==null) sOnClick="";
		if (sOnClick==undefined) sOnClick="";
		
		//if (sOnClick.length < 1) sOnClick = $(this).attr("onClick");
		
		//---------------------------------------------------------------
		// modify the href to remove the prefix and convert special chars
		//---------------------------------------------------------------
		if (sHrefLowerCase.indexOf("https://")==0) sHrefLowerCase = sHrefLowerCase.substr(8,sHrefLowerCase.length-8);
		if (sHrefLowerCase.indexOf("http://")==0) sHrefLowerCase = sHrefLowerCase.substr(7,sHrefLowerCase.length-7);
		// replace "." and "/" chars
		//sHrefLowerCase = sHrefLowerCase.replace(".", "_");
		//sHrefLowerCase = sHrefLowerCase.replace("/", "-");
		// remove querystring
		var nPos = sHrefLowerCase.indexOf("?");
		if (nPos != -1) sHrefLowerCase = sHrefLowerCase.substr(0,nPos);
		
		// insert the google code
		sOnClick = "pageTracker._trackPageview('/outgoing/" + sHrefLowerCase + "'); " + sOnClick;
		
		// set the onclick handler back to this value
		$(this).attr("onclick",sOnClick);
		
		//alert("found one: onclick=" + sOnClick);
		
      }); // $("a[href]").each(function (i) 

}
	//***********************************
	// CALL GoogleRecordExternalLinks
	//***********************************
	GoogleRecordExternalLinks();
	