// Copyright (c) 2002, 2003 AquaMinds Software Corporation. All rights reserved.

var sectionPageNumber   = 1;
var pageNumber          = (location.search.indexOf("?") == 0) ? location.search.substring(1, location.search.length) : 0;
var selectedEntryId     = (location.hash.length > 1) ? location.hash.substring(1, location.hash.length) : -1;
var notebookPageCount   = 1;
var notebookTitle       = "";
var notebookTitleHeight = 55;
var titleColor          = "";		       // Title frame background color
var sectionName         = "";	           // Section name of current page
var pageName            = "";		       // Name of current page
var titleStyles         = new Array();     // Array of style definitions for title frame
var entryStyles         = new Array();     // Array of style definitions for page frame
var hasCoverPage        = 0;
var contentsValid       = 0;
var showControls        = 1;
var showCoverTitle      = 1;
var showCoverRevision   = 0;
var revision            = "";

function gotoPage(pageNum, entryId) {
    var i;
    var doPageReload;

    // Make sure the new page number is within bounds
    if (pageNum < 0) {
        pageNum = hasCoverPage ? 0 : 1;
    }
    else if ((pageNum == 0) && !hasCoverPage) {
        pageNum = 1;
    }
    else if (pageNum > notebookPageCount) {
        pageNum = notebookPageCount;
    }

    // remember the previous page
    parent.document.cookie = "previousPageNumber=" + parent.pageNumber;

    // Set vars to new page and selected entry id
    doPageReload = (parent.pageNumber == pageNum) && (parent.selectedEntryId != entryId) && (entryId > 0);
    parent.pageNumber = pageNum;
    parent.selectedEntryId = entryId;

    // Change the window location to the new page
    i = location.href.indexOf("?");
    location.href = ((i >= 0) ? location.href.substring(0, i) : location.href)  + "?" + pageNum + ((entryId < 0) ? "" : ("#" + entryId));

    if (doPageReload)
        // Force page to refresh so that selection rectangle changes
	frames[0].location.reload();
}

function prevPage() {
    parent.gotoPage((parent.pageNumber == (parent.hasCoverPage ? 0 : 1)) ? parent.notebookPageCount : (parent.pageNumber - 1), -1);
}

function nextPage() {
    parent.gotoPage((parent.pageNumber == parent.notebookPageCount) ? 1 : (parent.pageNumber + 1), -1);
}

function writePage(doc) {
    // Make sure the new page number is within bounds
    if (pageNumber < 0) {
        pageNumber = hasCoverPage ? 0 : 1;
    }
    else if ((pageNumber == 0) && !hasCoverPage) {
        pageNumber = 1;
    }
    else if (pageNumber > notebookPageCount) {
        pageNumber = notebookPageCount;
    }

    doc.write("<HTML><HEAD>");
    doc.write('<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"notebook_pages/' + pageNumber +  "/contents.js\"></SCRIPT>");
    doc.write("</HEAD>");
    var NS4 = (window.navigator.appName.toUpperCase().indexOf("NETSCAPE") >= 0) &&
                (parseInt(window.navigator.appVersion) < 5);
    var titleHeight = notebookTitleHeight + (NS4 ? 5 : 0);
    doc.write("<FRAMESET ROWS=\"" + ((parent.pageNumber > 0) ? titleHeight : 2) + ", *\" BORDER=\"0\">");
    doc.write("<FRAME NAME=\"titleFrame\" SRC=\"pageTitle.html\" SCROLLING=\"NO\" MARGINHEIGHT=\"0\" NORESIZE>");
    doc.write("<FRAME NAME=\"pageBodyFrame\"  SRC=\"pageBody.html\"></FRAMESET></HTML>\n");
}

// Global function which generates the BODY HTML for the title frame
function writeTitle(doc) {
    doc.write("<HTML><HEAD>");
    for (var i = 0; i < titleStyles.length; i++)
        doc.write(titleStyles[i]);
    doc.write("<HEAD>");
    if (hasCoverPage) {
        doc.write("<BODY BGCOLOR=\"" + titleColor + "\" ondblclick=\"parent.parent.gotoPage(0, -1);\" >");
    }
    else {
        doc.write("<BODY BGCOLOR=\"" + titleColor + "\" >");
    }
    doc.write("<IMG SRC=\"notebook_images/top.gif\" style=\"position:absolute; left:0; top:0\">");
    doc.write("<IMG SRC=\"notebook_images/divider.gif\" style=\"position:absolute; left:0; top:" + (notebookTitleHeight-2) + "\">");

    doc.write("<TABLE VSPACE=\"0\" NOBORDER CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\" BGCOLOR=\"" + titleColor + "\" " +
                  "ONMOUSEOVER=\"window.status=\'Double-click to go to cover page\'; return true;\" " +
                  "ONMOUSEOUT=\"window.status=\'\'; return true;\">");
    doc.write("<TR><TD HEIGHT=4>");
    if (pageNumber > 0) {
       doc.write("<TD ROWSPAN=3 WIDTH=\"30%\" ALIGN=\"RIGHT\" VALIGN=\"MIDDLE\">Page " + pageNumber + " of " + notebookPageCount + "&nbsp;");
    }
    doc.write("<TD ROWSPAN=3 WIDTH=\"45\" ALIGN=\"RIGHT\" VALIGN=\"TOP\" NOWRAP><IMG SRC=\"notebook_images/dogears.gif\" BORDER=\"0\" ALIGN=\"ABSMIDDLE\" USEMAP=\"#earsmap\">");
    if (pageNumber > 0) {
       doc.write("<TR><TD VALIGN=\"MIDDLE\" WIDTH=\"70%\">&nbsp;&nbsp;");
    }
    else { 
       doc.write("<TR><TD ALIGN=\"CENTER\" VALIGN=\"MIDDLE\" WIDTH=\"100%\">&nbsp;&nbsp;");
    }
    doc.write(sectionName);
    doc.write("<TR><TD VALIGN=\"MIDDLE\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    doc.write(pageName);
    doc.write("\n</TABLE>");

    doc.write("<MAP NAME=\"earsmap\">\n");
    doc.write("<AREA SHAPE=POLY COORDS=\"0,0,40,40,0,40\" HREF=\"javascript:parent.parent.prevPage();\" ONMOUSEOVER=\"window.status=\'Go to previous page\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true\" >");
    doc.write("<AREA SHAPE=POLY COORDS=\"0,0,40,40,40,0\" HREF=\"javascript:parent.parent.nextPage();\" ONMOUSEOVER=\"window.status=\'Go to next page\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true\" >");
    doc.write("</MAP>");

    doc.write("\n</BODY></HTML>\n");
}

// First set all the tabs to their lowered image.  Then, if the current page has a tab, set its
// image to the raised image. If the current page has no tab image, use the current section's tab.
function updateTabs(document) {
    var i;
    var n = document.images.length;

    for (i = 2; i < n; i++) {
        document.images[i].src = document.images[i].name + "/tabL.png";
        document.images[i].border=0;
    }
    if (document.images["notebook_pages/" + pageNumber]) {
        document.images["notebook_pages/" + pageNumber].src = "notebook_pages/" +  pageNumber + "/tabH.png";
    }
    else if (document.images["notebook_pages/" + sectionPageNumber]) {
        document.images["notebook_pages/" + sectionPageNumber].src = "notebook_pages/" +  sectionPageNumber + "/tabH.png";
    }
}

