// functions to display table of contents

var currentTOCItem = null;
var tocProlog =
      '<html><head><title>Table of contents</title>'
	+ '<link rel="stylesheet" type="text/css" href="toc.css"></head>'
    + '<body>';
var tocEpilog = '</body></html>';

// Write the table of contents to the toc frame
function writeTOC() {
	var content = '';
	var row = 0;
    for (i = 0; i < tocItems.length; i++) {
    	var tocItem = tocItems[i];
		if (tocItem.getVisible()) {
			content += tocItems.getRow(i, row);
			row++;
		}
    }

    var doc = toc.document;

	// Save scrollTop
    var scrollY;
    if (doc.body && doc.body.scrollTop) {
    	scrollY = doc.body.scrollTop;
    } else {
    	scrollY = 0;
    }
    
    if (doc.body.innerHTML) {
    	doc.body.innerHTML = content;
    } else {
    	doc.open();
    	doc.writeln(tocProlog + content + tocEpilog);
    	doc.close();
    }

    // Restore scrollTop
    toc.scroll(0, scrollY);
}

function monitorContent() {
	if (typeof(getContentURL) != 'undefined') {
		var contentURL = getContentURL(window.location, content.location);
		if (currentTOCItem && (contentURL != currentTOCItem.url)) {
			showURL(contentURL, false /* changeContent */);
		}
	}
	window.setTimeout('monitorContent()', 1500); // Every 1.5 seconds
}

function showItem(id, changeCurrent, changeContent, mayCollapse) {
	// Should be synchronized
	var tocItem = tocItems.getItemByID(id);
	if (tocItem) {
		if (tocItem.expanded) {
			if (mayCollapse)
				tocItem.expanded = false;
			if (tocItem.parentItem)
				tocItem.parentItem.expand();
		} else tocItem.expand();
	}
	if (changeCurrent)
		currentTOCItem = tocItem;
	writeTOC();
	if (changeContent && tocItem && (tocItem.url != "")) {
		if (tocItem.target == "")
			content.location = tocItem.url;
		else window.location = tocItem.url;
	}
}

function showURL(url, changeContent) {
	if (url)
    	showItem(tocItems.getItemIDByURL(url), true /* changeCurrent */, changeContent, false /* mayCollapse */);
}

function doLoad() {
    // return the value of the url parameter
    var s = location.search.substring(1);
    var url = null;
    var startPos = s.indexOf("url=");
    if (startPos > -1) {
        startPos = startPos + 4;
        var endPos = s.indexOf("&", startPos);
        if (endPos == -1)
            endPos = s.length;
        url = unescape(s.substring(startPos, endPos));
    }
    if (url)
    	showURL(url, true /* changeContent */);
    else showItem(InitialID, true /*changeCurrent*/, false /*changeContent*/, false /*mayCollapse*/);
    window.setTimeout('monitorContent()',3000);
}

