var EquensSitestat = {
	/* Constructor */
	init: function() {		
		this.baseLocation = null;
		this.loginUrlOrTcmId = null;
		this.sitestatLanguageCode = null;
		this.sitestatReplaceChar = null;
		this.sitestatRegex = null;
		this.sitestatCustomerCountryCode = null;
		this.sitestatCustomerName = null;
		this.sitestatCustomerSitename = null;
		this.nsType_clickout = 'clickout';
		this.nsType_document = 'pdf';
		this.downloadExtensions = /\.(doc|eps|jpg|png|svg|xls|ppt|pps|pdf|xls|zip|txt|vsd|vxd|css|rar|exe|wma|mov|avi|wmv|mp3|dmg)/;
		this.sitestatLoginCounterName = null;
	},

	start: function() {
		sitestatCounterURL = 'http://' + EquensSitestat.sitestatCustomerCountryCode + '.sitestat.com/' + EquensSitestat.sitestatCustomerName + '/' + EquensSitestat.sitestatCustomerSitename + '/s?' + EquensSitestat.sitestatLanguageCode + '.';
		$("a").each(function () {
			urllocation = this.href;
			
			if (urllocation.indexOf('mailto:') == 0) {
				sitestatCounterName = urllocation.substring(7);
				$(this).attr('onclick', "ns_onclick(this,'','mailto."+EquensSitestat.replaceInvalidCharacters(sitestatCounterName)+"','clickout');return false;");
			}
			else if (urllocation.match(EquensSitestat.downloadExtensions) != null) { 
				//link is a download
				typeIndex = urllocation.lastIndexOf('.');
				sitestatCounterName = urllocation.substring(typeIndex+1) + '.' + EquensSitestat.replaceInvalidCharacters(urllocation.substring(urllocation.lastIndexOf('/')+1, typeIndex));
				$(this).attr('href', sitestatCounterURL + sitestatCounterName + '&ns_type=' + EquensSitestat.nsType_document + '&ns_url=' + urllocation);
			}
			else {
				//link is an url, check if it is an external link
				isExternalLink = true;
				if (urllocation.indexOf(EquensSitestat.baseLocation) == 0) {
					isExternalLink = false;
				}
				else if (urllocation.substring(0, 1) == "/") {
					isExternalLink = false;
				}
				
				if (isExternalLink) {
					//check if the url should use the onclick method
					if (urllocation.indexOf(EquensSitestat.loginUrlOrTcmId) == 0) {
						$(this).attr('onclick', "ns_onclick(this,'','"+EquensSitestat.sitestatLoginCounterName+"','clickout');return false;");
					}
					else {
						sitestatCounterName =  urllocation;
						//remove protocol
						protocolIndex = sitestatCounterName.indexOf('://');						
						if (protocolIndex > -1) {
							sitestatCounterName = sitestatCounterName.substring(protocolIndex+3);
						}
						//remove trailing slashes
						regex = new RegExp('/+$', 'g');
						sitestatCounterName = sitestatCounterName.replace(regex, '');
							
						sitestatCounterName = 'clickout.'+ EquensSitestat.replaceInvalidCharacters(sitestatCounterName);
						$(this).attr('href', sitestatCounterURL + sitestatCounterName + '&ns_type=' + EquensSitestat.nsType_clickout + '&ns_url=' + urllocation);
					}
				}
			}
		});
	},
	
	replaceInvalidCharacters: function(localsitestatCounterName) {
		localsitestatCounterName = unescape(localsitestatCounterName);
		
		regex = new RegExp('\\.', 'g');
		localsitestatCounterName = localsitestatCounterName.replace(regex, '_');
		
		regex = new RegExp('/', 'g');
		localsitestatCounterName = localsitestatCounterName.replace(regex, '.');
		
		var temp = new Array();
		temp = EquensSitestat.sitestatReplaceChar.split(';');
		for (var j = 0; j < temp.length; j++) {
		    if (temp[j].toString().length > 0) {
			var replacearray = temp[j].toString().split('=');
			if (replacearray.length == 2) {
			    replaceregex = new RegExp(replacearray[0], 'g');
			    localsitestatCounterName = localsitestatCounterName.replace(replaceregex, replacearray[1]);
			}
		    }
		}
		regex = new RegExp(EquensSitestat.sitestatRegex, "g");
		localsitestatCounterName = localsitestatCounterName.replace(regex, "_");
		return localsitestatCounterName.toLowerCase();
	}
}