// サイトのフォントサイズを調整するメニューを出すスクリプト
// 参考：del.icio.usのJSONなど←とくにdocument.createXxxあたりのDOM的操作

// version:	0.14 2006-10-16
// toDos:	cookieを設定/解除したときのメッセージを表示する（「文字サイズを設定しました」など。赤文字が良いかな←ただしCSSでやれば十分）
// toDos:	あるタイミングでcookieの期限を延長したほうがいいと思う。各アクセス時に更新？cookie期限が1週間を切ったら更新？
// note:	操作メニュー設置先をdiv#bannerの次とかにしたい（各ページにdiv#text-size-adjustingを埋め込むのは面倒）。ただそのためのJavaScriptDOM操作がよく分からん。insertBeforeがFirefoxでエラーを出すし…（第二引数が必要だった気がする…？）
// toDos:	出力はテンプレート形式（外部ファイルを読み込み、値を代入する）にできないか？

//---- Configurable Variables --------
var sizeAdjustMenuId = "txtSizeAdjustMenu"; // メニューpタグのID（空欄可）
var sizeAdjustButtonCommonClassName = "txtSizeAdjustButton";
	// 他にはメニュー全体をul、ボタンをliとかもできるが
	// 今のところinputボタンを入れてるので無難にp, spanにしておく

var menus = {
	"initialize" : {
		"txt": "［解除］",
		"popUpTxt": "設定を解除",
		"id": "txtSizeIntBtn",
		"elementType": "span"
	}, "enlarge" : {
		"ratio": 1.05,
		"txt": "［拡大］",
		"popUpTxt": "文字を拡大",
		"id": "txtSizeEnlBtn",
		"elementType": "span"
	}, "reduce" : {
		"ratio": 0.95,
		"txt": "［縮小］",
		"popUpTxt": "文字を縮小",
		"id": "txtSizeRedBtn",
		"elementType": "span"
	}
}; // ↑拡張性のために最後にカンマを入れるとIEでエラーになるらしい

var buttonOutputOrder = [
	"enlarge",
	"reduce",
	"initialize"
]

//var menuAppendTargetId = "text-size-adjusting";
var menuAppendTargetId = "head-tools";

// スタイル指定もこのスクリプト内に組み込もうとしたが
// CSSで十分可能なので放棄
//------------------------------------


//---- variables definition ----------
defaultSize = CookieManager.getCookie("txtSize",100);
defaultSize = (defaultSize+"").replace(/(%)|(%25)$/, "") - 0;
//------------------------------------

if (menuAppendTargetId) {

var otherFuncsOnLoad = window.onload;
window.onload = function() {
// イベントハンドラは全部小文字で書く！onLoadではない...orz

	if (otherFuncsOnLoad) { otherFuncsOnLoad(); }
	
	document.getElementsByTagName("body")[0].style.fontSize = defaultSize + "%";
	
	var adjustMenu = document.createElement("p");
	
	adjustMenu.appendChild(document.createTextNode("文字サイズ調整："));
	
	for (var buttonOutputIndex in buttonOutputOrder) {
		var behaviorType = buttonOutputOrder[buttonOutputIndex];
		
		var button = document.createElement(menus[behaviorType].elementType);
		button.behaviorType = behaviorType;
			// ↑button要素の中に動作タイプ（拡大/縮小など）を保存しておく
			// そうでないとonclick時の動作を規定しづらくなる
		button.appendChild(document.createTextNode(menus[behaviorType].txt));
		
		if (menus[behaviorType].ratio) {
			button.onclick = function () {
				var txtSize = document.getElementsByTagName("body")[0].style.fontSize;
				txtSize = (txtSize+"").match(/\d*/) - 0;
				if (!txtSize) { return void(0); }
					// ↑数字が抽出できなかった時のため。ほかにいい方法はあるか？
				txtSize = Math.round(txtSize * menus[this.behaviorType].ratio) + "%";
					// 倍率をtype.ratioとしていたが、変数typeはforループ中にどんどん書き換えられていて、
					// 実行時（onclick時）には別の値になってしまった。button.behaviorTypeを設定することで回避。
				document.getElementsByTagName("body")[0].style.fontSize = txtSize;
				
				txtSize = (txtSize+"").replace(/(%)|(%25)$/, "") - 0;
				CookieManager.setCookie("txtSize", txtSize);
			}
		} else if (behaviorType == "set") {
			button.onclick = function () {
				CookieManager.setCookie("txtSize", document.getElementsByTagName("body")[0].style.fontSize.match(/\d+/));
			}
		} else if (behaviorType == "reset") {
			button.onclick = function () {
				document.getElementsByTagName("body")[0].style.fontSize = CookieManager.getCookie("txtSize",100) + "%";
			}
		} else if (behaviorType == "initialize") {
			button.onclick = function () {
				CookieManager.deleteCookie("txtSize");
				document.getElementsByTagName("body")[0].style.fontSize = "100%";
			}
		}
		
		if (menus[behaviorType].popUpTxt) { button.title = menus[behaviorType].popUpTxt; }
		if (menus[behaviorType].id) { button.id = menus[behaviorType].id; }
		if (menus[behaviorType].className) { button.className = menus[behaviorType].className; }
		if (sizeAdjustButtonCommonClassName) { button.className = sizeAdjustButtonCommonClassName; }
		
		adjustMenu.appendChild(button);
	} // end of for loop
	
	temp = document.createElement("div");
	if (sizeAdjustMenuId) { temp.setAttribute("id", sizeAdjustMenuId); }
	temp.appendChild(adjustMenu);
	
	if (document.getElementById(menuAppendTargetId)) {
		document.getElementById(menuAppendTargetId).appendChild(temp);
	}
} // function for window.onload
} // brace for if condition

