﻿// FlucutatePageClass.js
// Changes 'body' class name

var FluctuatePageClass = Class.create({
    initialize: function(opts) {
        this.cookieName = 'FluctuatePageCookie';
        this.cookieDuration = 1;
        this.inc = 1;
        this.nameSpace = 'textSize';
        this.max = 5;
        if (opts) {
            if (opts.start) {
                this.inc = opts.start;
                this.setClass();
            }
            if (opts.max) {
                this.max = opts.max;
            }
        }
        if (this.getCookie(this.cookieName)) {
            this.inc = this.getCookie(this.cookieName);
            this.setClass();
        }
    },
    removeClass: function() {
        $$('body')[0].removeClassName(this.nameSpace + this.inc);
    },
    setClass: function() {
        this.setCookie(this.cookieName, this.inc, this.cookieDuration);
        $$('body')[0].addClassName(this.nameSpace + this.inc);
    },
    setCookie: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    getCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var 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;
    },
    removeCookie: function(name) {
        this.setCookie(name, "", -1);
    },
    increase: function() {
        if (this.inc < this.max) {
            this.removeClass(this.inc);
            this.inc++;
            this.setClass();
        }
    },
    decrease: function() {
        if (this.inc > 1) {
            this.removeClass();
            this.inc--;
            this.setClass();
        }
    }
});
