// of course Internet Explorer does not use the regular way of registering events like all the rest of the world *sigh* 
// and keep in mind that IE needs the event with "on" (e.g. onload) as all the others say just the event name ("load") 
if (window.addEventListener) window.addEventListener("load", initZoom, false);
else if (window.attachEvent) window.attachEvent("onload", initZoom);

function initZoom()
{
  if(document.cookie)
  {
    var zoom = 1;
    zoom = readZoomCookie();
    if(zoom != '') setZoom(zoom - 1);
  }
}

function getZoom()
{
  var zoom = 1;
  if (!isNaN(parseFloat(document.getElementsByTagName('body')[0].style.fontSize)))
  {
    zoom = parseFloat(document.getElementsByTagName('body')[0].style.fontSize);
  }
  return (zoom);
}

function setZoom(byValue)
{
  if (byValue == 0) byValue = 1;
  else byValue += getZoom();
  document.getElementsByTagName('body')[0].style.fontSize = byValue + 'em';
  writeZoomCookie();
}

function readZoomCookie()
{
  var cookieList = document.cookie + ';';
  var cookieName = '';
  var cookieValue = ''; 
  var nextCookiePos = 0;
  var zoomCookie = '';
  while(cookieList != '')
  {
    cookieName = cookieList.substring(0, cookieList.search('='));
    cookieValue = cookieList.substring(cookieList.search('=') + 1, cookieList.search(';'));
    if(cookieValue == '')
    {
      cookieValue = cookieList.substring(cookieList.search('=') + 1,cookieList.length);
    }
    
    if (cookieName == 'zoom') zoomCookie = cookieValue;
    
    nextCookiePos = cookieList.search(';') + 1;
    if (nextCookiePos == 0) nextCookiePos = cookieList.length;
    cookieList = cookieList.substring(nextCookiePos, cookieList.length);
  }
  return (zoomCookie);
}

function writeZoomCookie()
{
  var expiresAt = new Date();
  var expiresAt = new Date(expiresAt.getTime() + 1000*60*60*24*365);
  document.cookie = 'zoom=' + getZoom() + ';expires=' + expiresAt;
}

function clearZoomCookie()
{
  document.cookie = 'zoom=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 
