// 現在の文字サイズをクッキーから取得する
var SIZE = getCookie("seino_fontsize");
if (SIZE == false) {
	SIZE = getCookie("seino_fontsize_cgi");
}

// 文字サイズの値を連想配列に格納する
var SIZE_LIST = new Array();
SIZE_LIST["S"] = "80%";
SIZE_LIST["M"] = "100%";
SIZE_LIST["L"] = "120%";

// 現在の文字サイズアイコンを強調
function changeIcon(size) {
	if (size == "M") {
		document.getElementById("font-small").style.backgroundPosition = "0 0";
		document.getElementById("font-medium").style.backgroundPosition = "-17px -17px";
		document.getElementById("font-large").style.backgroundPosition = "-34px 0";
	} else if (size == "L") {
		document.getElementById("font-small").style.backgroundPosition = "0 0";
		document.getElementById("font-medium").style.backgroundPosition = "-17px 0";
		document.getElementById("font-large").style.backgroundPosition = "-34px -17px";
	}
	else {
		document.getElementById("font-small").style.backgroundPosition = "0 -17px";
		document.getElementById("font-medium").style.backgroundPosition = "-17px 0";
		document.getElementById("font-large").style.backgroundPosition = "-34px 0";
	} 
}

// 文字サイズアイコンを初期化
function initFontSizeIcon() {
	
	if (document.getElementById("util-menu")) {
		
		// アイコンにイベントリスナーを追加
		if (window.addEventListener) {
			document.getElementById("font-small").addEventListener("click", function() { changeFontSize("S"); }, false);
			document.getElementById("font-medium").addEventListener("click", function() { changeFontSize("M"); }, false);
			document.getElementById("font-large").addEventListener("click", function() { changeFontSize("L"); }, false);
		} else if (window.attachEvent) {
			document.getElementById("font-small").attachEvent("onclick", function() { changeFontSize("S"); });
			document.getElementById("font-medium").attachEvent("onclick", function() { changeFontSize("M"); });
			document.getElementById("font-large").attachEvent("onclick", function() { changeFontSize("L"); });
		}
		changeIcon(SIZE);
	}
}

// 文字サイズを変更する
function changeFontSize(size) {
	// スタイル変更
	document.getElementsByTagName('body')[0].style.fontSize = SIZE_LIST[size];
	// クッキー書き込み
	setCookie("seino_fontsize", size, "/seino", 365);
	setCookie("seino_fontsize_cgi", size, "/cgi-bin/seino", 365);
	// アイコン変更
	changeIcon(size);
}

// ウィンドウロード時の処理を設定する
if (window.addEventListener) {
	window.addEventListener("load", initFontSizeIcon, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", initFontSizeIcon);
}

// クッキーから取得した文字サイズをページに適用する
if (SIZE != false) {
	document.write('<style type="text/css">body { font-size: ' + SIZE_LIST[SIZE] + '; }</style>');
} else {
	document.write('<style type="text/css">body { font-size: ' + SIZE_LIST["S"] + '; }</style>');
}
