// FontChanger
// Copyright (c) 2007 Hirotaka Ogawa
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
  id: null,
  cookieManager: null,
  cookieName: 'body.style.fontSize',
  smallSize: '80%',
  mediumSize: '100%',
  largeSize: '120%',
  onSmall: '',
  onMedium: '',
  onLarge: '',
  initialize: function(id) {
    this.id = id || 'fontChanger';
    this.cookieManager = new CookieManager();
    this.cookieManager.cookieShelfLife = 30;
    var fontSize = this.cookieManager.getCookie(this.cookieName);
    if (fontSize) document.body.style.fontSize = fontSize;
  },
  change: function(fontSize) {
    var id = this.id;
    document.body.style.fontSize = fontSize;
    this.cookieManager.setCookie(this.cookieName, fontSize);
    switch (fontSize) {
        case this.smallSize:
            this.onSmall = id + '-on';
            this.onMedium = '';
            this.onLarge = '';
            break;
        case this.mediumSize:
            this.onSmall = '';
            this.onMedium = id + '-on';
            this.onLarge = '';
            break;
        case this.largeSize:
            this.onSmall = '';
            this.onMedium = '';
            this.onLarge = id + '-on';
            break;
        default:
            this.onSmall = '';
            this.onMedium = id + '-on';
            this.onLarge = '';
            break;
    }
    $(id + '-small').className = this.onSmall;
    $(id + '-medium').className = this.onMedium;
    $(id + '-large').className = this.onLarge;
  },
  reset: function() {
    document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {
    var id = this.id;
    document.writeln([
'<div id="' + id + '">',
'<div id="' + id + '-label">Change font size:</div>',
'<ul>',
'<li style="cursor: pointer; font-size: 80% ;" id="' + id + '-small">small</li>',
'<li style="cursor: pointer; font-size: 100%;" id="' + id + '-medium">mediun</li>',
'<li style="cursor: pointer; font-size: 120%;" id="' + id + '-large" >large</li>',
'</ul>',
'</div>'
    ].join("\n"));
    Event.observe($(id + '-small' ), 'click', this.onClickSmall.bind(this));
    Event.observe($(id + '-medium'), 'click', this.onClickMedium.bind(this));
    Event.observe($(id + '-large' ), 'click', this.onClickLarge.bind(this));
    this.change(this.cookieManager.getCookie(this.cookieName) || this.mediumSize);
  },
  onClickSmall:  function(e) { this.change(this.smallSize); },
  onClickMedium: function(e) { this.change(this.mediumSize); },
  onClickLarge:  function(e) { this.change(this.largeSize); }
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.show();
};