var aMenuList = new Array;
var aIsOpen = new Array;
var aIsSlc = new Array;
var intMaxLv = 0;

var scnt = 2;
var stimer = "";
var xmltimer ="";

/* 
階層Lv[0], リンク先[1], 名前[2], アイコンの種類[3]（0:フォルダ/1:PDF文書）
*/
function MakeMenuListFromToc() {
	var objXML = new ActiveXObject("Microsoft.XMLDOM");
	var i=0;
	var atmp1 = new Array, atmp2 = new Array;

	objXML.async = false;
	objXML.load("./toc.xml");
	
	var objChilds = objXML.documentElement.childNodes;
	
	for (i=0; i<objChilds.length; i++) {
		if (objChilds.item(i).nodeName=="toctree") {
			var objTec = objChilds.item(i);
		}
	}
	
	var objChilds = objTec.childNodes;
	for (i=0; i<objChilds.length; i++) {
		var sLv = objChilds.item(i).childNodes.item(0).text;
		/*
		Not First:Lv -1
		*/
		if (i>0&&parseInt(sLv)>0) {
			sLv = parseInt(sLv) - 1;
		}
		var sLink = objChilds.item(i).childNodes.item(1).text;
		var sTitle = objChilds.item(i).childNodes.item(2).text;
		aMenuList[i] = sLv+"<>"+sLink+"<>"+sTitle;
	}
	
	for (i=0; i<aMenuList.length -1; i++) {
		atmp1 = aMenuList[i].split("<>");
		var sIcon = "1";
		atmp2 = aMenuList[i+1].split("<>");
		if (atmp1[0]<atmp2[0]) {
			sIcon = "0";
		}
		aMenuList[i] = aMenuList[i]+"<>"+sIcon;
	}
	aMenuList[i] = aMenuList[i]+"<>1";
}

function iniList() {
	var i = 0;
	
	MakeMenuListFromToc();
	
	for (i = 1; i < aMenuList.length; i ++) {
		aIsOpen[i] = 0;
	}
	aIsOpen[0] = 1;
	
	for (i = 0; i < aMenuList.length; i ++) {
		aIsSlc[i] = 0;
	}

	intMaxLv = retLastLv(aMenuList);
}

function makeBody() {
	var doc = tree.window.document;
	var sBody = "";

	if (document.layers) {
		var iscroll = tree.window.pageYOffset;
	} else {
		var iscroll = tree.document.body.scrollTop;
	}

	doc.open();

	sBody = sBody + "<body leftmargin=\"5\" topmargin=\"10px\" marginheight=\"0\" marginwidth=\"0\"><input type=\"hidden\" id=\"slcanc\">";
	sBody = sBody + "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100\"><tr><td>";
	doc.write(sBody);

	cMenu(doc);

	sBody = "</td></tr></table>";
	sBody = sBody + "</body>";

	sBody = sBody + "</html>";

	doc.write(sBody);
	doc.close();
	
	if (document.layers) {
		tree.scrollTo(0,iscroll);
	} else {
		tree.document.body.scrollTop = iscroll;
	}
}

function cMenu(doc) {
	var lastNodeNum = aMenuList.length - 1;
	var aTailNodeLv = new Array
	var anow = new Array;
	var atmp = new Array;
	var i = 0;
	var j = 0;
	var sMenu = "" ;
	var isTailNodeLv = intMaxLv;
	var isCloseLv = (intMaxLv * 1) + 1;
	var isNextDrop = 0;

	for (i = 0; i <= lastNodeNum; i ++) {
		isVLine = 0;
		isNextDrop = 0;
		anow = aMenuList[i].split("<>");

		if (i < lastNodeNum && anow[3]=="0") {
			atmp = aMenuList[i+1].split("<>");
			if (anow[0] == atmp[0] - 1) {
				isNextDrop = 1;
			}
		}

		sMenu = sMenu + "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";

		isTailNode = retIsTailNode(aMenuList, i);

		if (isCloseLv >= anow[0]) {
			if (aIsOpen[i]==0) {
				isCloseLv = anow[0];
			} else {
				isCloseLv =  (intMaxLv * 1) + 1;
			}
		}
		if (isCloseLv >= anow[0]) {
			for (j = 0; j <= anow[0]; j ++) {

				if (j == 0) {
					sMenu = sMenu + "<tr>";
				}

				sMenu = sMenu + makeEntry(anow, i, j, isTailNode, aTailNodeLv, isCloseLv, aIsSlc[i], isNextDrop);
				
				if (j == anow[0]) {
					sMenu = sMenu + "</tr>";
				}
			}
		}

		if (isTailNode==1 && i > 0) {
			aTailNodeLv[anow[0]-1] = 1;
		} else {
			aTailNodeLv[anow[0]-1] = 0;
		}
		
		sMenu = sMenu + "</table>";
	}
	doc.write(sMenu);
}

