var dM = document.domain.toString()+"/";
var DOMSUFFIX = dM.substring(dM.lastIndexOf("."));

function flashEvent(category, action, opt_label, opt_value) {
	if (opt_label) {
		if (opt_value) {
			 _gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
		} else {
			 _gaq.push(['_trackEvent', category, action, opt_label]);
		}
	} else {
		 _gaq.push(['_trackEvent', category, action])
	}
}
function trackMe(event) {
	_gaq.push(['_trackPageview',event]);
}
$(document).ready(function() {
	if (_gaq) {
		$('a').click(function(){
			var thing = "";
			var linkurl = $(this).attr("href");
			if (linkurl && linkurl.charAt(0)!='#') { // ignore links with no hrefs or anchor hrefs
				if (linkurl.indexOf(dM)<0 && linkurl.indexOf('http')>-1) {
				// external
					if (linkurl.indexOf('online.btfunds')>-1) {
						thing = 'secureBT/'+linkurl.replace("https://online.btfunds.com.au/",""); 
					} else if (linkurl.indexOf('advisernet')>-1 || linkurl.indexOf('investoronline')>-1 || linkurl.indexOf('employeronline')>-1) {
						thing = 'secure/'+linkurl.replace(/http[s]*\:\/\//,""); 
					} else {
						thing = '/external/'+linkurl; 
					}
				} else if (
						linkurl.indexOf('javascript:')<0 && linkurl.indexOf('.asp')<0 && linkurl.indexOf('.htm')<0 
						&& linkurl.indexOf('.xhtm')<0 && linkurl.lastIndexOf('.') != linkurl.lastIndexOf(DOMSUFFIX)
						) { // not a web page, so must be a download
							thing = linkurl.replace(dM,"");
							thing = thing.replace(/http[s]*\:\/\//,"");
				}
				if (thing) { trackMe(thing); }
				
				if ($(this).parent().parent().parent().hasClass('ad-carousel-list') || //AdviserNet
					$(this).parent().hasClass("ad") || $(this).parent().hasClass("promotion") || //Wrap/BT promo
					$(this).parent().hasClass("clearfix") && $(this).parent().parent().parent().parent().hasClass("genericBox") || // BT/BTFG infobox
					$(this).parent().parent().parent().hasClass('copy') || // Advance infobox
					$(this).parent().hasClass("homepage-ad")) { // LifeCentral
					thing = linkurl.replace(dM,"");
					thing = thing.replace(/http[s]*\:\/\//,"");

					var doctrim = document.location.toString().replace(dM,"");
					doctrim = doctrim.replace(/http[s]*\:\/\//,"");
					doctrim = doctrim.replace("default.asp","");
					var imgURL = "Infobox";
					if ($(this).find("img").attr("src")) {
						imgURL = $(this).find("img").attr("src");
						imgURL = imgURL.substring(imgURL.lastIndexOf("/")+1)
						flashEvent("PROMO TILE", thing, doctrim+" : "+imgURL);
					} else {
						flashEvent("PROMO LINK", thing, doctrim);
					}
				} else if (linkurl.indexOf("javascript:")>-1) {
					thing = linkurl.replace("javascript:","");
					jfn = thing.substring(0,thing.indexOf("("));
					jcall = thing.replace(jfn,"");
					jcall = jcall.replace(/['\(\)";]/g,"");
					flashEvent("Javascript", jfn, jcall);
				}
			}
		});
	}
});
