// --- begin font size
function alterFontSize(direction)
{
	var size;
	// cast cookie return as integer
	var currentFontSize = parseInt(readCookie("ErimusfontSize"));
	
	// if currentFontSize isn't numeric then default to this value
	if (isNaN(currentFontSize))
		currentFontSize = 100;
	
	switch(direction)
	{
		case 'up':
			if (currentFontSize < 140)
				size = currentFontSize + 10;
		break;
		
		case 'down':
			if (currentFontSize > 90)
				size = currentFontSize - 10;
		break;
		
		default:
			size = currentFontSize;
	}

	if (size > 0)
		setFontSize(size);
}

// set body.fontSize attribute (change the size of a currently loaded page)
function setFontSize(size)
{
	var body = document.getElementById('body');
	body.style.fontSize = size + '%';
	createCookie("ErimusfontSize", size, 365);
}

/*
we can't access body.fontSize until the page is loaded, which would result in page text being shown at default size for a brief period,
so we can workaround that by inlining a style override for initial page loads at non-default font sizes
*/
function checkFontSize()
{
	var fontSize = readCookie("ErimusfontSize");
	if (fontSize != null)
	{
		document.write('<style type="text/css">body {font-size: ' + fontSize + '%; }</style>');
	}
}

// --- end font size
// --- begin helper functions ---

// bake a cookie
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// eat a cookie
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// check for fontsize cookie
checkFontSize();