/*
===========================================================
文字サイズ変更スクリプト
===========================================================
*/

// ========== ::: 初期設定 ::: ==========

// 文字サイズ変更機能の使用有無を設定（ダブルクオートやクオートで括らない）
// ※必ず小文字で設定して下さい。（true=利用する、false=利用しない）
var useFontChange = true;

/*
============================================================
注：システム制御情報のため以降記述は安易に修正しない事
============================================================
*/

// 一回の操作で変化させる値を設定（ダブルクオートやクオートで括らない）
var perOrder = 10;

// 値の単位を設定（必ずダブルクオートかクオートで括る）
var fontSizeUnit = "%";

// 初期状態の値を設定（ダブルクオートやクオートで括らない）
var defaultSize = 100;

// 最大値を設定（ダブルクオートやクオートで括らない）
var maxSize = 200;

// クッキーの名前（必ずダブルクオートかクオートで括る）
var ckName = "SerlsFontSize";

// クッキーの有効期限（日）（ダブルクオートやクオートで括らない）
// 0を指定した場合、ブラウザ終了時に初期化する
var ckDays = 0;

// クッキーのパス（必ずダブルクオートかクオートで括る。指定が不要の場合は"/"にする）
var ckPath = "/"

// フォントサイズを変更するタグ（表示系）
var tags_disp    = new Array('div','td','tr','th','caption','span');

// フォントサイズを変更するタグ（入力系）
var tags_inp   = new Array('input','select','textarea');



// ========== ::: ページ読み込み時の値を設定 ::: ==========

var currentSize ;
// クッキー読み出し
var ckSize = GetCookie(ckName);
if ( ckSize != null && !isNaN(ckSize) ) {
  //クッキーがあれば現在の値をクッキーの値に設定
  currentSize = parseInt(ckSize);
} else {
  //クッキーが無ければデフォルトの値を初期状態の値に設定
  currentSize = defaultSize;
}



//*************************************************
// 文字サイズ拡大
//*************************************************
function FontLarge(target) {
  // currentSizeがmaxSize以上は無効
  if( currentSize + perOrder > maxSize ) return;

  FontChange(target, 0 + perOrder);
}

//*************************************************
// 文字サイズ縮小関数
//*************************************************
function FontSmall(target) {
  // currentSizeが0以下は無効
  if( currentSize - perOrder <= 0 ) return;
  FontChange(target, 0 - perOrder);
}

//*************************************************
// 文字サイズ標準関数
//*************************************************
function FontDefault(target) {
  FontChange(target, null);
}

//*************************************************
// 文字サイズ変更関数
//*************************************************
function FontChange(target, size) {
  if (!document.getElementById) return;
  var dore = document,tarS = null,value,su,cTags;

  if( size == null){
    // null指定の場合、初期値にリセット
    currentSize = defaultSize;
  } else {
    // sizeをプラス
    currentSize = currentSize + size;
  }
  if(document.getElementById("fontchange") != null){
    if(useFontChange == true){
      // フォントサイズ変更機能が有効の場合、ボタン表示
      document.getElementById("fontchange").style.visibility='visible';
    } else {
      // フォントサイズ変更機能が有効の場合、ボタン非表示、フォントサイズデフォルト
      document.getElementById("fontchange").style.visibility='hidden';
      currentSize = defaultSize;
    }
  }

  // Cookieにフォントサイズをセット
  SetCookie(ckName, currentSize);

  if (!(tarS = dore.getElementById(target))) tarS = dore.getElementsByTagName(target)[0];
  tarS.style.fontSize = currentSize + fontSizeUnit;

  // 表示系タグ
  for (value = 0 ; value < tags_disp.length ; value++) {
    cTags = tarS.getElementsByTagName(tags_disp[value]);
    for (su = 0 ; su < cTags.length ; su++) cTags[su].style.fontSize = currentSize + fontSizeUnit;
  }

  // 入力系タグはテキスト表示系よりも大きくなる傾向があるため、若干小さめにする
  for (value = 0 ; value < tags_inp.length ; value++) {
    cTags = tarS.getElementsByTagName(tags_inp[value]);
    for (su = 0 ; su < cTags.length ; su++) cTags[su].style.fontSize = currentSize - (currentSize * 0.2) + fontSizeUnit;
  }

}


//*************************************************
// クッキーに値を書き込む
//*************************************************
function SetCookie( name , value ){
  var dobj = new Date();
  var expiryDate = "";
  // クッキーの有効期限が0以上の場合
  if( ckDays > 0){
    dobj.setTime(dobj.getTime() + 24 * 60 * 60 * ckDays * 1000);
    expiryDate = ';expires=' + dobj.toGMTString();
  }
  document.cookie = name + '=' + escape(value) + expiryDate + ';path=' + ckPath;
}

//*************************************************
// クッキーを取得する
//*************************************************
function GetCookie (name){
  var arg  = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen){
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

//*************************************************
// クッキーの値を抽出する
//*************************************************
function getCookieVal (offset){
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset,endstr));
}

//*************************************************
// クッキーを削除する
//*************************************************
function DeleteCookie(name){
  if (GetCookie(name)){
    document.cookie = name + '=' +
    '; expires=Thu, 01-Jan-70 00:00:01 GMT;path='+ckPath;
  }
}

//EOF