// JavaScript Analytics code.
// Copyright 2008 James G Ross.

// Data collected:
//   id = User's unique ID (randomly generated).
//   tz = Time zone offset.
//   l  = Locale/language.
//   ss = Screen resolution and bit-depth.
//   bs = Browser content area.
//        Browser UA.
//   pl = Platform.
//   os = OS/CPU (Mozilla/Firefox).
//   cc = CPU class (Internet Explorer).
//   ck = Cookies enabled.
//   pc = Page's quicks/standard mode switch.
//   ph = Page's hostname.
//   pp = Page's path.
//   pt = Page's title.

function jgrAnalytics() {
	var a = new Array();
	a.push("id=" + Number(jgrAnalyticsGetID()));
	a.push("tz=" + Number(jgrAnalyticsGetWinterTimezoneOffset()));
	a.push("l="  + escape(String(navigator.language || navigator.userLanguage || "")));
	a.push("ss=" + Number(screen.width) + "x" + Number(screen.height) + "x" + Number(screen.colorDepth));
	a.push("bs=" + Number(window.innerWidth || (document.documentElement ? document.documentElement.offsetWidth : 0) || (document.body ? document.body.offsetWidth : 0) || "") + "x" + Number(window.innerHeight || (document.documentElement ? document.documentElement.offsetHeight : 0) || (document.body ? document.body.offsetHeight : 0) || ""));
	// Browser UA is send as User-Agent with the request.
	a.push("pl=" + escape(String(navigator.platform || "")));
	a.push("os=" + escape(String(navigator.oscpu || "")));
	a.push("cc=" + escape(String(navigator.cpuClass || "")));
	a.push("ck=" + escape(String(navigator.cookieEnabled || "")));
	a.push("pc=" + escape(String(document.compatMode || "")));
	a.push("ph=" + escape(String(location.hostname)));
	a.push("pp=" + escape(String(location.pathname)));
	a.push("pt=" + escape(String(document.title)));
	a.push("r="  + String(Math.random()));
	var img = new Image();
	img.src = "/cps_static/analytics.png?" + a.join("&");
}

function jgrAnalyticsGetID() {
	var id = String(document.cookie).match(/JGRID=(\d+)/);
	if (id) {
		id = id[1];
	} else {
		id = String(Math.random()).replace(/[^0-9]/g, "");
	}
	var ex = new Date();
	ex.setFullYear(ex.getFullYear() + 1);
	document.cookie = "JGRID=" + id + "; EXPIRES=" + ex.toUTCString();
	return id;
}

function jgrAnalyticsGetWinterTimezoneOffset() {
	// getTimezoneOffset() is +ve for behind, -ve for ahead.
	// We date two dates - now and 6 months forward - and pick whichever has
	// the bigger value, which equals most behind.
	var d1 = new Date();
	var d2 = new Date();
	d2.setMonth(d1.getMonth() + 6);
	return (d1.getTimezoneOffset() > d2.getTimezoneOffset() ? d1.getTimezoneOffset() : d2.getTimezoneOffset());
}

jgrAnalytics();
