// FontChanger
// Copyright (c) 2007 Hirotaka Ogawa
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
  id: null,
  cookieManager: null,
  cookieName1: 'body.style.fontSize',
  cookieName2:  'body.style.imageName',
  initialize: function(id) {
    this.id = id || 'fontChanger';
    this.cookieManager = new CookieManager();
    var fontSize = this.cookieManager.getCookie(this.cookieName1);
    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.cookieName1, fontSize);
  },
  reset: function() {
    document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName1);
    this.cookieManager.clearCookie(this.cookieName2);
  },
  image: function(imageName) {
	
		if (imageName == 'fontchange1') {
			$('font-image-fontchange2').src = '/english/img/fontchange2.gif';
			$('font-image-fontchange3').src = '/english/img/fontchange3.gif';
		} else if (imageName == 'fontchange2') {
			$('font-image-fontchange1').src = '/english/img/fontchange1.gif';
			$('font-image-fontchange3').src = '/english/img/fontchange3.gif';
		} else if (imageName == 'fontchange3') {
			$('font-image-fontchange1').src = '/english/img/fontchange1.gif';
			$('font-image-fontchange2').src = '/english/img/fontchange2.gif';
		}
	    $('font-image-'+imageName).src = '/english/img/'+imageName+'on.gif';
	    this.cookieManager.setCookie(this.cookieName2, imageName);
  },
  show: function() {
    var id = this.id;
    document.writeln([
'<span id="' + id + '">',
'<span id="' + id + '-small" ><a href="javascript:;" onmouseout="mOut(\'fontchange1\')" onmouseover="mOver(\'fontchange1\')"><img id="font-image-fontchange1" src="/english/img/fontchange1.gif" alt="" width="1" height="1" /></a></span>',
'<span id="' + id + '-medium"><a href="javascript:;" onmouseout="mOut(\'fontchange2\')" onmouseover="mOver(\'fontchange2\')"><img id="font-image-fontchange2" src="/english/img/fontchange2.gif" alt="Small" title="a" width="18" height="18" /></a></span>',
'<span id="' + id + '-large" ><a href="javascript:;" onmouseout="mOut(\'fontchange3\')" onmouseover="mOver(\'fontchange3\')"><img id="font-image-fontchange3" src="/english/img/fontchange3.gif" alt="Large" title="A" width="18" height="18" /></a></span>',
'</span>'
    ].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));
    var imageName = this.cookieManager.getCookie(this.cookieName2);
	if (imageName) {
		this.image(imageName);
	} else {
		this.image('fontchange2');
	}
  },
  onClickSmall:  function(e) { this.change('10px'); this.image('fontchange1'); },
  onClickMedium: function(e) { this.change('12px'); this.image('fontchange2'); },
  onClickLarge:  function(e) { this.change('14px'); this.image('fontchange3'); }
};

// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.setCookieShelfLife(90);
  window.preload_images = []
  for(var i=0; i<3; i++) {
    window.preload_images[i] = new Image();
	window.preload_images[i].src = "/english/img/fontchange" + (i+1) + "on.gif";
  }

  fontChanger.show();
};


function mOver(id)
{
    $('font-image-'+id).src = '/english/img/'+id+'on.gif';
}

function mOut(id)
{	
	var cookieName2 = 'body.style.imageName';
    var cookieManager = new CookieManager();
    var imageName = cookieManager.getCookie(cookieName2);

	if (!imageName) {
		imageName = 'fontchange2';
	}
	
	if (imageName == id) {
	    $('font-image-'+id).src = '/english/img/'+id+'on.gif';
	} else {
	    $('font-image-'+id).src = '/english/img/'+id+'.gif';
	}
}
