//Title:                Font Switcher
//Purpose:              Enlarge, shrink, and reset the font size of a document
//Author:               Sean Robinson
//Date Created:         2007
//Date Modified:        2007-07-09
//Version:              0.5

//Test to see if we have a cookie set; if so, read in data
var cookieData = document.cookie;
var cPos = cookieData.indexOf("fontSize=");
if (cPos != -1) {
  var cStar = cPos + 9;
  var cEnd = cookieData.indexOf("'", cStar);
  if (cEnd == -1)
    cEnd = cookieData.length;
  var currentFont = cookieData.substring(cStar, cEnd);
}

//Variables
//Set default font size
var defaultFont = 14;
if (cPos == -1)
  var currentFont = defaultFont;
var previousFont = currentFont;

//Display content in Web page
//We do it this way to prevent non-JavaScript users from seeing it.
function displayFontSwitcher(status) {
  if (status == 0) {
    document.getElementById('fontSwitcher').innerHTML = "Change Text Size";
  }
  else if(status == 1) {
    if (fSize = prompt("Text size:", currentFont)) {
      previousFont = currentFont;
      changeFont(fSize);
      previewFont();
    }
  }
}

function previewFont() {
  if(confirm("Accept current size?")) {
    var expDate = new Date();
    expDate.setFullYear(expDate.getFullYear() + 2);
    document.cookie = 'fontSize=' + currentFont + '; expires=' + expDate.toGMTString() + '; path=/';
  }
  else {
    changeFont(previousFont);
    displayFontSwitcher(1);
  }
}

function changeFont(change) {
  if(change != currentFont) {
    currentFont = change;
    document.body.style.fontSize = change + "px";
  }
}

