// Copyright (c) 2002-2004 AquaMinds Software Corporation. All rights reserved.

var allEntries = new Array();
var topEntries = new Array();

// Global method called when clicking on the expand or contract image for an entry
function expand(id) {
    var x = 0;
    var y = 0;
    if (pageBodyFrame.document.body && pageBodyFrame.document.body.scrollTop) {
        // IE
        x = pageBodyFrame.document.body.scrollLeft;
        y = pageBodyFrame.document.body.scrollTop;
    }
    else if (pageBodyFrame.scrollY) {
        // Mozilla
        x = pageBodyFrame.scrollX;
        y = pageBodyFrame.scrollY;
    }
    else if (pageBodyFrame.pageYOffset) {
        // Netscape
        x = pageBodyFrame.pageXOffset;
        y = pageBodyFrame.pageYOffset;
    }
    allEntries[id].expanded = !allEntries[id].expanded;
    writePageBody();
    pageBodyFrame.scrollTo(x, y);
}

// Entry method which returns the HTML for an entry (a table with the image and contents)
function getHTML(indent, doc) {
    doc.write("<A NAME=\"Entry" + this.id + "\"><TABLE ENTRIESPACING=\"0\" ENTRYPADDING=\"2\" BORDER=\"" + (this.selected ? 1 : 0) + "\" WIDTH=\"100%\">\n");
    doc.write("<TR> <TD NOWRAP WIDTH=" + (indent * 26) + " ALIGN=RIGHT VALIGN=TOP>\n");
    if (parent.showControls) {
        if (this.children.length > 0) {
            doc.write("<A HREF=\"javascript:parent.expand(");
            doc.write(this.id);
            doc.write(")\" ONMOUSEOVER=\"window.status=\'Expand/Collapse selected outline\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\">");
            doc.write("<IMG BORDER=\"0\" SRC=\"");
            doc.write((this.expanded ? "notebook_images/open.png" : "notebook_images/closed.png"));
            doc.write("\"></A>\n");
        }
        else {
            if (this.linkedPageNum > 0) {
                doc.write("<A HREF=\"javascript:parent.parent.gotoPage(");
                doc.write(this.linkedPageNum);
                doc.write(",");
                doc.write(this.linkedEntryId);
                doc.write(");\" ");
                doc.write("ONMOUSEOVER=\"window.status=\'Go to selected page\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\" TARGET=\"_parent\"><IMG BORDER=\"0\" SRC=\"notebook_images/link.png\"></A>\n");
            }
            else {
                doc.write("<IMG BORDER=\"0\" SRC=\"notebook_images/node.png\">\n");
            }
        }
    }
    doc.write("<TD ALIGN=\"LEFT\" WIDTH=\"100%\">");
    doc.write(this.contents);
    doc.write("\n</TABLE></A>\n");
    if ((this.children.length > 0) && this.expanded) {
        for (var i = 0; i < this.children.length; i++) {
            this.children[i].getHTML(indent + 1, doc);
        }
    }
    this.selected = false;
}

// Entry method which adds a child entry to a parent entry
function addChild(child) {
    this.children[this.children.length] = child;
    child.parent = this;
    return child;
}

// Entry method called to expand a entry. Expands all parent entries as well
function setExpanded(flag) {
    this.expanded = true;
    if (this.expanded && (this.parent != null)) {
	    this.parent.setExpanded(true);
    }
}

// Entry method called to flag an entry as selected.  Expands the parent to 
// make sure the entry is visible
function setSelected(flag) {
    this.selected = flag;
    if (this.selected && (this.parent != null)) {
	    this.parent.setExpanded(true);
    }
}

// Entry object definition
function entry(id, contents, expanded, linkedPageNum, linkedEntryId) {
    this.id = id;
    allEntries[this.id] = this;
    this.contents       = contents;
    this.children       = new Array();
    this.parent         = null;
    this.expanded       = expanded;
    this.linkedPageNum  = linkedPageNum;
    this.linkedEntryId  = linkedEntryId;
    this.selected       = false;
    this.setSelected    = setSelected;
    this.setExpanded    = setExpanded;
    this.getHTML        = getHTML;
    this.addChild       = addChild;
}

// Retrieve the previous page number from our cookie
function previousPageNumber() {
    var n = 1;
    var i1 = parent.document.cookie.indexOf("previousPageNumber=");
    if (i1 >= 0) {
        var i2 = parent.document.cookie.indexOf(";", i1 + 19);
        if (i2 < 0) i2 = parent.document.cookie.length;
        n = parent.document.cookie.substring(i1, i2);
        if (n < 1) n = 1;
        if (n > parent.parent.notebookPageCount)
            n = parent.parent.notebookPageCount;
    }
    return n;
}

// Global function which generates the BODY HTML for the page (entry list) frame
function writePageBody() {
    var pageNum = 1;
    pageBodyFrame.document.write("<HTML><HEAD>");
    pageBodyFrame.document.write('<META http-equiv="Content-Type" content="text/html; charset=UTF-8">');
    for (var i = 0; i < parent.entryStyles.length; i++)
        pageBodyFrame.document.write(parent.entryStyles[i]);
    pageBodyFrame.document.write(parent.pageBackgroundStyle);
    pageBodyFrame.document.write('<script language="JavaScript">\nfunction go(pn, en) {\nparent.parent.gotoPage(pn, en);\n}\n</script>');
    pageBodyFrame.document.write("</HEAD>");
    var i = parent.selectedEntryId;
    if ((i >= 0) && (i < allEntries.length)) {
        allEntries[i].setSelected(true);
    }
    pageBodyFrame.document.write("<BODY class=\"pageBackgroundStyle\"" +
                                 " onload=\"parent.parent.updateTabs(parent.parent.tabFrame.document);\"");
    if (parent.pageNumber == 0) {
        pageNum = previousPageNumber();
        pageBodyFrame.document.write(" onclick=\"parent.parent.gotoPage(" + pageNum + ", -1);\"");
        pageBodyFrame.document.write(" onmousemove=\"window.status=\'Click to enter notebook\'; return true;\"");
    }
    pageBodyFrame.document.write(" >");
    pageBodyFrame.document.bgcolor = parent.pageColor;
    entryCount = 0;
    for (i = 0; i < topEntries.length; i++) {
        topEntries[i].getHTML(0, pageBodyFrame.document);
    }
    if (parent.pageNumber == 0) {
        if ((parent.showCoverTitle != 0) || (parent.showCoverRevision != 0)) {
            pageBodyFrame.document.write("<TABLE class=\"coverTitleTableStyle\" border=\"0\" cellspacing=\"4\">");
            if (parent.showCoverTitle != 0)
                pageBodyFrame.document.write("<TR><TD><DIV onclick=\"parent.parent.gotoPage(" + pageNum + ", -1);\" class=\"coverTitleStyle\">" + parent.coverText + "</DIV>");
            if (parent.showCoverRevision != 0)
                pageBodyFrame.document.write("<TR><TD><DIV onclick=\"parent.parent.gotoPage(" + pageNum + ", -1);\" class=\"coverRevisionStyle\">" + parent.revision + "</DIV>");
            pageBodyFrame.document.write("</TABLE>");
        }
        
        if (parent.showCoverBadge != 0)
            pageBodyFrame.document.write("<table width=\"98%\" style=\"position:absolute; top:92%\"><tr><td align=center><a href=\"http://www.aquaminds.com\" target=\"aminds\"><img src=\"notebook_backgrounds/badge.gif\"><\a></table>");
    }
    pageBodyFrame.document.write("</BODY></HTML>\n");
    pageBodyFrame.document.close();
}
