/*
===========================================================
文字サイズ変更スクリプト
update 2010/6/10
===========================================================
*/

// 一回の操作で変化させる値を設定（ダブルクオートやクオートで括らない）
var perOrder = 5;

// 値の単位を設定（必ずダブルクオートかクオートで括る）
var fontSizeUnit = "%";

// 初期状態の値を設定（ダブルクオートやクオートで括らない）
var defaultSize = 100;

// クッキーの名前（必ずダブルクオートかクオートで括る）
var ckName = "SerlsFontSize";

// クッキーの有効期限（日）（ダブルクオートやクオートで括らない）
// 0を指定した場合、ブラウザ終了時に初期化する
var ckDays = 0;

// クッキーのパス（必ずダブルクオートかクオートで括る。指定が不要の場合は"/"にする）
var ckPath = "/"

// フォントサイズを変更するタグ
var tags_disp    = new Array('div','td','tr','th','font','span','h1','h2','h3','h4','h5','h6');


// ========== ::: ページ読み込み時の値を設定 ::: ==========
var currentSize ;
// クッキー読み出し
var ckSize = GetCookie(ckName);

window.onload = function() {
	if ( ckSize != null && !isNaN(ckSize) ) {
	  //クッキーがあれば現在の値をクッキーの値に設定
	  currentSize = parseInt(ckSize);
	  FontChange('body',0);
	} else {
	  //クッキーが無ければデフォルトの値を初期状態の値に設定
	  currentSize = defaultSize;
	}
}
//*************************************************
// 文字サイズ拡大
//*************************************************
function FontLarge(target) {
  FontChange(target, 0 + perOrder);
}

//*************************************************
// 文字サイズ標準関数
//*************************************************
function FontDefault(target) {
  currentSize = defaultSize;
  DeleteCookie(ckName);
  window.location.reload();
}

//*************************************************
// 文字サイズ変更関数
//*************************************************
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;
  }

  // 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;
    }
  }
}


//*************************************************
// クッキーに値を書き込む
//*************************************************
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
