//スタイルシート切替メニューの初期化
function installStyleMenu() {
	if (!document.getElementById) return;	//DOM非対応ブラウザには書き出さない
	
	//cookieから保存済みのスタイルシート名を取得
	savedStyle = readCookie("myStyle");

	//デフォルトのスタイルシートとして適用
	if (savedStyle != null) {
		if (document.all) setStyleSheet(savedStyle, 0);
		else window.onload = new Function("setStyleSheet(savedStyle, 0)");
	}

	//選択メニュー書き出し
	document.write("<div id='stylemenu'>");
	document.write("文字サイズの変更：<select onchange='setStyleSheet(this.options[this.selectedIndex].value,1)'>");
	
	//固定スタイルシートまたは推奨スタイルシート（基本ページスタイル）
	found = 0;
	for(i=0; (linkTag = document.getElementsByTagName("link")[i]); i++) {
		if(linkTag.getAttribute("rel") == "stylesheet") found++;
	}
	if (found) {
		selectedVal = (savedStyle == "_") ? " selected":  "";
		//edit comment out
		//document.write("<option value='_'" + selectedVal + ">基本サイズ<"+"/option>");
		//edit end
	}
	
	//代替スタイルシート
	for(i=0; (linkTag = document.getElementsByTagName("link")[i]); i++) {
		if(linkTag.getAttribute("rel").indexOf("stylesheet") > -1 && linkTag.getAttribute("title")) {
			t = linkTag.getAttribute("title");
			selectedVal = (t == savedStyle || (savedStyle == null && linkTag.getAttribute("rel") == "stylesheet")) ? " selected":  "";
			if(t) {
				document.write("<option value='" + t + "'" + selectedVal + ">" + t + "<"+"/option>");
			}
		}
	}

	document.write("<"+"/select>");
	document.write("<"+"/div>");
}

//スタイルシートの切替処理
function setStyleSheet(titleNam, saveFlag) {
	//disabledプロパティの切替
	for(i=0; (temp = document.getElementsByTagName("link")[i]); i++) {
		if(temp.getAttribute("rel").indexOf("stylesheet") > -1 && temp.getAttribute("title")) {
			temp.disabled = true;
			if(temp.getAttribute("title") == titleNam) temp.disabled = false;
		}
	}
	//cookieの保存
	if (saveFlag == 1) {
		saveCookie("myStyle", titleNam, 365);
	}

}

//cookieへの書き込み処理
function saveCookie(nam,val,expDays) {
	if (expDays) {
		//cookieの保存日数の設定
		expDate = new Date();
		expDate.setTime(expDate.getTime()+(expDays*24*60*60*1000));
		exp = "; expires=" + expDate.toGMTString();
	}
	else exp = "";
	document.cookie = nam + "=" + val + exp;
}

//cookieからの読み込み処理
function readCookie(nam) {
	valNam = nam + "=";
	cookies = document.cookie.split(';');	//クッキーを「;」で切り分けて配列化する

	for(i=0; i<cookies.length; i++) {
		c = cookies[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);	//1文字目が空白文字だった場合それを取り除いておく
		if (c.indexOf(valNam) == 0) return c.substring(valNam.length,c.length);	//指定したクッキー名が見つかった場合その値を返す
	}
	return null;	//指定したクッキーが無い場合nullを返す
}

