﻿// Cookie Functions - Second Helping (21-Jan-96)
// Written by: Bill Dortch, hIdaho Design <bdortch@netw.com>
// The following functions were released to the public domain by him.
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value,expires,path,domain,secure) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

// -->

// SET VARIABLES
var fontSize = 1;

// set currentSize to either 1, or whatever is in the cookie
var currentSize = GetCookie('textSizer');
var currentSize = parseFloat(currentSize, 12);

if (isNaN(currentSize) == true) { 
    currentSize = 1;
}

// INITIALIZE THE UI SLIDER ------------------------------------------------------------------------------------------------
function initializeUiSlider() {
    // jquery.ui slider
    $("#sliderBar").slider({
        handle: '.slider_handle',
        value: currentSize,
        min: 0.8,
        max: 1.5,
        step: 0.02,
        slide: function(event, ui) { changeFontSize(ui.value); },
        stop: function(event, ui) { saveFontSize(ui.value); } 
    });
    
    // increase button
    $("a.increaseButton").bind("click", function(event) {
         // round the current size to 1 dp
         currentSize = roundNumber(currentSize, 1);
         // if it is not at its max size
         if (currentSize < 1.5) {   
            // add 0.1 em to the font size
            currentSize = currentSize + 0.1;    
            // alter the slider value and change the font size
            $('#sliderBar').slider('option','value', currentSize);
            $('.resizeable').css('font-size', currentSize + "em");
            // save the new size to the cookie
            saveToCookie();
        }
        event.preventDefault();
    });
    
    // decrease button (same as increase button in functionality)
    $("a.decreaseButton").bind("click", function(event){
        currentSize = roundNumber(currentSize, 1);
        if (currentSize > 0.8) {
            currentSize = currentSize - 0.1;
            $('#sliderBar').slider('option','value', currentSize);
            $('.resizeable').css('font-size', currentSize + "em");
            saveToCookie();
        }
        event.preventDefault();
    });
}

// FONT SIZE FUNCTIONS ----------------------------------------------------------------------------------------------------------------
    function changeFontSize (ui) {
        // get the slider value
               
        currentSize = ui;
        
        if (currentSize == null) { 
            currentSize = 1;
            SetCookie('textSizer', 1, null, "/");
        }

        // set the font size
        $('.resizeable').css("font-size", (currentSize + "em"));
    }

    function saveFontSize (ui) {
        //alert(currentSize);
        
        // get the slider value
        currentSize = ui;        
        // save to a cookie
        
        saveToCookie();
    }  

    function saveToCookie () {
        
        var fontSize = parseFloat(currentSize, 10);        
        SetCookie('textSizer', fontSize, null, "/");
    }

    function roundNumber(num, dec) {
        var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
        return result;
    }
    
    $(function() {    
        var ckValue = GetCookie('textSizer');
        initializeUiSlider();
        changeFontSize(ckValue);
    });