﻿// FontChanger
// REQUIRES: prototype.js, cookiemanager.js

FontChanger = Class.create();
FontChanger.prototype = {
  id: null,
  contentsname: null,
  cookieManager: null,
  cookieName: 'body.style.fontSize',
  floorPath: null,
  domain : "http://www.mcservice.co.jp/pre/", //domain
  initialize: function(id,contentsname, floorNum) {
	this.id = id || 'fontChanger';
	this.contentsname = contentsname || 'middlecontents';
	this.cookieManager = new CookieManager();
	
	var path = location.href;
	
	path = path.replace(this.domain, "a");
	fNum = path.split("/").length - 1;
	var str = "";
	for(var i = 0; i < fNum; i++){
	  str += "../";
	}
	
	this.floorPath = str;
  },
  setCookieShelfLife: function(days) {
	this.cookieManager.cookieShelfLife = days;
  },
  change: function(fontSize) {
	var d = $(this.contentsname);
	d.style.fontSize = fontSize;
	this.cookieManager.setCookie(this.cookieName, fontSize);
	this.change_img(fontSize);
  },
  change_first: function(contentsname) {
	var fontSize = this.cookieManager.getCookie(this.cookieName);
	if(navigator.userAgent.indexOf("MSIE 6.") >= 0){
		if(!fontSize)fontSize = '90%';

	}else{
		if(!fontSize)fontSize = '90%';
	}
	var d = $(contentsname);
	d.style.fontSize = fontSize;
	this.change_img(fontSize);
  },
  change_img: function(fontSize) {
	var id = this.id;
	var small='size_small_on',middle='size_middle_on',big='size_big_on';
	if(navigator.userAgent.indexOf("MSIE 6.") >= 0){
		if(fontSize == '90%'){ small = 'size_small_on'; middle = 'size_middle'; big = 'size_big'; }

	}else{
		if(fontSize == '90%'){ small = 'size_small_on'; middle = 'size_middle'; big = 'size_big'; }
	}
	if(fontSize == '100%'){ small = 'size_small'; middle = 'size_middle_on'; big = 'size_big'; }
	if(fontSize == '140%'){ small = 'size_small'; middle = 'size_middle'; big = 'size_big_on'; }

	var d = $(id + '-small');
	d.src = this.floorPath + 'img/common/' + small + '.gif';
	d = $(id + '-middle');
	d.src = this.floorPath + 'img/common/' + middle + '.gif';
	d = $(id + '-big');
	d.src = this.floorPath + 'img/common/' + big + '.gif';

  },
  reset: function() {
	var d = $(this.contentsname);
	d.style.fontSize = fontSize;
	this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {
	var id = this.id;
	var small='small_o',middle='middle',big='big';
	var fontSize = this.cookieManager.getCookie(this.cookieName);
	if(navigator.userAgent.indexOf("MSIE 6.") >= 0){
		if(!fontSize)fontSize = '90%';

	}else{
		if(!fontSize)fontSize = '90%';
	}
	document.writeln([
'<a href="javascript:void(0)" onfocus="this.blur()"><img src="' + this.floorPath + 'img/common/size_small.gif" alt="小" title="小" id="' + id + '-small" /></a>',
'<a href="javascript:void(0)" onfocus="this.blur()"><img src="' + this.floorPath + 'img/common/size_middle.gif" alt="中" title="中" id="' + id + '-middle" /></a>',
'<a href="javascript:void(0)" onfocus="this.blur()"><img src="' + this.floorPath + 'img/common/size_big.gif" alt="大" title="大" id="' + id + '-big" /></a>',
'<img src="' + this.floorPath + 'img/common/size_big_on.gif" width="1" height="1" id="tmp_b" /><img src="' + this.floorPath + 'img/common/size_middle_on.gif" width="1" height="1" id="tmp_m" /><img src="' + this.floorPath + 'img/common/size_small_on.gif" width="1" height="1" id="tmp_s" />'
	].join("\n"));
	Event.observe($(id + '-small' ), 'click', this.onClickSmall.bind(this));
	Event.observe($(id + '-middle'), 'click', this.onClickMiddle.bind(this));
	Event.observe($(id + '-big' ), 'click', this.onClickBig.bind(this));

	this.change_img(fontSize);
  },

  onClickSmall:  function(e) { 
		if(navigator.userAgent.indexOf("MSIE 6.") >= 0){
		this.change('90%' ); 
		}else{
		this.change('90%' ); 
		}
	},
  onClickMiddle: function(e) { this.change('100%'); },
  onClickBig:  function(e) { this.change('140%'); }
};
// Bootstrap
FontChanger.start = function(id,contentsname, floorNum) {
  var fontChanger = new FontChanger(id,contentsname, floorNum);
  fontChanger.show();
};
FontChanger.change_first = function(id,contentsname, floorNum){
  var d = $('tmp_s');
  d.style.display = 'none';
  d = $('tmp_m');
  d.style.display = 'none';
  d = $('tmp_b');
  d.style.display = 'none';

  var fontChanger = new FontChanger(id,contentsname, floorNum);
  fontChanger.change_first(contentsname);
  
};
