// Library of common functions
// Written by Andrew Hedges, andrew@clearwired.com
// August 30, 2006

d = document;

// Return a reference to an object
getObj = function (id) { return (d.getElementById)? d.getElementById(id): false; }

// Fix for flickering background images in IE 6
// From: http://blog.nemus.se/index.php/get-rid-of-ie-flickering/
if (d.all) {
	try {
		d.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
}

// Add emulation of :hover behaviors to arbitrary elements in IE
// id = id of the container object, tag = tag name of objects to which to add hover behaviors
addHovers = function (id, tag) {
	if (navigator.userAgent.indexOf('MSIE') > 0) {
		var obj = document.getElementById(id);
		if (!obj) return;
		var subObjs = obj.getElementsByTagName(tag);
		// for IE, add the hover behaviors using JavaScript
		for (var i = 0; i < subObjs.length; i++) {
			subObjs[i].onmouseover = function() { this.className += ' hovered'; }
			subObjs[i].onmouseout = function() { this.className = this.className.replace('hovered', ''); }
		}
	}
}

// Add functions to fire on page load
function addLoadEvent (func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload=function() {
			if (oldonload) {
				oldonload();
			}
			func ();
		}
	}
}

// add functions to fire on page unload
function addUnloadEvent (func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload=function() {
			if (oldonunload) {
				oldonunload();
			}
			func ();
		}
	}
}

// rough emulation of PHP's sprintf function
// rough because there's no padding, no alignment ... just straight string substitution
// strings_array = array of strings to be substituted, in order, for occurrences of %s
// will not change the string if there are not the same number of replacement strings and occurrences of %s
// will not change the string if the strings array is empty
String.prototype.sprintf = function (strings_array) {
	if (strings_array.length > 0) {
		var idx;
		var string_pieces = this.split('%s');
		var string_new = '';
		if (string_pieces.length == strings_array.length+1) { // split seems to return a value at position [0] whether there is anything there or not
			for (var s = 0; s < string_pieces.length; s++) {
				string_new += string_pieces[s];
				if (s < string_pieces.length-1) string_new += strings_array[s];
			}
			return string_new;
		} else {
			return this;
		}
	} else {
		return this;
	}
}

// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/JavaScript/randomizer.html
// Usage: rand(n) returns random integer between 0 and n-1

rnd = function () {
	var today = new Date();
	var seed = today.getTime();
	seed = (seed*9301+49297) % 233280;
	return seed/(233280.0);
}

rand = function (number) {
	return Math.ceil(rnd()*number)-1;
}

