﻿var fontSizeSwitcher = {
    str: '',
    config: {
        area: ['content'],
        length: 3,
        id: ['FS1', 'FS2', 'FS3'],
        label: ['M', 'L', 'LL'],
        size: ['100%', '114%', '129%'],
        cookieName: 'exampleName',
        cookieDate: '90'
    },
    cookie: {
        set: function(n, v) {
            var t = new Date();
            t.setTime(t.getTime() + (1000 * 60 * 60 * 24 * fontSizeSwitcher.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('fontSizeSwitcherList').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] + '"><a href="#" onclick="fontSizeSwitcher.changeFontSize(' + j + ')">' + config.label[j] + '</a></li>';
        }

        document.write('<dl id="fontSizeSwitcher"><dt>文字サイズ : </dt><dd><ul id="fontSizeSwitcherList">' + this.str + '</ul></dd></dl>');

        if(i == '') {
            i = 1;
        }

        try {
            window.addEventListener("load", function() {
                fontSizeSwitcher.changeFontSize(i)
            }, false);
        }
        catch(e) {
            window.attachEvent("onload", function() {
                fontSizeSwitcher.changeFontSize(i)
            });
        }
    }
}

fontSizeSwitcher.start();