var dM = document.domain.toString()+"/";
var DOMSUFFIX = dM.substring(dM.lastIndexOf("."));
var allLinks = document.getElementsByTagName("a");
var isPromo;

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]);
	// OPTIONAL - split 'events' from download/external URLS ?
}
function notHTML(linkurl) {
	if (
	(linkurl.indexOf(dM)<0 && linkurl.indexOf('javascript:')<0) || 
	(linkurl.indexOf('.asp')<0 && linkurl.indexOf('.htm')<0 && linkurl.indexOf('.xhtm')<0 && linkurl.lastIndexOf('.') != linkurl.lastIndexOf(DOMSUFFIX))
	)
	 {
		return true;
	} else {
		return false;
	}
}
function checkPromo(linkobj) {
	cpStr = "";
	if (linkobj.parentElement) { tilebag = linkobj.parentElement; } else if (linkobj.parentNode) { tilebag = linkobj.parentNode; }
	if (tilebag) {
		if (tilebag.className.indexOf('promotion')>-1) { cpStr = '/Tile/'; }
		else {
			if (linkobj.parentElement && linkobj.parentElement.parentElement.className) {
				if (tilebag.parentElement.parentElement.className.indexOf('clearfix')>-1 &&
					tilebag.parentElement.parentElement.parentElement.className == 'genericBox') {
					cpStr = '/Infobox/';
				} else if (tilebag.parentElement.parentElement && tilebag.parentElement.parentElement.className.indexOf('ad-carousel')>-1) { // advisernet
					cpStr = '/Tile/';
				}
			} else if (linkobj.parentNode && linkobj.parentNode.parentNode.className) {
				if (tilebag.parentNode.parentNode.className.indexOf('clearfix')>-1 &&
					tilebag.parentNode.parentNode.parentNode.className == 'genericBox') {
					cpStr = '/Infobox/';
				} else if (tilebag.parentNode.parentNode && tilebag.parentNode.parentNode.className.indexOf('ad-carousel')>-1) { // advisernet
					cpStr = '/Tile/';
				}
			}
		}
	}
	return cpStr;
}
function onTrack() {
	var thing = "";
	var linkurl = this.toString();	
	isPromo = checkPromo(this);
	if (linkurl.indexOf(dM)<0 && linkurl.indexOf('javascript:')<0) {
	// 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 = 'secureADV/'+linkurl; 
		} else {
			thing = 'external/'+linkurl; 
		}
	} else { // not a web page, so must be a download
		thing = linkurl.replace(dM,"");
		thing = thing.replace("http://","");
		thing = thing.replace("javascript:","JS:");
	}
	// dev & staging
	if (dM.indexOf("wip.web")>-1) { thing = "wip/"+thing; }
	if (dM.indexOf("staging.web")>-1) { thing = "staging/"+thing; }
	// promos & billboards
	if (isPromo != "") {
		var doctrim = document.location.toString().replace(dM,"");
		var imgURL = "";
		if (this.firstChild.src) { imgURLs = this.firstChild.src.toString().split("/");
		imgURL = imgURLs[imgURLs.length-1]; }
		doctrim = doctrim.replace("http://","");
		doctrim = doctrim.replace("default.asp","");
		thing = "PROMO/TO/"+thing+'/FROM/'+doctrim+"/"+isPromo+imgURL;
	}
	if (thing) { trackMe(thing); }
}

if (_gaq) {
	for (var i in allLinks) {
	  if (!isNaN(i)) { // leave out length
		var notReturn = notHTML(allLinks[i].href);
		isPromo = checkPromo(allLinks[i]);
		if (allLinks[i] && (notReturn || isPromo != "")) {
			if (allLinks[i].onclick == null) {
				allLinks[i].onclick = onTrack;
			} else if (allLinks[i].onmousedown == null) {
				allLinks[i].onmousedown = onTrack;
			}
		}
		if (allLinks[i].href.indexOf('.pdf')>-1 && allLinks[i].className.indexOf('nopdf')<0) { // style pdf links
			allLinks[i].className += " pdflink";
			if (!allLinks[i].target) { allLinks[i].target = "_blank"; }
		}
	  }
	}
}
