
_gaUtil = new gaUtils();
// Uncomment the following line to turn off automatic link tracking based on the #linkid value
// _gaUtil.TrackLinkID=false;

// Uncomment the following line to turn on debug messages
//_gaUtil.SetDebug(true);

// Uncomment the following line to turn on outbound link tracking
_gaUtil.SetTrackExternalLinks(true);

_gaUtil.TrackLinkSource("linkid");

// If the search results parameter is set and the numresults variable is set
if(typeof(numResults) != 'undefined')
	_gaUtil.TrackNumberSearchResults('q',numResults); // change q to the appropriate value if used on another search engine

// no user servicable parts below this line
///// gaUtils object
// (c) 2009 Panalysis Pty Ltd all rights reserved www.panalysis.com
// licensed to Curtin University
// for alterations and support regarding this script please contact support@panalysis.com

function gaUtils()
{
	// private property declarations
	var bDomain ="curtin.edu.au";
	var bDebug = false;
	var myDest="";
	var bTrackExternalDomains = false;
	var bTrackLinkID = true;
	
	// Public method declarations
	// TrackLinkSource(string linkID param) tracks a link from a referring page using the document.location.hash value. Expected #linkid=LINK
	this.TrackLinkSource = _gaTrackLinkSource;
	
	// SetTrackExternalLinks(bool) - turns on or off the tracking of links to other sites
	this.SetTrackExternalLinks = _gaTrackExternalLinks;
	
	// SetTrackInternalLinks(bool) - turns on or off the tracking of the internal links (see also TrackLinkSource)
	this.SetTrackInternalLinks = _gaTrackInternalLinks;
	
	// TrackNumberSearchResults(string param, integer numResults) - records the number of search results as an event in Google Analytics
	this.TrackNumberSearchResults = _gaTrackSearchResults;
	
	// 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
	_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;
	}
	
	// _gaTrackExternalLinks(bool) - sets the flag to turn tracking of external links on or off
	function _gaTrackExternalLinks(val)
	{
		bTrackExternalDomains = val;
		if(val ==true)
			_gaInitLinks();
	}
	
	// _gaTrackInternalLinks(bool) - sets the flag to turn tracking of internal links on or off
	function _gaTrackInternalLinks(val)
	{
		bTrackLinkID = val;
	}
	
	// _getHashParam - retrieves the a query parameter from the document hash
	function _gaGetHashParam(strParam){
		var _pstr = document.location.hash.substring(1);
		var _uparams = _pstr.split("&");
		for(var i = 0; i < _uparams.length; i++){
			var np = _uparams[i].split("=");
			if(_gATrim(np[0].toLowerCase()) == strParam.toLowerCase())
			{
				return _gATrim(np[1]);
				break;
			}
		}
		return "";
	}
	
	// _getHashParam - retrieves the a query parameter from the document URL
	function _gaGetParam(strParam){
		var _pstr = document.location.search.substring(1);
		var _uparams = _pstr.split("&");
		for(i = 0; i < _uparams.length; i++){
			var np = _uparams[i].split("=");
			if(_gATrim(np[0].toLowerCase()) == strParam.toLowerCase())
				return _gATrim(np[1]);
		}
		return "";
	}
	
	// _gaTrim - trims leading and trailing whitespace
	function _gATrim(val){ return val.replace(/^\s+|\s+$/g, '') ; }
	
	// _gaTrackLinkSource - tracks a link from a referring page using the document.location.hash value. Expected linkid=LINK
	function _gaTrackLinkSource(val)
	{
		if(bTrackLinkID == false)
			return;
			
		if(document.location.hash !="")
		{
			var strLinkID = _gaGetHashParam(val);
			if(strLinkID != "")
			{
				if(self.pageTracker)
				{
					// track the event
					// category = link tracking
					// action = linkID
					pageTracker._trackEvent("Link Tracking",strLinkID + " -> " + document.location.href);
				}
			}
		}
	}
	
	// _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 && bTrackExternalDomains==true)
			{
				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;};
			}
			else if(mL[i].href.indexOf('mailto:')>-1)
			{
				
				var oldFunc = (mL[i].onclick) ? mL[i].onclick : function () {};
				mL[i].onclick = function () {oldFunc(); _gaTrackEmail(this.href);};
			}
			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(t);
			pageTracker._trackPageview(t);
		}
		if(r==true)
		{
			setTimeout("document.location.href=myDest;",500); // delay for 1/2 second
			return false;
		}
		else
			return true;
	}
	
	// _gaTrackEmail(string email address) - record any email address clicks
	function _gaTrackEmail(m)
	{
		m = m.toLowerCase();
		var myMatch = m.match(/^mailto:(.*)/);
		if(myMatch)
		{
			m = "/email/" + myMatch[1];
		}
		
		if(self.pageTracker)
		{
			if(bDebug==true)
				alert(m);
			pageTracker._trackPageview(m);
		}
	}
	
	// _gaTrackSearchResults(string query parameter,integer numResults)
	function _gaTrackSearchResults(param,numResults)
	{
		if(param=="" || param =='undefined' || numResults =='undefined')
			return;
		
		var keyword = _gaGetParam(param);
		if(self.pageTracker && keyword != 'undefined' && keyword !="")
		{
			// track the event
			// category = "search results"
			// action = "Low Results Searches"
			// label = keyword
			// value = num results
			if(bDebug==true)
				alert("Keyword: " + keyword + " Num: " + numResults);
			pageTracker._trackEvent("Search Results","No. Search Results",keyword,numResults);
		}
	}
}