/*
（現在行の配列, 行, 列, このレベルで最後の要素かどうか, 前回出現した最後の要素のLv,  閉じているレベル, 選択しているリンクかどうか, 次の行がひとつ落ちているかどうか）
*/
function makeEntry(anow, i, j, isTailNode, aTailNodeLv, isCloseLv, isSlc, isNextDrop) {
	var sEntry = "";
	var sPic = "";

	if (j == anow[0]) {
		if (anow[3] == "0") {
		/*
		type:folder
		*/
			if (isCloseLv==anow[0]) {
				sPic1 = "./treeimage/closedfolder.gif";
				if (isNextDrop) {
					sPic2 = "./treeimage/plus.gif";
				} else {
					sPic2 = "./treeimage/blank.gif";
				}
			} else {
				sPic1 = "./treeimage/openfolder.gif";
				if (isNextDrop) {
					sPic2 = "./treeimage/minus.gif";
				} else {
					sPic2 = "./treeimage/blank.gif";
				}
			}

			sEntry = sEntry + "<td valign=\"middle\" nowrap>";
			sEntry = sEntry + "<span onClick=\"javascript:parent.closeNode("+i+")\" style=\"cursor:hand\">";
			sEntry = sEntry + "<img src=\""+sPic2+"\" border=\"0\" width=\"10\" height=\"10\">";
			sEntry = sEntry + "</span>";
			sEntry = sEntry + "</td>";

			sEntry = sEntry + "<td valign=\"middle\" nowrap>";
			if (isNextDrop) {
				sEntry = sEntry + "<span onClick=\"javascript:parent.closeNode("+i+")\" style=\"cursor:hand\">";
			}
			sEntry = sEntry + "<img src=\""+sPic1+"\" border=\"0\" width=\"20\" height=\"20\">";
			if (isNextDrop) {
				sEntry = sEntry + "</span>";
			}
			sEntry = sEntry + "</td>";

			if (isSlc) {
				sStyle = "color:#FFFFFF;background-color:#0000FF;cursor:hand";
				sScript = "onMouseover=\"this.style.backgroundColor='#0000FF';\" onMouseout=\"this.style.backgroundColor='#0000FF'\"";
			} else {
				sStyle = "color:#000000;background-color:transparent;cursor:hand";
				sScript = "onMouseover=\"this.style.backgroundColor='#bbbbbb';\" onMouseout=\"this.style.backgroundColor='#FFFFFF'\"";
			}
			sEntry = sEntry + "<td valign=\"middle\" align=\"left\" nowrap>";
			sEntry = sEntry + "<span onClick=\"parent.slctLink("+i+",'"+anow[1]+"')\" Style=\""+sStyle+"\" "+sScript+">";
			sEntry = sEntry + "<font size=\"-1\" face=\"Arial, Helvetica\">" + anow[2] + "</font>";
			sEntry = sEntry + "</span>";
			sEntry = sEntry + "</td>";
		} else if (anow[3] == "1") {
			/*
			type:doc
			*/
			
			if (anow[0] == "0") {
				sPic = "./treeimage/blank.gif";
			} else {
				sPic = "./treeimage/horiline.gif";
			}
			sEntry = sEntry + "<td valign=\"middle\" nowrap>";
			sEntry = sEntry + "<img src=\""+sPic+"\" border=\"0\" width=\"12\" height=\"22\">";
			sEntry = sEntry + "</td>";
			
			
			if (isSlc) {
				sPic = "./treeimage/doc.gif";
				sStyle = "color:#FFFFFF;background-color:#0000FF;cursor:hand";
				sScript = "onMouseover=\"this.style.backgroundColor='#0000FF';\" onMouseout=\"this.style.backgroundColor='#0000FF'\"";
			} else {
				sPic = "./treeimage/doc.gif";
				sStyle = "color:#000000;background-color:transparent;cursor:hand";
				sScript = "onMouseover=\"this.style.backgroundColor='#bbbbbb';\" onMouseout=\"this.style.backgroundColor='#FFFFFF'\"";
			} 

			sEntry = sEntry + "<td valign=\"middle\" nowrap>";
			sEntry = sEntry + "<img src=\""+sPic+"\" border=\"0\" width=\"16\" height=\"22\">";
			sEntry = sEntry + "</td>";

			sEntry = sEntry + "<td valign=\"middle\" align=\"left\" nowrap>";
			sEntry = sEntry + "<span onClick=\"parent.slctLink("+i+",'"+anow[1]+"')\" Style=\""+sStyle+"\" "+sScript+">";
			sEntry = sEntry + "<font size=\"-1\" face=\"Arial, Helvetica\">" + anow[2] + "</font>";
			sEntry = sEntry + "</span>";
			sEntry = sEntry + "</td>";
		}
	} else {
		/*
		type:line
		*/
		if (j == anow[0] - 1) {
			if (isTailNode) {
				sEntry = sEntry + "<td valign=\"middle\" nowrap><img src=\"./treeimage/lastnode.gif\" width=\"14\" height=\"22\"></td>";
			} else {
				sEntry = sEntry + "<td valign=\"middle\" nowrap><img src=\"./treeimage/node.gif\" width=\"14\" height=\"22\"></td>";
			}
		} else {
			if (aTailNodeLv[j]==1) {
				sEntry = sEntry + "<td valign=\"middle\" nowrap><img src=\"./treeimage/blank.gif\" width=\"14\" height=\"22\"></td>";
			} else {
				sEntry = sEntry + "<td valign=\"middle\" nowrap><img src=\"./treeimage/vertline.gif\" width=\"14\" height=\"22\"></td>";
			}
		}
	}
	return sEntry;
}

