﻿/*
	Text resizer script functions.
	
	A "fontsize" cookie value of "normal", "medium", or "large" is set by the button clicks.
	
	This value is retrieved from the cookie, and then a percentage size is applied to various
	elements of the document based on the descriptive value obtained from the cookie.
*/

// Set the cookie
function setCookie(name, newValue) {

	var cookieval = name + "=" + newValue;

	var expires = "expires=";
	var expdate = new Date();
	expdate.setFullYear(expdate.getFullYear() + 1, expdate.getMonth(), expdate.getDate());
	expires += expdate.toGMTString();
	cookieval += ";" + expires;
	
	document.cookie = cookieval;
}

// Get the cookie
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

// Set the font size in the cookie
// Called from the text-resizer buttons on the html page
function setFontSize(newFontSize) {
	setCookie("fontsize", newFontSize);
	applyFontSize();
}

// Apply the font size stored in the cookie.
// Called from the page load, and when the text-resize buttons are pressed.
//
// I originally did this with document.getElementById("xyz").setAttribute("style"...
// but IE doesn't seem to recognise setAttribute properly, so I had to use the
// properties version instead.
function applyFontSize() {
	var newFontSizeDescription = getCookie("fontsize");
	var newFontSize;

	if (newFontSizeDescription == null || newFontSizeDescription.length == 0) {	newFontSizeDescription = "normal"; }
	
	if (!document.getElementById) return;

	switch (newFontSizeDescription) {
		case "normal":
			document.getElementById("small-A-active").style.display = "none";
			document.getElementById("small-A-disabled").style.display = "inline";
			document.getElementById("medium-A-active").style.display = "inline";
			document.getElementById("medium-A-disabled").style.display = "none";
			document.getElementById("large-A-active").style.display = "inline";
			document.getElementById("large-A-disabled").style.display = "none";
			newFontSize = "100%";
			break;

		case "medium":
			document.getElementById("small-A-active").style.display = "inline";
			document.getElementById("small-A-disabled").style.display = "none";
			document.getElementById("medium-A-active").style.display = "none";
			document.getElementById("medium-A-disabled").style.display = "inline";
			document.getElementById("large-A-active").style.display = "inline";
			document.getElementById("large-A-disabled").style.display = "none";
			newFontSize = "120%";
			break;

		case "large":
			document.getElementById("small-A-active").style.display = "inline";
			document.getElementById("small-A-disabled").style.display = "none";
			document.getElementById("medium-A-active").style.display = "inline";
			document.getElementById("medium-A-disabled").style.display = "none";
			document.getElementById("large-A-active").style.display = "none";
			document.getElementById("large-A-disabled").style.display = "inline";
			newFontSize = "140%";
			break;
	}
	
	// change the font size on the bits that require changing
	if (document.getElementById("content")) { document.getElementById("content").style.fontSize = newFontSize; }
	if (document.getElementById("leftcol")) { document.getElementById("leftcol").style.fontSize = newFontSize; }
	if (document.getElementById("rightcol")) { document.getElementById("rightcol").style.fontSize = newFontSize; }
}
