﻿/*
<div class="TableOfContents">
	<table><tbody><tr><td>1.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.1.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.1.1.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.1.2.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.1.3.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.2.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>1.3.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	<table><tbody><tr><td>2.</td><td><a href="#id">セクションタイトル</a></td></tr></tbody></table>
	...
</div>
*/

function TableOfContents() {
}

TableOfContents.GiveSectionNumber = function(sections)
{
	for(var i = 0; i < sections.length; i++){
		var section = sections[i];
		section.number = i + 1;
		if(section.parentSection == null){
			section.formatNumber = section.number + ".";
		}else{
			section.formatNumber = section.parentSection.formatNumber + section.number + ".";
		}
		TableOfContents.GiveSectionNumber(section.childSections);
	}
}


TableOfContents.Create = function()
{
	//作業用
	var xList;
	
	xTOC = document.createElement("div");
	xTOC.className = "TableOfContents";

	var sections = new Array();
	xList = document.getElementsByTagName("*");
	for(var i = 0; i < xList.length; i++){
		if(xList[i].className == "Section"){
			sections.push(xList[i]);
		}
	}
	
	//セクションにIDの割り振り
	for(var i = 0; i < sections.length; i++){
		var section = sections[i];
		if(section.id == undefined || section.id == null || section.id == ""){
			section.id = "__TableOfContentsSectionId" + i;
		}
	}
	
	//セクションの親子関係を解析
	var rootSections = new Array();
	for(var i = 0; i < sections.length; i++) sections[i].childSections = new Array();
	for(var i = 0; i < sections.length; i++){
		var section = sections[i];
		section.parentSection = null;
		var elem = section.parentNode;
		while(elem != null){
			if(elem.className == "Section"){
				section.parentSection = elem;
				elem.childSections.push(section);
				break;
			}
			elem = elem.parentNode;
		}
		if(section.parentSection == null){
			rootSections.push(section);
		}
	}
	
	TableOfContents.GiveSectionNumber(rootSections);
	
	//ページトップへのページ内リンクを生成
	var topA = document.createElement("a");
	document.body.insertBefore(topA, document.body.firstChild);
	topA.id = "__TableOfContentsSectionIdPageTop";
	var table = document.createElement("table");
	xTOC.appendChild(table);
	TableOfContents.CreateTableOfContentsItemTable(table, "", "ページ先頭へ", topA.id);

	//各セクションへのページ内リンクを生成
	for(var i = 0; i < sections.length; i++){
		var section = sections[i];
		var h = section.getElementsByTagName("*")[0];
		//セクションタイトルの取得
		section.title = h.firstChild.nodeValue;
		//セクションタイトルにセクション番号を付与
		h.firstChild.nodeValue = section.formatNumber + h.firstChild.nodeValue;

		var table = document.createElement("table");
		xTOC.appendChild(table);
		TableOfContents.CreateTableOfContentsItemTable(table, section.formatNumber, section.title, section.id);
		
	}
	
	return xTOC;
}

TableOfContents.CreateTableOfContentsItemTable = function(table, formatNumber, sectionTitle, sectionId)
{
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);
	var tr = document.createElement("tr");
	tbody.appendChild(tr);

	var td1 = document.createElement("td");
	tr.appendChild(td1);
	td1.appendChild(document.createTextNode(formatNumber));
	td1.style.verticalAlign = "top";
	td1.style.whiteSpace = "nowrap";
	
	var td2 = document.createElement("td");
	tr.appendChild(td2);
	var a = document.createElement("a");
	a.href = "#" + sectionId;
	a.appendChild(document.createTextNode(sectionTitle));
	td2.appendChild(a);
	
	return table;
}