function closeNode(tarIdx) {
	if (aIsOpen[tarIdx] == 1) {
		aIsOpen[tarIdx] = 0;
	} else {
		aIsOpen[tarIdx] = 1;
	}
	makeBody();
}

function slctLink(tarIdx, pdfname) {
	var sFilePath = pdfname.replace(/\#.*$/,"");
	var sAnchorName = pdfname.replace(/.*\#/,"");

	main.window.location.href = sFilePath;

	if (main.document.anchors.length) {
		main.window.location.hash = "#" + sAnchorName;
	}

	for (i = 0; i < aMenuList.length; i ++) {
		aIsSlc[i] = 0;
	}

	if (aIsSlc[tarIdx] == 1) {
		aIsSlc[tarIdx] = 0;
	} else {
		aIsSlc[tarIdx] = 1;
	}

	makeBody();

	/*
	For:SetScroll
	*/
	var slcanc = tree.document.getElementById('slcanc')
	slcanc.value=sAnchorName;
}

function retIsTailNode(ary,nowIdx) {
	var i = 0;
	var atmp = new Array;
	var ret = 1;
	atmp = ary[nowIdx].split("<>");
	str = atmp[2];
	var nowLv = atmp[0];
	for (i=nowIdx+1; i < ary.length; i++) {
		atmp = ary[i].split("<>");
		if (atmp[0]==nowLv) {
			ret = 0;
			break;
		} else if (atmp[0]<nowLv) {
			break;
		}
	}
	return ret;
}

function retLastLv(ary) {
	var i = 0;
	var nowLv = 0;
	var atmp = new Array;
	for (i=0; i<ary.length; i++) {
		atmp = ary[i].split("<>");
		if (atmp[0] > nowLv) {
			nowLv = atmp[0];
		}
	}
	return nowLv;
}

function initializeTree() {
	iniList();
	makeBody();
}

function setScroll() {
	if (parent.tree.slcanc) {
		var id = parent.tree.slcanc.value;
		var i = 0;
		if (id&&scnt>0) {
			if (parent.main.document.all(id)) {
				if (parent.main.document.all(id).offsetTop>0) {
					parent.main.document.body.scrollTop = parent.main.document.all(id).offsetTop;
					stimer = setTimeout("setScroll()", 1);
				} else {
					clearTimeout(stimer);
				}
			}
		}
		scnt--;
	}
}

