// 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;
		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);
	},

	reset: function() {
		document.body.style.fontSize = '';
		this.cookieManager.clearCookie(this.cookieName);
	},

	show: function() {
		var id = this.id;
		document.writeln([
			'<div id="FontSize">',
			'<dl>',
			'<dt><img src="http://www.mishin-shop.co.jp/common/images/title_font.gif" width="64" height="18" alt="文字サイズ" /></dt>',
			'<dd><span><img id="' + id + 'Small" src="http://www.mishin-shop.co.jp/common/images/font_s.gif" width="18" height="18" alt="小" /></span></dd>',
			'<dd><span><img id="' + id + 'Medium" src="http://www.mishin-shop.co.jp/common/images/font_m.gif" width="18" height="18" alt="中" /></span></dd>',
			'<dd><span><img id="' + id + 'Large" src="http://www.mishin-shop.co.jp/common/images/font_l.gif" width="18" height="18" alt="大" /></span></dd>',
			'</dl>',
			'</div>',


		].join("\n"));
		Event.observe(window, 'load', this.setSize.bind(this));
		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));
	},

	setSize: function(e){
		var Size = document.body.style.fontSize;
		if(Size == '77%'){
			$(this.id + 'Small').src = $(this.id + 'Small').src.replace(".gif", "_on.gif");
		} else if(Size == '114%'){
			$(this.id + 'Large').src = $(this.id + 'Large').src.replace(".gif", "_on.gif");
		} else{
			$(this.id + 'Medium').src = $(this.id + 'Medium').src.replace(".gif", "_on.gif");
		}
	},

	onClickSmall:  function(e) {
		if($(this.id + 'Small').src.lastIndexOf("_on.gif") == -1){
			$(this.id + 'Small').src = $(this.id + 'Small').src.replace(".gif", "_on.gif");
			$(this.id + 'Medium').src = $(this.id + 'Medium').src.replace("_on.gif", ".gif");
			$(this.id + 'Large').src = $(this.id + 'Large').src.replace("_on.gif", ".gif");
		}
		this.change('77%');
	},

	onClickMedium: function(e) {
		if($(this.id + 'Medium').src.lastIndexOf("_on.gif") == -1){
			$(this.id + 'Small').src = $(this.id + 'Small').src.replace("_on.gif", ".gif");
			$(this.id + 'Medium').src = $(this.id + 'Medium').src.replace(".gif", "_on.gif");
			$(this.id + 'Large').src = $(this.id + 'Large').src.replace("_on.gif", ".gif");
		}
		this.change('92%');
	},

	onClickLarge:  function(e) {
		if($(this.id + 'Large').src.lastIndexOf("_on.gif") == -1){
			$(this.id + 'Small').src = $(this.id + 'Small').src.replace("_on.gif", ".gif");
			$(this.id + 'Medium').src = $(this.id + 'Medium').src.replace("_on.gif", ".gif");
			$(this.id + 'Large').src = $(this.id + 'Large').src.replace(".gif", "_on.gif");
		}
		this.change('114%');
	}
};

// Bootstrap
FontChanger.start = function(id) {
	var fontChanger = new FontChanger(id);
	fontChanger.show();
};