// FontChanger
// Copyright (c) 2007 Hirotaka Ogawa
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
  id: null,
  cookieManager: null,
  cookieName: 'body.style.fontSize',
  initialize: function(id) {
    this.id = id || 'fontChanger';
    this.cookieManager = new CookieManager();

  },
  setCookieShelfLife: function(days) {
    this.cookieManager.cookieShelfLife = days;
  },
  change: function(fontSize) {

    if(fontSize == ''){
      fontSize = this.cookieManager.getCookie(this.cookieName);
      if(fontSize == '' || fontSize == 'null'){
        fontSize = '80%';
      }
    }

    document.body.style.fontSize = fontSize;
    this.cookieManager.setCookie(this.cookieName, fontSize);

    var imgSS = "/truj/common/images/fsize_small.gif";
    var imgMM = "/truj/common/images/fsize_middle.gif";
    var imgLL = "/truj/common/images/fsize_big.gif";
    if(fontSize == '100%'){
        imgLL = "/truj/common/images/fsize_big_on.gif";
    }else if(fontSize == '65%'){
        imgSS = "/truj/common/images/fsize_small_on.gif";
    }else{ // if(fontSize == "80%"){
        imgMM = "/truj/common/images/fsize_middle_on.gif";
    }
    var id = this.id;
    $(id + '-small' ).src = imgSS;
    $(id + '-medium' ).src = imgMM;
    $(id + '-large' ).src = imgLL;
  },
  reset: function() {
    document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {

    var id = this.id;
    document.writeln([
'<div id="' + id + '">',
'<a href="#" onclick="print();"><img src="/truj/common/images/btn_print.gif" alt="このページを印刷する" width="119" height="24" border="0" align="absmiddle"></a>',
'<img src="/truj/common/images/btn_fontsize.gif" alt="文字サイズ変更" width="70" height="10" align="absmiddle" class="mg5r" />',
'<img id="' + id + '-small" src="/common/images/fsize_small.gif" alt="小" width="18" height="18" style="cursor:pointer" align="absmiddle" />',
'<img id="' + id + '-medium" src="/truj/common/images/fsize_middle_on.gif" alt="中" width="18" height="18" style="cursor:pointer" align="absmiddle" class="mg4lr" />',
'<img id="' + id + '-large" src="/truj/common/images/fsize_big.gif" alt="大" width="18" height="18" style="cursor:pointer" align="absmiddle" />',
'</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));
  },
  onClickSmall:  function(e) { this.change('65%' ); },
  onClickMedium: function(e) { this.change('80%'); },
  onClickLarge:  function(e) { this.change('100%'); }
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.setCookieShelfLife(90);
  fontChanger.show();

  fontChanger.change('');
};

