var fontsize = {
    str: '',
    config: {
        area: ['content'], // 対象となるid
        length: 3, // 切り替え段階数
        id: ['fontsmall', 'fontmedium', 'fontlarge'], // 切り替えボタンの各id
        label: ['サイズを小さく', '標準サイズ', 'サイズを大きく'], // 切り替えボタンの各ラベル
        size: ['80%', '100%', '130%'], // 切り替えサイズ 
        cookieName: 'ishihaken_fontsize', // クッキー名
        cookieDate: '90' // クッキーの有効日数
    },

    cookie: {
        set: function(n, v) {
            var t = new Date();
            t.setTime(t.getTime() + (1000 * 60 * 60 * 24 * fontsize.config.cookieDate));
            document.cookie = n + '=' + encodeURIComponent(v) + '; path=/; expires=' + t.toGMTString();
        },
        get: function(n, m) {
            return (m = ('; ' + document.cookie + ';').match('; ' + n + '=(.*?);')) ? decodeURIComponent(m[1]) : '';
        }
    },

    changeFontSize: function(i) {
        var config = this.config;

        var items = document.getElementById('fontsize').childNodes;
        for(var j = 0; j < items.length; j++) {
            if(i == j) {
                items[j].setAttribute('class', 'current');
            }
            else {
                items[j].removeAttribute('class');
            }
        }

        for(var j = 0, l = config.area.length; j < l; j++) {
            document.getElementById(config.area[j]).style.fontSize = config.size[i];
        }

        // set cookie
        this.cookie.set(config.cookieName, i);
    },

    start: function() {
        var config = this.config;
        var i      = this.cookie.get(config.cookieName, 's');

        for(var j = 0; j < config.length; j++) {
            this.str += '<li id="' + config.id[j] + '" onclick="fontsize.changeFontSize(' + j + ')">' + config.label[j] + '</li>';
        }

        document.write('<ul id="fontsize">' + this.str + '</ul>');

        if(i == '') {
            i = 1;
        }

        try {
            window.addEventListener("load", function() {
                fontsize.changeFontSize(i)
            }, false);
        }
        catch(e) {
            window.attachEvent("onload", function() {
                fontsize.changeFontSize(i)
            });
        }
    }
}

fontsize.start();
