// A D D   H A N D L E R
// allows adding more than one function to the onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// G E T   U R L
// function to load a new page in the browser
function getUrl(url) {
	if (typeof url != 'string') return false;
	if (url.indexOf('.html') != -1 || url.indexOf('http') != -1) {
		document.location.href = url;
	} else if (url) {
		alert(url);
	}
	return false;
}


// M A G I C   C O O K I E S
// utility cookie handling object
// loosely based on David Flannagan's cookie functions in /Javascript: The Definitive Guide/
// published by O'Reilly
function MagicCookie(id) {
	// set our name so we know it
	this.name = this.id;
	// add us to the global namespace
	window[id] = this;
	// initialize empty object to hold the cookies
	this.cookies = new Object();
	// get the document cookie
	var allCookies = document.cookie.split(';');
	// loop through cookies and separate them into name+value pairs housed in this.cookies object
	var cookieParts;
	var numCookies = allCookies.length;
	for (var i = 0; i < numCookies; i++) {
		cookieParts = allCookies[i].split('=');
		this.cookies[cookieParts[0].replace(/ /,'')] = unescape(cookieParts[1]);
	}
	// get the domain so we can use that as a default when setting cookies
	this.domain = 'domain=' + document.domain + ';';
	// set a default path
	this.path = 'path=/;';
	// create a new date to be the default expiration date
	var expiration = new Date();
	expiration.setFullYear(expiration.getFullYear() + 1);
	// set the default expiration date to 1 year from now
	this.expire = 'expires=' + expiration.toGMTString() + ';';
	return this;
}
// get the value of the cookie with the name equal to a string passed as an argument
MagicCookie.prototype.getCookie = function(cookieName) {
	if (typeof this.cookies[cookieName] != 'undefined') return this.cookies[cookieName];
	else return false;
}
// set a cookie with a supplied name and value
MagicCookie.prototype.setCookie = function(cookieName,cookieValue) {
	this.cookies[cookieName] = escape(cookieValue);
	// set a cookie with the name and value passed as arguments and the calculated domain and expiration dates
	document.cookie =  cookieName + '=' + cookieValue + ';' + this.expire + this.path + this.domain;
	return true;
}
var magicCookie = new MagicCookie('magicCookie');


// S T Y L E   T O O L S
// Cheeseball widget for changing font size and text alignment and printing the page
// VERY badly written -- crib it and you'll get what you deserve you lazy muppet ;-)
// kinda loosely based on Andy Clarke's 'Invasion of the Body Switchers' article at:
// http://www.alistapart.com/articles/bodyswitchers/
// with apologies to Andy for mucking up a perfectly good script :-)
// requires almost equally cheeseball MagicCookie object
function StyleTools(id) {
	// set our name so we know it
	this.name = id;
	// add us to the global namespace
	window[id] = this;
	// cache reference to body
	this.body = document.getElementsByTagName('body')[0];
	// cache the base class so we don't have to muck about with string ops to get the base
	// when removing/switching classes later
	this.baseClass = ('' != this.body.className)?this.body.className:'styleTools';
	// this is where things go bad, as I'm hard-coding classnames and the like into the script
	// also using the same string for classname + id for simplicity -- VERY slimy
	this.currentFont = 'normalFont';
	this.currentAlign = 'ragRightAlign';
	// get alignment/font size prefs if any
	var fontPref = this.getFontPref();
	var alignPref = this.getAlignPref();
	// see if we've got saved preferences; if not, use the defaults
	fontPref = (fontPref)?fontPref:this.currentFont;
	alignPref = (alignPref)?alignPref:this.currentAlign;
	this.setFont(fontPref,document.getElementById(fontPref));
	this.setAlign(alignPref,document.getElementById(alignPref));
	// set up handlers for buttons
	document.getElementById('smallFont').firstChild.onclick = new Function(this.name + '.setFont("smallFont",this.parentNode); return false;');
	document.getElementById('normalFont').firstChild.onclick = new Function(this.name + '.setFont("normalFont",this.parentNode); return false;');
	document.getElementById('largeFont').firstChild.onclick = new Function(this.name + '.setFont("largeFont",this.parentNode); return false;');
	document.getElementById('ragRightAlign').firstChild.onclick = new Function(this.name + '.setAlign("ragRightAlign",this.parentNode); return false;');
	document.getElementById('justifyAlign').firstChild.onclick = new Function(this.name + '.setAlign("justifyAlign",this.parentNode); return false;');
	if (typeof window.print != 'undefined') document.getElementById('printPage').firstChild.onclick = new Function('window.print(); return false;');
	return this;
}
// gets the stored font preference
StyleTools.prototype.getFontPref = function () {
	return magicCookie.getCookie('fontPref');
}
// stores the font preference
StyleTools.prototype.setFontPref = function () {
	return magicCookie.setCookie('fontPref',this.currentFont);
}
// gets the stored align preference
StyleTools.prototype.getAlignPref = function () {
	return magicCookie.getCookie('alignPrefr');
}
// stores the align preference
StyleTools.prototype.setAlignPref = function () {
	return magicCookie.setCookie('alignPrefr',this.currentAlign);
}
// changes the font; duh
StyleTools.prototype.setFont = function (desiredFont,button) {
	// get rid of hilight on currently active button
	document.getElementById(this.currentFont).className = '';
	// set classname
	this.body.className = this.baseClass + ' ' + this.currentAlign + ' ' + desiredFont;
	this.currentFont = desiredFont;
	button.className = 'active';
	this.setFontPref();
}
// changes the alignment; duh
StyleTools.prototype.setAlign = function (desiredAlign,button) {
	// get rid of hilight on currently active button
	document.getElementById(this.currentAlign).className = '';
	// set classname
	this.body.className = this.baseClass + ' ' + this.currentFont + ' ' + desiredAlign;
	this.currentAlign = desiredAlign;
	button.className = 'active';
	this.setAlignPref();
}

addLoadEvent(new Function('if (typeof document.getElementById != "undefined" && document.getElementById("tools")) var styleTools = new StyleTools("styleTools");'));
