/* ******************************************************
	フォントサイズ変更用スクリプト(fontSizeChange.js)
********************************************************* */

/* *** ▼初期設定 *** */

//フォントサイズ初期設定（%）
var defaultFontSize = 100;

//ブラウザ判別
var aVer = navigator.appVersion;
var aName = navigator.appName;
var uAgent = navigator.userAgent;

//MACの場合
if(aVer.indexOf("Mac") > -1)
{
	//Safariの場合
	if(uAgent.indexOf("Safari") > -1){
		defaultFontSize = 95;
	}
	
	//Firefoxの場合
	if (uAgent.indexOf("Firefox") > -1){
		defaultFontSize = 110;
	}
}

//フォントサイズ（単位）の設定
var unit = "%";

//1回の変更で増減する値（%）
var diff = 20;

//クッキー名称
var cookieName = "JAMSTEC_FONT_SIZE";

//クッキーの有効期限（日数）
var cookieExpiresDay = 1;

/* *** ▲初期設定 *** */
	
/* *** ▼読み込み時の設定 *** */

//クッキー取得
var ck = readCookie(cookieName);

//クッキーなし
if(ck == null){
  currentFontSize = defaultFontSize;
//クッキーあり
}else{
  currentFontSize = Number(ck);
}

document.writeln( '<style type="text/css" media="all">' );
document.write( 'body{font-size:' + currentFontSize + unit+ '}' );
document.writeln( '</style>' );

/* *** ▲読み込み時の値設定 *** */

/* ============================================
  function: setCookie
  クッキーをセット
=============================================== */

function setCookie(name,value){
  var sday = new Date();
  sday.setTime(sday.getTime() + 24 * 60 * 60 * cookieExpiresDay * 1000);
  var s2day = sday.toGMTString();
  //クッキーをセット
  document.cookie = name + '=' + escape(value)+ ';expires=' + s2day + ';path=/';
}


/* ============================================
  function: readCookie
  クッキーを読み込む
=============================================== */
	
function readCookie(name){
  var arg  = name + "=";
  var argLen = arg.length;
  var cookieLen = document.cookie.length;
  var i = 0;
  while (i < cookieLen){
    var j = i + argLen;
    if (document.cookie.substring(i, j) == arg){
    	var end = document.cookie.indexOf (";", j);
  		if(end == -1)
  		end = document.cookie.length;
  		return unescape(document.cookie.substring(j,end));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
   return null;
}

/* ============================================
  function: delCookie
  クッキーを削除する
=============================================== */
	
function delCookie(name){
  if (readCookie(name)) {
    document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/';
  }
}

/* ============================================
  function: fontSizeChange
  bodyのfontSizeをcurrentFontSizeに設定
=============================================== */

function fontSizeChange(){
  	//bodyのfontSizeを変更
    document.body.style.fontSize = currentFontSize + unit;
}


/* ============================================
  function: fontSizeSetCookie
  引数("larger" or "smaller")
  変更後の値をクッキーに書き込む
=============================================== */

function fontSizeSetCookie(mode){

  //フォントサイズ拡大
  if(mode == "larger"){
    var newFontSize = Number(currentFontSize + diff);
    setCookie(cookieName,newFontSize);
  }

  //フォントサイズ縮小
  if( mode == "smaller" ){
    if (currentFontSize != diff){
      var newFontSize = Number(currentFontSize - diff);
      setCookie(cookieName,newFontSize);
    }else{
      var newFontSize = Number(currentFontSize);
    }
  }

  //フォントサイズを初期値に戻す
  if( mode == "default" ){
    var newFontSize = defaultFontSize;
    //クッキー削除
    delCookie(cookieName);
  }


  //現在のフォントサイズを変更後の値に設定
  currentFontSize = newFontSize;

  //bodyのfontSizeを変更
  fontSizeChange();

}