var yomotsuSwitchFontSize = {

  conf : {
    fontSize    : ["140%","120%","100%"],
    switchId    : ["switchFontSizeBig", "switchFontSizeMedium", "switchFontSizeSmall"],
    defoSizeId  : "switchFontSizeMedium",
    targetAreaId: "page",              //フォントサイズを適用するエリアのID
    cookieName  : "sougistation",      //クッキーの名前
    cookieLimit : 30,                     //有効期限(日にち)
    switchWriteArea : "utility",          //指定したIDのエリアの一番最後にスイッチのHTMLが書き込まれる
    switchHTML      : '<ul><li id="switchFontSizeSmall"><img src="/img/share/fontchange_s.gif" alt="小" /></li><li id="switchFontSizeMedium"><img src="/img/share/fontchange_m.gif" alt="中" /></li><li id="switchFontSizeBig"><img src="/img/share/fontchange_l.gif" alt="大" /></li></ul>'

// switchHTMLの参考用デフォルト値
//
//  <ul>
//  <li id="switchFontSizeBig">大</li>
//  <li id="switchFontSizeMedium">中</li>
//  <li id="switchFontSizeSmall">小</li>
//  </ul>

  },

  main : function(){
    yomotsuSwitchFontSize.setHTML();
    yomotsuSwitchFontSize.defo();
    var i, j, switchItem = yomotsuSwitchFontSize.conf.switchId;

    for(i=0;i<switchItem.length;i++){
      document.getElementById(switchItem[i]).onclick = yomotsuSwitchFontSize.action;
    }
  },

  setHTML : function(){
    var fontsizeSwitch = document.createElement('div');
    fontsizeSwitch.id  = "fontsizeControl";
    fontsizeSwitch.innerHTML = yomotsuSwitchFontSize.conf.switchHTML;

    document.getElementById(yomotsuSwitchFontSize.conf.switchWriteArea).appendChild(fontsizeSwitch);
  },

  defo : function(){
    var i;
    var switchId = yomotsuSwitchFontSize.conf.switchId;
    var targetAreaId = yomotsuSwitchFontSize.conf.targetAreaId;
    var fontSize = yomotsuSwitchFontSize.conf.fontSize;

    cookieValue = this.getCookie() || yomotsuSwitchFontSize.conf.defoSizeId;
    for(i = 0; i < switchId.length; i++){
      if(cookieValue == switchId[i]){
        document.getElementById(targetAreaId).style.fontSize = fontSize[i];
      }
    }
    document.getElementById(cookieValue).className ="active";
  },

  action : function(){
    var i;
    var switchId = yomotsuSwitchFontSize.conf.switchId;
    var targetAreaId = yomotsuSwitchFontSize.conf.targetAreaId
    var fontSize = yomotsuSwitchFontSize.conf.fontSize

    for(i=0;i<switchId.length;i++){
      var switchItem = document.getElementById(switchId[i]);
      switchItem.className="";
      if(this.id == switchId[i]){
        document.getElementById(targetAreaId).style.fontSize = fontSize[i];
      }
    }
    this.className ="active";

    yomotsuSwitchFontSize.setCookie(this.id);
  },

  setCookie: function(data) {
    var today = new Date();
    today.setTime(today.getTime() + (1000 * 60 * 60 * 24 * Number(this.conf.cookieLimit)));
    document.cookie = this.conf.cookieName + '=' + encodeURIComponent(data) + '; path=/; expires=' + today.toGMTString();
  },

  getCookie: function(m) {
    return (m = ('; ' + document.cookie + ';').match('; ' + this.conf.cookieName + '=(.*?);')) ? decodeURIComponent(m[1]) : null;
  },

  addEvent : function(){
    if(window.addEventListener) {
      window.addEventListener("load", this.main, false);
    }
    else if(window.attachEvent) {
      window.attachEvent("onload", this.main);
    }
  }

}

yomotsuSwitchFontSize.addEvent();

