//
// 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=='116%'){     fontsizeid ='3';
		}else if (fontSize=='86%'){	fontsizeid ='1';
	    }else{						fontsizeid ='2';
	    }
    }
	if(typeof fontsizeid=='undefined'){ fontsizeid ='2'; }
	if(document.getElementById('chfontsize')){
		document.getElementById('chfontsize').src = "pages/design/common/img/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){
	if(document.getElementById('chfontsize')){
	    document.getElementById('chfontsize').src = "pages/design/common/img/bt_fontsize0"+ imgsrc +".gif";
    }
  },
  backsrc: function(){
  	var fontsizeid;
  	var fontSize = this.cookieManager.getCookie(this.cookieName);
    if (fontSize) {
		if (fontSize=='116%'){   	fontsizeid ='3';
		}else if (fontSize=='86%'){ fontsizeid ='1';
		}else{				    	fontsizeid ='2';
	    }
	}
	if(typeof fontsizeid=='undefined'){ fontsizeid ='2'; }
		if(document.getElementById('chfontsize')){
		    document.getElementById('chfontsize').src = "pages/design/common/img/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="139,8,159,27" style="cursor:pointer;" id="' + id + '-small" title="¬">',
'<area shape="rect" coords="162,8,181,27" style="cursor:pointer;" id="' + id + '-medium" title="’†">',
'<area shape="rect" coords="183,8,203,27" 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('86%' ); },
  onClickMedium: function(e) { this.change('100%'); },
  onClickLarge:  function(e) { this.change('116%'); },
  
  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();
};

