// This file contains javascript functions used to adapt anchors to enable event tracking in google analytics
// It uses jQuery
google.setOnLoadCallback(function(){

	$('A').each(function(){try{if($(this).hasClass("GAeventbanner")){trackBannerto(this)}else if(this.protocol=="mailto:"){trackMailto(this)}else if(this.hostname==location.host){var path=this.pathname+this.search;var isDoc=path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);var isRedirect=path.match(/^\/counter\.php\?redirectto\=http(s)?\:\%2F\%2F(?!www\.csmaclub\.co\.uk)/i);if(isDoc)trackDownloadLinks(this);else if(isRedirect)trackExternalLinks(this)}else{trackExternalLinks(this)}}catch(e){}});$('form').each(function(){try{myhost=location.hostname;if(this.action.indexOf(myhost)==-1&&this.action.indexOf('http')==0){trackExternalForms(this)}}catch(e){}});

});

function trackPageArea()
	{
	area=location.href;
	area=area.substring(location.protocol.length+2+location.hostname.length);
	area=(area.charAt(0)=="/")?area.substring(1):area;
	if(area=='index.php'||area=='')zone='homepage';
	else
		{
		findslash=area.indexOf("/");
		if(findslash>-1)zone=area.substr(0,findslash);
		else zone='root'
	}
	return zone
}
function trackMailto(evnt)
	{
	$(evnt).click(function()
		{
		var href=this.href;
		var mailto=href.substring(7);
		_gaq.push(['_trackEvent', 'Mailto', trackPageArea(), href.substring(7)]);
	}
	)
}
function trackBannerto(evnt)
	{
	$(evnt).click(function()
		{
		var e=this;
		while(e.tagName!="A")
			{
			e=e.parentNode
		}
		var lnk=(e.pathname.charAt(0)=="/")?e.pathname:"/"+e.pathname;
		if(e.search&&e.pathname.indexOf(e.search)==-1)lnk+=e.search;
		if(e.hostname!=location.host)lnk=e.hostname+lnk;
		else lnk=lnk;
		thisid=$(this).attr("id");
		if(thisid=='')thisid=trackPageArea();
		_gaq.push(['_trackEvent', 'Banner', thisid, lnk]);
	}
	)
}
function trackExternalLinks(evnt)
	{
	$(evnt).click(function()
		{
		var e=this;
		while(e.tagName!="A")
			{
			e=e.parentNode
		}
		var path=e.pathname+e.search;
		var isRedirect=path.match(/^\/counter\.php\?redirectto\=/);
		var lnk='';
		if(isRedirect)
			{
			lnk=path.replace(/^\/counter\.php\?redirectto\=/,'');
			lnk=unescape(lnk);
			lnk=(lnk.charAt(0)=="/")?lnk:"/"+lnk;
			lnk=lnk
		}
		else
			{
			lnk=(e.pathname.charAt(0)=="/")?e.pathname:"/"+e.pathname;
			if(e.search&&e.pathname.indexOf(e.search)==-1)lnk+=e.search;
			lnk=e.hostname+lnk
		}
		_gaq.push(['_trackEvent', 'External', trackPageArea(), lnk]);
	}
	)
}
function trackDownloadLinks(evnt)
	{
	$(evnt).click(function()
		{
		var e=this;
		while(e.tagName!="A")
		{
			e=e.parentNode
		}
		var lnk=(e.pathname.charAt(0)=="/")?e.pathname:"/"+e.pathname;
		if(e.search&&e.pathname.indexOf(e.search)==-1)lnk+=e.search;
		if(e.hostname!=location.host)lnk=e.hostname+lnk;
		else lnk=lnk;
		_gaq.push(['_trackEvent', 'Download', trackPageArea(), lnk]);
		}
	)
}
