window.prt_utilities = function () {
    var prefsLoaded = false,
        defaultFontSize = 100,
        currentFontSize = defaultFontSize;

    var setFontSize = function (fontSize) {
        document.body.style.fontSize = fontSize + '%';
    };

    var changeFontSize = function (sizeDifference) {
        currentFontSize = parseInt(currentFontSize, 10) + parseInt(sizeDifference * 5, 10);
        // if (currentFontSize > 220) {
        // currentFontSize = 220;
        // }else if (currentFontSize < 60) {
        // currentFontSize = 60;
        // };
        setFontSize(currentFontSize);
    };

    var revertStyles = function () {
        currentFontSize = defaultFontSize;
        changeFontSize(0);
    };

    var createCookie = function (name, value, days) {
        var date = new Date(),
        expires = "";
        if (days) {
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    };

    var readCookie = function (name) {
        var nameEQ = name + "=",
        ca = document.cookie.split(';'),
        c = '';
        for (var i = 0; i < ca.length; i += 1) {
            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;
    };

    var setUserOptions = function () {
        var cookie = "";
        if (!prefsLoaded) {
            cookie = readCookie("fontSize");
            currentFontSize = cookie ? cookie : defaultFontSize;
            setFontSize(currentFontSize);
            prefsLoaded = true;
        }
    };

    var saveSettings = function () {
        createCookie("fontSize", currentFontSize, 365);
    };

    var onDocLoad = function () {
        var top_nav_element = document.getElementById("top_nav");
		top_nav_element.innerHTML = 'Fontsize: <ul>' +
		    '<li><a href="#" title="Increase size" onclick="prt_util.changeFontSize(2); return false;" ' +
			    'class="larger">bigger</a><span class="hideme"> . </span></li> ' + 
		    '<li><a href="#" title="Decrease size" onclick="prt_util.changeFontSize(-2); return false;" ' +
			    'class="smaller">smaller</a><span class="hideme"> . </span></li> ' +
		    '<li><a href="#" title="Revert styles to default" onclick="prt_util.revertStyles(); return false;" ' +
			    'class="reset">reset</a><span class="hideme"> . </span></li></ul><hr class="hideme" />' + top_nav_element.innerHTML;
        setUserOptions();		
    };

    return {
        onDocLoad: onDocLoad,
        saveSettings: saveSettings,
        changeFontSize: changeFontSize,
        revertStyles: revertStyles,
    };
};

window.prt_util = window.prt_utilities();
window.onload = window.prt_util.onDocLoad;
window.onunload = window.prt_util.saveSettings;