// Copyright (c) 2002, 2003 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) {
    var result = "<A NAME=\"Entry" + this.id + "\"><TABLE ENTRIESPACING=\"0\" ENTRYPADDING=\"2\" BORDER=\"" + (this.selected ? 1 : 0) + "\">\n";
    result += "<TR> <TD WIDTH=" + (indent * 26) + " ALIGN=RIGHT VALIGN=TOP>\n";
    if (this.children.length > 0) {
        result += "<A HREF=\"javascript:parent.expand(" + this.id + ")\" ";
        result += "ONMOUSEOVER=\"window.status=\'Expand/Collapse selected outline\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\">";
        result += "<IMG BORDER=\"0\" SRC=\"" + (this.expanded ? "notebook_images/Expand.png" : "notebook_images/Contract.png") + "\">";
        result += "</A>\n";
    }
    else {
	if (this.linkedPageNum > 0) {
	    result += "<A HREF=\"javascript:parent.parent.gotoPage(" + this.linkedPageNum + "," + this.linkedEntryId + ");\" ";
	    result += "ONMOUSEOVER=\"window.status=\'Go to selected page\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\" ";
	    result += "TARGET=\"_parent\"><IMG BORDER=\"0\" SRC=\"notebook_images/Link.png\"></A>\n";
	}
        else {
	    result += "<IMG BORDER=\"0\" SRC=\"notebook_images/Leaf.png\">\n";
	}
    }
    result += "<TD ALIGN=\"LEFT\">" + this.contents + "\n";
    result += "</TABLE></A>\n";
    if ((this.children.length > 0) && this.expanded) {
        for (var i = 0; i < this.children.length; i++) {
            result += this.children[i].getHTML(indent + 1);
        }
    }
    this.selected = false;
    return result;
}

// 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>");
    for (var i = 0; i < parent.entryStyles.length; i++)
        pageBodyFrame.document.write(parent.entryStyles[i]);
    pageBodyFrame.document.write(parent.pageBackgroundStyle);
    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.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++) {
        pageBodyFrame.document.write(topEntries[i].getHTML(0));
    }
    if (parent.parent.pageNumber == 0) {
        pageBodyFrame.document.write("<TABLE WIDTH=\"100%\" HEIGHT=\"100%\" NOBORDER  onclick=\"parent.parent.gotoPage(" + pageNum + ", -1);\" >");
        pageBodyFrame.document.write("<TR><TD HEIGHT=\"62%\" ALIGN=CENTER VALIGN=CENTER>");
        pageBodyFrame.document.write("<SPAN onclick=\"parent.parent.gotoPage(" + pageNum + ", -1);\" class=\"coverTitleStyle\">" + parent.parent.notebookTitle + "</SPAN>");
        pageBodyFrame.document.write("</TD></TR><TR><TD HEIGHT=\"38%\"></TD></TR></TABLE>\n");
    }
    pageBodyFrame.document.write("</BODY></HTML>\n");
    pageBodyFrame.document.close();
}

