// COMMON JAVASCRIPT FUNCTIONS

// COOKIE FUNCTIONS
// Functions for setting and reading cookies

// Set a named cookie to a value, optionally to expire in a number of days
function set_cookie(cname, cvalue, days)
{
	var today = new Date();
	var expire = new Date();
	if (days==null || days==0) { days = 1; }
	expire.setTime(today.getTime() + 3600000 * 24 * days);

	document.cookie = cname + "=" + escape(cvalue) + ";expires=" + expire.toGMTString();
}

// Return the value of the named cookie
function read_cookie(cname)
{
	var c = "" + document.cookie;

	var ind = c.indexOf(cname);
	if ( ind == -1 || cname == "") { return ""; }

	var ind1 = c.indexOf(';', ind);
	if (ind1==-1) { ind1 = c.length; }

	return unescape(c.substring(ind + cname.length + 1, ind1));
}


// UNOBTRUSIVE FUNCTIONS
// Functions for working unobtrusively with Javascript

// Add an event handler to an object; you can add multiple events to the same 
// object. For window onloads, use the addLoadEvent function below.
//
// usage: addEvent(window, 'load', handler_name)
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on" + evType, fn); 
		return r; 
	} 
	else 
	{ 
		return false; 
	} 
}

// Add a function to the window.onload list. This is more compatible than 
// the general addEvent function above, and the functions can be standard
// functions rather than having to be event handlers.
//
// usage: addLoadEvent(function_name)
function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} 
	else 
	{
		window.onload = function() 
		{
			if (oldonload) { oldonload(); }
			func();
		}
	}
}

// SCREEN STATS FUNCTIONS
// Functions for determining user screen & window size

// Return the client's screen size as a string in the format "640x480"
function screen_size()
{
	var s = "";
	if (screen.width) {	s = screen.width + "x" + screen.height;	}
	return s;
}

// Return the client's current browser window size as a string in the format "640x480"
function window_size()
{
	var win_width = 0, win_height = 0;

	if (typeof(window.innerWidth) == 'number')
	{
		// Non-IE browser
		win_width = window.innerWidth;
		win_height = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		// IE 6+ in 'standards compliant mode'
		win_width = document.documentElement.clientWidth;
		win_height = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		// IE 4 compatible
		win_width = document.body.clientWidth;
		win_height = document.body.clientHeight;
	}

	return win_width + "x" + win_height;
}

// Return the client's current colour depth
function colour_depth()
{
	var c = "";
	if (screen.colorDepth)
	{
		c = screen.colorDepth;
	}
	return c;
}

// Return an HTML image tag with a query string containing client stats
function stats_image_tag()
{
	// location of the image file
	var i = "/shared/images/screenstats.png";
	
	// styles to apply to the image
	var css = "width:1px;height:1px;margin:0;padding:0;";

	// client stats
	var s = screen_size();
	var w = window_size();
	var c = colour_depth();

	// build the image tag
	var imagetag = "<img style='" + css + "' src='" + i + "?s=" + s + "&w=" + w + "&c=" + c + "' alt='' />";

	return imagetag;
}

// Given an element ID, write out an image tag generated by stats_image_tag()
// as the content of that element. Call this from an onload function like this:
//
// 		[body onload="write_stats_tag('foo')"]
//
// and place an empty DIV near the bottom of the page:
//
// 		[div id="foo"][/div]
function write_stats_tag(element_id)
{
	var cookie_name = "screenstats";
	
	// set a default so we can call this function with no params
	if (! element_id) { element_id = "screenstats"; }
	
	if (read_cookie(cookie_name) != "SET")
	{
		var el = document.getElementById(element_id);
		if (el)
		{
			el.innerHTML = stats_image_tag();
		}

		// drop a 30-day cookie so we don't call the stats routine again for a month
		set_cookie(cookie_name, "SET", 30);
	}
}

// Attach the screen stats-gathering event to the window
addLoadEvent(function() { write_stats_tag("screenstats"); });

