/*
===========================================================
文字サイズ変更スクリプト
===========================================================
*/

// 文字サイズ変更機能の使用有無を設定（ダブルクオートやクオートで括らない）
// ※必ず小文字で設定して下さい。（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','font','span','h1','h2','h3','h4','h5','h6');



// ========== ::: ページ読み込み時の値を設定 ::: ==========

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(!useFontChange) return;
  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++) {
    	/*----------------------
    	 【タグ別】倍率の調整
    	 ----------------------- */
    	//<font>タグ専用処理
    	if (tags_disp[value] == "font") {
    		//<font size="xx">への対応
    		switch(cTags[su].size){
    		case "-2":
    			cTags[su].style.fontSize = currentSize - (currentSize * 0.4) + fontSizeUnit;
    			break;
    		case "-1":
    			cTags[su].style.fontSize = currentSize - (currentSize * 0.2) + fontSizeUnit;
    			break;
    		case "+1":
    			cTags[su].style.fontSize = currentSize + (currentSize * 0.2) + fontSizeUnit;
    			break;
    		case "+2":
    			cTags[su].style.fontSize = currentSize + (currentSize * 0.5) + fontSizeUnit;
    			break;
    		case "+3":
    			cTags[su].style.fontSize = currentSize + (currentSize * 0.8) + fontSizeUnit;
    			break;
    		case "+4":
    			cTags[su].style.fontSize = currentSize + (currentSize * 2.0) + fontSizeUnit;
    			break;
    		default:
    			cTags[su].style.fontSize = currentSize + fontSizeUnit;
    			break;
    		}
    	}
    	//<h1>タグ専用処理
    	else if (tags_disp[value] == "h1"){
    		cTags[su].style.fontSize = currentSize + (currentSize * 0.8) + fontSizeUnit;
    	}
    	//<h2>タグ専用処理
    	else if (tags_disp[value] == "h2"){
    		cTags[su].style.fontSize = currentSize + (currentSize * 0.5) + fontSizeUnit;
    	}
    	//<h3>タグ専用処理
    	else if (tags_disp[value] == "h3"){
    		cTags[su].style.fontSize = currentSize + (currentSize * 0.2) + fontSizeUnit;
    	}
    	//<h5>タグ専用処理
    	else if (tags_disp[value] == "h5"){
    		cTags[su].style.fontSize = currentSize - (currentSize * 0.2) + fontSizeUnit;
    	}
    	//<h6>タグ専用処理
    	else if (tags_disp[value] == "h6"){
    		cTags[su].style.fontSize = currentSize - (currentSize * 0.5) + fontSizeUnit;
    	}
    	//その他、配列tags_disp()にて指定したタグの処理【通常倍率】
    	else {
    		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
