function loadSitestatsImage(appendix, parameters, callback, custom_nourl) {

	// nourl
	var path = location.pathname;
	if (path == "/") {
			path = "/homepage";
	}
	var nourl = location.hostname + path;
	if (nourl.substring(nourl.length - 1) == "/") {
		nourl = nourl.substring(0, nourl.length - 1);
	}
	nourl += appendix;
	
	// custom nourl
	if (custom_nourl) {
		nourl = custom_nourl;
	}
	
	// nosubsite
	var nosubsite = "";
	var reporting = "";
	var str = location.hostname;
	if (str.search(/archers/i) > -1) { 
		nosubsite = "archers";
		reporting = "reporting.archersdirect.co.uk";
	} else if (str.search(/cosmos/i) > -1) { 
		nosubsite = "cosmos";
		// RedTag sitestats turned off
		// reporting = "reporting.cosmostourama.co.uk";
		if (callback) callback();
		return false;
	}
	
	// referrer - do not log on clicks (appendix)
	var referrer = "";	
	if (!appendix && document.referrer && document.referrer != '') {
		referrer = document.referrer;
/*		referrer = referrer.replace("http://", "");
		referrer = referrer.replace("https://", "");*/
		referrer = "&norefer=" + escape(referrer);
	}
	
	// random value (to prevent image caching)
	var rand = "&random=" + Math.round((Math.random() * 10000000000));

	// sitestats image
	var img = new Image();
	img.src = "http://" + reporting + "/cgi-bin/rr.cgi/images/blank.gif?nosubsite="+nosubsite+"&nourl="+nourl+referrer+parameters+rand;
	img.onabort = function() { if (callback) callback(); }
	img.onerror = function() { if (callback) callback(); }
	img.onload = function() { if (callback) callback(); }
	return false;
}

function bannerRedirect(url) {
	// sitestats
	loadSitestatsImage(
		"-click/banner" + url,
		"&action=click&type=banner&target=" + url,
		function () { window.location = url;	} // redirect
	);
}

function linkTracker() {
	// sitestats
	url = jQuery(this).attr("href");
	url = url.replace("http://", "/");
	url = escape(url);
	loadSitestatsImage(
		"-click/link" + url,
		"&action=click&type=link&target=" + url,
		function () {
			//alert('track: ' + url);
		}
	);
}

jQuery(document).ready(function() {
	jQuery("a._track").bind("click", linkTracker)
});

function videoTracker(url) {
	// sitestats
	url = url.replace("http://", "/");
	url = escape(url);
	loadSitestatsImage(
		"-click/link" + url,
		"&action=click&type=link&target=" + url,
		function () {
			//alert('track: ' + url);
		}
	);
}
