
_gaUtil = new gaUtils();

// Uncomment the following line to turn on debug messages
//_gaUtil.SetDebug(true);


// no user servicable parts below this line
///// gaUtils object
// (c) 2009 Panalysis Pty Ltd all rights reserved www.panalysis.com
// licensed to Mitac
// for alterations and support regarding this script please contact support@panalysis.com

function gaUtils()
{
	// private property declarations
	var bDomain ="";
	var bDebug = false;
	var myDest="";
	var bTrackExternalDomains = false;
	var bTrackLinkID = false;
	
	// Public method declarations
	
	// SetBaseDomain (string domain) - sets the base domain from which to identify external links
	this.SetBaseDomain = _gaSetBaseDomain;
	
	// SetDebug(bool) - turn on alerts when sending data to Google Analytics
	this.SetDebug = _gaSetDebug;
	
	// initialisation
	var cDomain = document.location.hostname.toLowerCase();
	// split the domain into its component parts
	var dp = cDomain.split(".");
	bDomain = dp[dp.length-2] + "." + dp[dp.length-1];
	
	_gaInitLinks();
	
	// _gaSetDebug(bool) - turns on or off debug mode
	function _gaSetDebug(val)
	{
		bDebug = val;
	}
	
	// _gaSetBaseDomain(string domain) - sets the base domain from which to determine whether a link is external or not
	function _gaSetBaseDomain(val)
	{
		bDomain = val;
		_gaInitLinks();
	}
	
	// _gaTrim - trims leading and trailing whitespace
	function _gATrim(val){ return val.replace(/^\s+|\s+$/g, '') ; }
	
	// _gaInitLinks - scan the DOM and associate an onclick handler for each link based on its content
	function _gaInitLinks()
	{
		var dTypes = new Array(".pdf",".doc",".docx",".xls",".xlsx",".exe",".zip",".rtf");

		var mL = document.getElementsByTagName("a");
		var myRegexp = /^http[s]?:\/\/(.*)/;
		for(var i=0;i<mL.length;i++)
		{
			var myRef = mL[i].href.toLowerCase();
			if(myRef == undefined)
				continue;
				
			var myMatch = myRegexp.exec(myRef);
			var lchar = myRef.substring(myRef.length-1);
			if(myMatch != undefined && myMatch.length>1 && bDomain != undefined && myRef.indexOf(bDomain) < 0)
			{
				var oldFunc = (mL[i].onclick) ? mL[i].onclick : function () {};
				mL[i].onclick = function () {
					oldFunc(); 
					try { 
						if(typeof('pageTracker') != 'undefined') 
							pageTracker._link(this.href,false); 
						else if(typeof('pageTracker2') != 'undefined') 
							pageTracker2._link(this.href,false); 
						
						return false;
					} catch (err) {} 
					};
					
			}
			else
			{
				for(x=0;x<dTypes.length;x++)
				{
					if(myRef.indexOf(dTypes[x])>-1 && self.pageTracker)
					{
						var oldFunc = (mL[i].onclick) ? mL[i].onclick : function () {};
						if(mL[i].target != "")
							mL[i].onclick = function () {oldFunc(); _gaTrackThis(this.href,false);};
						else
							mL[i].onclick = function () {oldFunc(); _gaTrackThis(this.href,true); return false;};
					}
				}
			}
		}
	}

	// _gaTrackThis(string path,bool has target attribute) - records clicks on pdf and other links. 
	// If the target is the same window, then dwell for 1/2 second before redirecting user.
	function _gaTrackThis(t,r)
	{
		myDest = t;
	
		var myMatch = myDest.match(/^http[s]?:\/\/(.*)/);
		if(myMatch && myDest.indexOf(bDomain) < 0)
		{
			t = "/outbound/" + myMatch[1];
		}
		else
			t = t.replace(/http[s]?:\/\/[^\/]*/,"");
	
		if(self.pageTracker)
		{
			if(bDebug==true)
				alert("P1 " + t);
			pageTracker._trackPageview(t);
		}
		
		if(self.pageTracker2)
		{
			if(bDebug==true)
				alert("P2 " + t);
			pageTracker2._trackPageview(t);
		}
		
		if(r==true)
		{
			setTimeout("document.location.href=myDest;",500); // delay for 1/2 second
			return false;
		}
		else
			return true;
	}
}