//
// 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;
 	var fontsizeid;
    if (fontSize) {
		if (fontSize=='110%'){     fontsizeid ='3';
		}else if (fontSize=='70%'){	fontsizeid ='1';
	    }else{						fontsizeid ='2';
	    }
    }
	if(typeof fontsizeid=='undefined'){ fontsizeid ='2'; }
	document.getElementById('chfontsize').src = pass+"misc/images/bt_fontsize0"+ fontsizeid +".gif";
  },
  setCookieShelfLife: function(days) {
    this.cookieManager.cookieShelfLife = days;
  },
  change: function(fontSize) {
    document.body.style.fontSize = fontSize;
    this.cookieManager.setCookie(this.cookieName, fontSize);
  },
  srcchange: function(imgsrc){
    document.getElementById('chfontsize').src = pass+"misc/images/bt_fontsize0"+ imgsrc +".gif";
  },
  backsrc: function(){
  	var fontsizeid;
  	var fontSize = this.cookieManager.getCookie(this.cookieName);
    if (fontSize) {
		if (fontSize=='110%'){   	fontsizeid ='3';
		}else if (fontSize=='70%'){ fontsizeid ='1';
		}else{				    	fontsizeid ='2';
	    }
	}
	if(typeof fontsizeid=='undefined'){ fontsizeid ='2'; }
    document.getElementById('chfontsize').src = pass+"misc/images/bt_fontsize0"+ fontsizeid +".gif";
  },
  reset: function() {
    document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {
    var id = this.id;
    document.writeln([
'<map id="' + id + '" name="chsize">',
'<area shape="rect" coords="107,6,126,26" style="cursor:pointer;" id="' + id + '-small" title="小" />',
'<area shape="rect" coords="131,8,150,26" style="cursor:pointer;" id="' + id + '-medium" title="中" />',
'<area shape="rect" coords="155,7,174,26" style="cursor:pointer;" id="' + id + '-large" title="大" />',
'</map>'
    ].join("\n"));
    Event.observe($(id), 'load', this.onload.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));
    
    Event.observe($(id + '-small' ), 'mouseover', this.onMouseOverSmall.bind(this));
    Event.observe($(id + '-medium'), 'mouseover', this.onMouseOverMedium.bind(this));
    Event.observe($(id + '-large' ), 'mouseover', this.onMouseOverLarge.bind(this));
    
    Event.observe($(id + '-small' ), 'mouseout', this.onMouseOutSmall.bind(this));
    Event.observe($(id + '-medium'), 'mouseout', this.onMouseOutMedium.bind(this));
    Event.observe($(id + '-large' ), 'mouseout', this.onMouseOutLarge.bind(this));
  },
  onload: function(e) { this.backsrc(); },
  
  onClickSmall:  function(e) { this.change('70%' ); },
  onClickMedium: function(e) { this.change('82%'); },
  onClickLarge:  function(e) { this.change('110%'); },
  
  onMouseOverSmall:  function(e) { this.srcchange(1); },
  onMouseOverMedium: function(e) { this.srcchange(2); },
  onMouseOverLarge:  function(e) { this.srcchange(3); },
  
  onMouseOutSmall:  function(e) { this.backsrc(); },
  onMouseOutMedium: function(e) { this.backsrc(); },
  onMouseOutLarge:  function(e) { this.backsrc(); }
 
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.show();
};
