// FontChanger
// Copyright (c) 2007 Hirotaka Ogawa
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
	fs: new Array("fs","fm","fl"),
  id: null,
  cookieManager: null,
  cookieName: 'body.style.fontSize',
  initialize: function(id) {
    this.id = id || 'fsize';
    this.cookieManager = new CookieManager();
    var className = this.cookieManager.getCookie(this.cookieName);
    if (className){
			Element.addClassName(document.body, className);
		}else{
			className = "fm";
			Element.addClassName(document.body, className);
		}
		this.activateBtn(className);
  },
  setCookieShelfLife: function(days) {
    this.cookieManager.cookieShelfLife = days;
  },
  change: function(className) {
    var fs = this.fs;
		var clsName = Element.classNames(document.body);
		$A(clsName).each(function(obj){
			fs.each(function(f){
				if(obj==f){
					clsName.remove(obj);
				}
			});
		});
    Element.addClassName(document.body, className);
		this.activateBtn(className);
  this.cookieManager.setCookie(this.cookieName, className);
  },
  reset: function() {
    Element.addClassName(document.body, '');
    this.cookieManager.clearCookie(this.className);
  },
  show: function() {
    var id = this.id;
    Event.observe($(id + '-s' ), 'click', this.onClickMedium.bind(this));
    Event.observe($(id + '-m'), 'click', this.onClickLarge.bind(this));
    Event.observe($(id + '-l' ), 'click', this.onClickExtraLarge.bind(this));
  },
  onClickMedium:  function(e) { this.change('fs' ); },
  onClickLarge: function(e) { this.change('fm'); },
  onClickExtraLarge:  function(e) { this.change('fl'); },
	activateBtn: function(id) {
		var imgfs = document.getElementsByClassName("imgfs");
//		var imgfs = document.getAttribute("imgfs");
		imgfs.each(function(e){
			// 文字サイズ変更ボタンに対する処理
			var src = e.getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
//			src = src.replace('_ov'+ftype, ftype);
			var hsrc = src.replace(ftype, '_ov'+ftype);
			if (e.getAttribute('id') == id){
				e.setAttribute('src', hsrc);
			}else{
				hsrc = src.replace('_ov'+ftype, ftype);
				e.setAttribute('src', hsrc);
			}
		});
	}
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.show();
};












