// 現在の文字の大きさ global変数
currentFontSize = getFontSize();
// 文字サイズ上限 global変数
fontSizeMax = 200;
// 文字サイズ下限 global変数
fontSizeMin = 50;
// 文字サイズクッキー保存日数 global変数
fontSizeCookieExpire = 30;


// 文字変更用のHTML作成
function fontSizeHtml(){
	if(!document.getElementById("linkHomePage")){
		return;
	}
	//var rooturl  = document.getElementById("linkHomePage").href;
	var rooturl = getrooturl();
	var html  = '<p class="fontsize">';
	    html += '<img src="'+rooturl+'img/fontsize/font.png" width="107" height="18" alt="文字サイズ" />';
	    html += '<a href="#" onClick="changeFontSize(80);return false;" id="fontSizeSmall"><img src="'+rooturl+'img/fontsize/small.png" width="32" height="18" alt="文字を小さく" /></a>';
	    html += '<a href="#" onClick="changeFontSize(0);return false;"><img src="'+rooturl+'img/fontsize/middle.png" width="32" height="18" alt="標準" /></a>';
	    html += '<a href="#" onClick="changeFontSize(125);return false;" id="fontSizeLarge"><img src="'+rooturl+'img/fontsize/large.png" width="32" height="18" alt="文字を大きく" /></a>';
	    html += '</p>';
	
	//document.getElementById(elmID).innerHTML = html;
	document.write(html);
	setFontSize();
}

// 現在の文字サイズ
function getFontSize(){
	if(!document.getElementById){
		return 100;
	}
	var cookieFontSize = getCookie("fontSize");
	if (cookieFontSize) {
		//クッキーがあれば現在の値をクッキーの値に設定
		cookieFontSize = Number(cookieFontSize);
	}else{
		//クッキーが無ければ現在の値を初期状態の値に設定
		cookieFontSize = 100;
	}
	return (cookieFontSize);
}

// bodyのstyle.fontSizeを書き換える
function setFontSize(){
	if(document.getElementById){
		document.body.style.fontSize = currentFontSize + "%";
	}
}

//フォントサイズ変更（%）
//changeFontSize(125);現在より25%up
//changeFontSize(0); デフォルトに戻す
function changeFontSize(val){
	if(!document.getElementById){
		return null;
	}
	if(val > 0){
		var newFontSize = Number( currentFontSize * val / 100 );
		// 文字サイズ上限下限
		if(newFontSize < fontSizeMin || newFontSize > fontSizeMax){
			newFontSize = currentFontSize;
		}
		setCookie( "fontSize" , newFontSize , "/" , fontSizeCookieExpire );//クッキー書き込み
	// 元に戻す：操作後の値を初期値にする、クッキー削除
	}else{
		var newFontSize = 100;
		setCookie( "fontSize" , 100 , "/" ,fontSizeCookieExpire );//クッキー書き込み
		//deleteCookie( "fontSize" ,"/");
	}
	currentFontSize = newFontSize;
	setFontSize();
}

//クッキー関連ここから
// setCookie(key,val,path,days)
// 基本的に path は "/" でOK
// days は、31[日]とか
function setCookie(key,val,path,days){
	var newDate = new Date();
	newDate.setTime(newDate.getTime() + days * 24 * 60 * 60* 1000);
	var expiresDate = newDate.toGMTString();
	document.cookie = key + '=' + escape(val)+ ';expires=' + expiresDate + ';path=' + path;
}

//クッキー：keyから値を取り出す
function getCookie(key){
	var cookieStr = document.cookie+";";
	var strMatch = cookieStr.indexOf(key,0);
	if(strMatch == -1){
		return null;
	}
	var str = cookieStr.substring(strMatch,cookieStr.length);
	var fromStrlength = str.indexOf("=", 0);
	var toStrlength   = str.indexOf(";", fromStrlength);
	var retStr = str.substring(fromStrlength + 1, toStrlength);
	return(unescape(retStr));
}

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

