/* The following is based on code from
   http://www.white-hat-web-design.co.uk/articles/js-fontsize.php

   I changed it to assume that the font size is set on the BODY element,
   of which there is only one, so there's no need to iterate over all of
   the paragraphs.  I made some other minor modifications. (For example,
   parseInt stops at non-digits, so there's no need to get rid of the
   units of the existing font size.)

   This version reads a cookie called "fontSize and, if it's set, sets the
   font size to that value. Any function that sents the fontsize also sets
   the cookie.  Relies on the cookies.js file.

   Scott D. Anderson
   Fall 2009
*/

// cookies last for 30 days by default.
const cookie_duration = 30;

const defaultFontSize = 12;
const minFontSize = 8;
const maxFontSize = 28;

var currentFontSize = defaultFontSize;

// Read the cookie, if any, and sets the default font size variable, and the body font
function readCookieFontSize() {
    var cval = getCookie("fontSize");
    if (cval != "") {
        currentFontSize = parseInt(cval);
    }
    setBodyFontSize(currentFontSize);
}

/* function to return the DOM object corresponding to the BODY element of the document */
function getBodyElt() {
    var elts = document.getElementsByTagName('body');
    if( elts.length == 0 ) {
        alert("Couldn't find body tag; how is that possible?");
        return;
    }
    if( elts.length > 1 ) {
        alert("Found more than one body tag; how is that possible?");
        return;
    }
    return elts[0];
}

function setFontSize(bodyelt,size) {
    // finally, modify the element's style
    bodyelt.style.fontSize = size+"pt";
    currentFontSize=size;
    setCookie("fontSize",size,cookie_duration);
}

/* function to increase the current font size by 1pt, up to the maximum */
function increaseFontSize() {
    var bodyelt = getBodyElt();
    var size = readCookieFontSize()
    if( bodyelt.style.fontSize ) {
        // fontSize attribute exists, so find out what the current
        // fontSize is and replace the default
        size = parseInt(bodyelt.style.fontSize);
    }
    if( size < maxFontSize ) {
        size = size + 1;
    }
    setFontSize(bodyelt,size);
}

/* function to decrease the current font size by 1pt, down to the minimum */
function decreaseFontSize() {
    var bodyelt = getBodyElt();
    var size = 12;              // default value
    if( bodyelt.style.fontSize ) {
        // fontSize attribute exists, so find out what the current
        // fontSize is and replace the default
        size = parseInt(bodyelt.style.fontSize);
    }
    if( size > minFontSize ) {
        size = size - 1;
    }
    setFontSize(bodyelt,size);
}

function makeFontIncreaser() {
    document.writeln("<li><a href='javascript:increaseFontSize()' style='font-size: larger'>A</a>");
}
    
function makeFontDecreaser() {
    document.writeln("<li><a href='javascript:decreaseFontSize()' style='font-size: smaller'>a</a>");
}

function setBodyFontSize(size) {
    var elts = document.getElementsByTagName('body');
    var bodyelt = elts[0];
    setFontSize(bodyelt,size);
}

function makeFontSizeControl(size) {
    document.writeln("<li><a href='javascript:setBodyFontSize("+size+");'"+
                     "       style='font-size: "+size+"pt'>"+size+"pt</a> ");
}

function hide(eltId) {
    var elt = document.getElementById(eltId);
    if( elt ) {
        elt.style.visibility='hidden';
        elt.style.overflow='hidden';
        elt.style.height='0px';
    }
}

function hideAccessibilityBar() {
    hide('accessibilitybar');
}

function makeAccessibilityBar() {
    document.writeln("<div id='accessibilitybar'><ul>");
    document.writeln("<a href='javascript:hideAccessibilityBar()'>hide this</a>");
    makeFontDecreaser();
    makeFontSizeControl(8);
    makeFontSizeControl(12);
    makeFontSizeControl(16);
    makeFontSizeControl(20);
    makeFontSizeControl(24);
    makeFontIncreaser();
    document.writeln("</ul></div>");
}

// always read the cookie

readCookieFontSize();
makeAccessibilityBar();
