//
// 折りたたみメニュー4(メニューが2桁以上の場合にも対応)
// http://www5e.biglobe.ne.jp/~purest/を参照
//

// (Cookieキーワードはファイル名)


// ファイル名の取得
function getFileName(){
	path = window.location.href;
	fileName = path.substring(path.lastIndexOf('/',path.length)+1,path.length);
	if(fileName == ""){
		fileName = "index.html";
	}
	fileName = fileName.substring(0,fileName.indexOf(".")); //拡張子を取り除く
	return fileName;
}

// cookie読み出し
function getCookie(){
	fileName = getFileName();
	tmp = document.cookie + ";";
	index1 = tmp.indexOf(fileName,0);
	if(index1 != -1){
		tmp = tmp.substring(index1,tmp.length);
		index2 = tmp.indexOf("=",0) + 1;
		index3 = tmp.indexOf(";",index2);
		return(unescape(tmp.substring(index2,index3)));
	}
	else return ("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
				//---------1---------2---------3---------4---------5---------6---------7---------8---------9---------0
				// IDの数だけ0を増やす(とりあえず1ページに100個))
}

// cookie書き込み
function setCookie(val){
	fileName = getFileName();
	myExp = new Date();
	myExp.setTime(myExp.getTime()+(365*24*60*60*1000));
	tmp = fileName + "=" + escape(val) + ";";
	tmp += "expires=" + myExp.toGMTString();
	document.cookie = tmp;
}


// 折りたたみ制御 ー IDの値はfoldItem_n(nは0から始める)
function foldCtrl(id){
	foldStatus = getCookie();
	cli = id.substring(9);	//クリックされたidの番号を取得、2桁以上に対応するため第2引数を省略
	hai = foldStatus.match(/[01]/g);	//0か1にmatchさせて配列にする
	obj=(document.all)?document.all(id):((document.getElementById)?document.getElementById(id):null);
	if(obj) obj.style.display=(hai[cli]=="0")?"block":"none";
	hai[cli] = "" + ((eval(hai[cli]))^1);	//クリックされた部分の0か1を反転
	foldStatus = "";	//いったん空にする
	for(i=0;i<hai.length;i++) foldStatus += hai[i];	//foldStatusに再び0と1の状態を格納
	setCookie(foldStatus);
}


function init(){
	foldStatus = getCookie();
	hai = foldStatus.match(/[01]/g);

	for(i=0;i<hai.length;i++){
		obj=(document.all)?document.all('foldItem_'+i):((document.getElementById)?document.getElementById('foldItem_'+i):null);
		if(obj)	obj.style.display=(hai[i]=="0")?"none":"block";
	}
}
