// 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();
    var fontSize = this.cookieManager.getCookie(this.cookieName);
    if (fontSize) document.body.style.fontSize = fontSize;
  },
  setCookieShelfLife: function(days) {
    this.cookieManager.cookieShelfLife = days;
  },
  change: function(fontSize) {
    document.body.style.fontSize = fontSize;
    this.cookieManager.setCookie(this.cookieName, fontSize);
	
	var id = this.id;
	// 選択されている内容でメニューを切替
	if(fontSize=="68%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small_on.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large.gif" width="36" height="14" alt="大" />';
	}else if(fontSize=="76%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium_on.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large.gif" width="36" height="14" alt="大" />';
	}else if(fontSize=="90%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large_on.gif" width="36" height="14" alt="大" />';
	}
  },
  reset: function() {
    document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {
    var id = this.id;
	
    document.writeln(
		'<div id="' + id + '">',
		'<dl>',
		'<dt><img src="images/size.gif" alt="文字サイズ" width="62" height="13" border="0"></dt>',
		'<dd class="fontChanger01" id="' + id + '-small" ><img src="images/small.gif" width="36" height="14" alt="小" /></dd>',
		'<dd class="fontChanger02" id="' + id + '-medium"><img src="images/medium_on.gif" width="36" height="14" alt="中" /></dd>',
		'<dd class="fontChanger03" id="' + id + '-large" ><img src="images/large.gif" width="36" height="14" alt="大" /></dd>',
		'</dl>',
		'</div>');
	
	
	// 選択されている内容でメニューを切替
	if(this.cookieManager.getCookie(this.cookieName)=="68%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small_on.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large.gif" width="36" height="14" alt="大" />';
	}else if(this.cookieManager.getCookie(this.cookieName)=="76%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium_on.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large.gif" width="36" height="14" alt="大" />';
	}else if(this.cookieManager.getCookie(this.cookieName)=="90%"){
		document.getElementById(id + '-small').innerHTML = '<img src="images/small.gif" width="36" height="14" alt="小" />';
		document.getElementById(id + '-medium').innerHTML = '<img src="images/medium.gif" width="36" height="14" alt="中" />';
		document.getElementById(id + '-large').innerHTML = '<img src="images/large_on.gif" width="36" height="14" alt="大" />';
	}
	
    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('68%' ); },
  onClickMedium: function(e) { this.change('76%'); },
  onClickLarge:  function(e) { this.change('90%'); }
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.show();
};
