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

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

if (isIPhone) {
    document.write("<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"iphone.js\"></SCRIPT>");
}

var bgImageXOffset = 0;
var bodyIsLoaded = 0;

function gotoSelectedEntry() {
    var frame = frames["pageFrame"].frames["pageBodyFrame"];
    var hash  = (selectedText && (selectedText.length > 0)) ? "selection" : ("" + selectedEntryId);
    var e = frame.document.getElementsByName(hash)[0];
    var y = 0;
    while (e) {
        y += e.offsetTop;
        e = e.offsetParent;
    }
    if (y > 0) {
        // This is better than using hash because it does not add to the history
        if (isIPhone || isIPad)
            window.scrollTo(0, y);
        else
            frame.scrollTo(0, y);
    }
    else {
        frame.location.hash = hash;
    }
    selectedText = null;
    selectedEntryId = -1;
}

function bodyLoaded(pageNum) {
    if (pageNum)
        pageNumber = pageNum;
    var frame = frames["pageFrame"].frames["pageBodyFrame"];
    var doc   = frame.document;
    if (bgImageType >= 2) {
		if (doc.pageBgImage && !isIPhone) {
			bgImageSize();
		}
    }
    if ((pageNum > 0) && (!allEntries || !allEntries.isSet)) {
        frame.location = frame.location;
    }
    if (selectedEntryId && (selectedEntryId >= 0)) {
		setTimeout("gotoSelectedEntry()", isIPhone ? 150 : (isIPad ? 50 : 20));
    }
    setTimeout("bodyIsLoaded = 1;", 5);
}

function bodyResized() {
    bgImageSize(bodyIsLoaded ? 0 : 16);
}

function bgImageSize(sbWidth) {
    var frame = frames["pageFrame"].frames["pageBodyFrame"];
    var doc   = frame.document;
	
    var winWidth  = 0;
    var winHeight = 0;
    
    var imgWidth  = bgImageWidth;
    var imgHeight = bgImageHeight;
    var imgLeft   = 0;
	
    if (frame.innerWidth) {
		winWidth  = frame.innerWidth;
		winHeight = frame.innerHeight;
    }
    else if (doc.body.clientWidth) {
		winWidth  = doc.body.clientWidth;
		winHeight = doc.body.clientHeight;
    }
    if (winWidth == 0) {
		if (frame.parent) {
			winWidth  = frame.parent.innerWidth;
			winHeight = frame.parent.innerHeight;
			if (winWidth == 0) {
				winWidth  = frame.parent.top.innerWidth;
				winHeight = frame.parent.top.innerHeight;
			}
		}
    }
    if ((winWidth > 0) && (winHeight > 0)) {
		if (isSafari && sbWidth && (pageNumber > 0))
			winWidth += sbWidth;
		if (isIPhone) {
			if ((winWidth < 320) && (winHeight > 420))
				winHeight = 420;
			else if ((winWidth > 400) && (winHeight > 270))
				winHeight = 270;
		}
		
		var bgImageRatio = imgHeight / imgWidth;
		
		if (bgImageType == 2) {
			// fit to width of page
			imgWidth  = winWidth;
			imgHeight = (winWidth * bgImageRatio);
		}
		else if (bgImageType == 3) {
			// cover - preserve aspect but cover the page
			var winRatio = winHeight / winWidth;
			if (winRatio > bgImageRatio) {
				imgHeight = winHeight;
				imgWidth  = (imgHeight / bgImageRatio);
				if (imgWidth > winWidth) {
					imgLeft = winWidth - imgWidth;
					if (!isIPhone) {
						imgLeft = imgLeft / 2;
					}
				}
			}
			else {
				imgWidth  = winWidth;
				imgHeight = (winWidth * bgImageRatio);
			}
		}
		else if (bgImageType == 4) {
			// scale
			imgWidth  = winWidth;
			imgHeight = winHeight;
		}
    }
    
    imgLeft   = Math.round(imgLeft);
    imgWidth  = Math.round(imgWidth);
    imgHeight = Math.round(imgHeight);
	
    if (!isIPhone) {
		var img = doc.pageBgImage;
		if (img) {
			img.left   = imgLeft;
			img.width  = imgWidth;
			img.height = imgHeight;
		}
    }
    
    imgWidth  = (Math.abs(imgWidth - winWidth) > 2) ? (imgWidth + "px") : "100%";
    return "left: " + imgLeft + "px; top: 0px; width: " + imgWidth + "; height: " + imgHeight + "px";
}

var sx = 0;
var sy = 0;

function scrollback() {
    var frame = frames["pageFrame"].frames["pageBodyFrame"];
    frame.scrollTo(sx, sy);
}

// Global method called when clicking on the expand or contract image for an entry
function expand(id) {
    sx = 0;
    sy = 0;
    var frame = frames["pageFrame"].frames["pageBodyFrame"];
    var doc   = frame.document;
    var bd    = doc.body;
    if (doc.documentElement && doc.documentElement.scrollTop) {
        // IE
        sx = doc.documentElement.scrollLeft;
        sy = doc.documentElement.scrollTop;
    }
    else if (doc.body && doc.body.scrollTop) {
        // IE
        sx = doc.body.scrollLeft;
        sy = doc.body.scrollTop;
    }
    else if (frame.scrollY) {
        // Mozilla
        sx = frame.scrollX;
        sy = frame.scrollY;
    }
    else if (frame.pageYOffset) {
        // Netscape
        sx = frame.pageXOffset;
        sy = frame.pageYOffset;
    }
    var entry = allEntries["id"+id];
    entry.expanded = !entry.expanded;
    if (entry.pageNumber)
        pageNumber = entry.pageNumber;
    writePageBody(doc, true);
    loadStyles(doc, 0);
    setTimeout("scrollback()", 100);
}

// This function mimics the html encoding of the entry content done when the web notebook is generated
function encode(str) {
    var index  = 0;
    var len    = str.length;
    var entity;
    var added;
    var chr;
    var code;
    
    while (index < len) {
		chr = str.charAt(index);
		entity = null;
		
		switch (chr) {
			case  '&': entity = "&amp;";  break;
			case  '<': entity = "&lt;";   break;
			case  '>': entity = "&gt;";   break;
			case '\"': entity = "&quot;"; break;
			case '\'': entity = "&#39;"; break;
			case '\\': entity = "&#92;"; break;
			case  '#': entity = "&#35;"; break;
			default: {
				code = str.charCodeAt(index);
				if ((code >= 127) && (code < 256)) {
					entity = String(code);
					while (entity.length < 4) {
						entity = "0" + entity;
					}
					entity = "&#" + entity + ";";
				}
				break;
			}
		}
		
		if (entity != null) {
			str    = str.substr(0, index) + entity + str.substr(index + 1);
			added  = entity.length - 1;
			index  += added;
			len    += added;
		}
		
		index++;
    }
    
    return str;
}


//  ---------------  Entry object definition  ---------------------

// Entry method which returns the HTML for an entry (a table with the image and contents), and its expanded children
function getHTML(indent, doc) {
    var s = "";
    s += "<A NAME=\"" + this.id + "\"><TABLE ID=\"" + this.id + "\" ENTRIESPACING=\"0\" ENTRYPADDING=\"2\" NOBORDER WIDTH=\"100%\">\n";
    s += "<TR> <TD style=\"white-space: nowrap; width: " + ((indent + 1) * 15) + "px; text-align: right; vertical-align:top\">\n";
    if (showControls) {
        if (this.children.length > 0) {
            var ss = "<A HREF=\"javascript:parent.parent.expand(";
            ss += this.id;
            ss += ")\" ONMOUSEOVER=\"window.status=\'Expand/Collapse selected outline\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\">";
            ss += "<IMG BORDER=\"0\" SRC=\"";
            ss += imageURL + (this.expanded ? "open.png" : "closed.png");
            ss += "\"></A>\n";
			s += ss;
        }
        else {
            if (this.linkedPageNum > 0) {
                var ss = "<A HREF=\"javascript:parent.gotoPage(";
                ss += this.linkedPageNum;
                ss += ",";
                ss += this.linkedEntryId;
                ss += ");\" ";
                ss += "ONMOUSEOVER=\"window.status=\'Go to selected page\'; return true;\" ONMOUSEOUT=\"window.status=\'\'; return true;\" TARGET=\"_parent\"><IMG BORDER=\"0\" SRC=\"" + imageURL + "link.png\"></A>\n";
				s += ss;
            }
            else {
				if (this.todoState == 2) {
					s += "<IMG BORDER=\"0\" SRC=\"" + imageURL + "todo_check_on.png\">\n";
				}
				else if (this.todoState == 1) {
					s += "<IMG BORDER=\"0\" SRC=\"" + imageURL + "todo_check_off.png\">\n";
				}
				else {
					s += "<IMG BORDER=\"0\" SRC=\"" + imageURL + "node.png\">\n";
				}
            }
        }
    }
	
    s += "<TD style=\"text-align:left";
    
    var isSectionPage = (this.linkedPageNum > 0) && (pageName == "");
    
    if (isIPad) {
	s += "; -webkit-text-size-adjust:" + Math.round(107.0 * fontScaling) + "%";
    }

    if (this.selected) {
		if (!this.selectedText) {
			s += "; background: #B5D5FF";
		}
		else {
			s += "; border: 1px solid #B5D5FF";
		}
    }
    if (isSectionPage) {
		s += "; cursor:pointer";
    }
    s += "\"";
    
    if (isSectionPage) {
		// On section pages, link the page names to the pages
		s += " onclick=\"javascript:parent.parent.gotoPage(" + this.linkedPageNum + "," + this.linkedEntryId + ");\"";
    }
    else if (this.children.length) {
		if (isIPhone) {
			s += " onclick=\"javascript:parent.parent.expand(" + this.id + ")\"";
		}
    }
    s += "  >";
    
    // Make some changes for the iPhone
    var str = this.contents;
    
    if (isIPhone) {
		var w = window.innerWidth - 28;
		var ringFrame = window.document.getElementById("vringFrame");
		if (ringFrame)
			w -= ringFrame.width;
		var tabFrame = window.document.getElementById("tabFrame");
		if (tabFrame)
			w -= tabFrame.width;
        w -= ((indent+1) * 15);
		str = resizeImages(str, w);   // size images to available width
		str = removeIFrame(str, w);   // iframes not terribly useful in this context
		str = str.replace(/LinkPage/g, "_top"); // keep external links in same "window" so back button works as expected
		str = fixMovieEmbeds(str);  // reference the preview image, etc
		str = wrapLongURLs(str);
    }
    else if (isIPad)
		str = str.replace(/LinkPage/g, "_top"); // keep external links in same "window" so back button works as expected
        
    if (this.selectedText) {
		var sel = encode(this.selectedText);
        var i1 = 0;
        var i2 = 0;
        var i3 = -1;
        // Only search between tags, i.e. between ">" and "<"
        while (i2 < str.length) {
            i1 = str.indexOf(">", i2);
            if (i1 == -1) {
                i1 = 0;
                i2 = str.length;
            }
            else {
                i2 = str.indexOf("<", i1);
                if (i2 == -1)
                    i2 = str.length;
            }
            if ((i2 - i1) > 1) {
                var rExp = new RegExp(sel, "i");
                i3 = str.substr(i1, i2 - i1).search(rExp);
                if (i3 >= 0) {
                    i3 += i1;
                    break;
                }
            }
        }
        if (i3 >= 0) {
			var n = sel.length;
			str = str.substr(0, i3) + "<a name=\"selection\"><span style=\"background: #B5D5FF\">" + str.substr(i3, n) + "</span></a>" + str.substr(i3 + n);
        }
    }
    
    s += str;
    s += "</td>";
    if (isSectionPage) {
		s += "<td style=\"text-align:right; cursor:pointer";
    		if (isIPad) {
			s += "; -webkit-text-size-adjust:" + Math.round(107.0 * fontScaling) + "%";
    		}
		s += "\" onclick=\"javascript:parent.parent.gotoPage(" + this.linkedPageNum + "," + this.linkedEntryId + ");\"><span class=\"pageNumber\">" + this.linkedPageNum + "&nbsp;</span></td>";
    }
    
    s += "\n</TABLE></A>\n";
    if ((this.children.length > 0) && this.expanded) {
        for (var i = 0; i < this.children.length; i++) {
            s += this.children[i].getHTML(indent + 1, doc);
        }
    }
    this.selected = false;
    this.selectedText = null;
    return s;
}

// 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, todoState) {
    this.id = id;
    allEntries[("id"+this.id)] = this;
    allEntries.isSet    = true;
    this.contents       = contents;
    this.children       = new Array();
    this.parent         = null;
    this.expanded       = expanded;
    this.linkedPageNum  = linkedPageNum;
    this.linkedEntryId  = linkedEntryId;
    this.selected       = false;
    this.selectedText   = null;
    this.setSelected    = setSelected;
    this.setExpanded    = setExpanded;
    this.getHTML        = getHTML;
    this.addChild       = addChild;
    this.pageNumber     = pageNumber;
    if (todoState) {
        this.todoState = todoState;
    }
    else {
        this.todoState = 0;
    }
}

//  -----------------  End of Entry object definition  -----------------

function loadStyles(doc, version) {
    var base = baseURL + "pages/" + validatePageNumber(pageNumber) + "/";
    var vers = (version && (version > 0)) ? ("?version=" + version) : "";
    loadCSS(doc, base + "page.css" + vers);
    if (isIPhone)
    	loadCSS(doc, base + "page-iphone.css" + vers);
    if (!useTitleFrame && (pageNumber > 0))
    	loadCSS(doc, base + "title.css" + vers);
}

// ---------  Handle multi-touch slide gesture for turning pages on iPhone & iPad  -----------

var x0;
var y0;
var x1;
var y1;
var touchStartX0 = 0;
var touchDistanceStart = 0;
var isMulti = false;
var requireMulti = false;

function touchstart(event) {
    if (useTouch) {
        requireMulti = (window.innerWidth > screen.availWidth);
        x0 = event.touches[0].pageX;
        y0 = event.touches[0].pageY;
        touchStartX0 = x0;
        if (event.touches.length > 1) {
            scalableViewport();
            x1 = event.touches[1].pageX;
            y1 = event.touches[1].pageY;
            touchDistanceStart = Math.sqrt(Math.pow((x1 - x0), 2) + Math.pow((y1 - y0), 2));
        }
        else {
            touchDistanceStart = 0;
        }
    }
}

function touchmove(event) {
    if (useTouch) {
        x0 = event.touches[0].pageX;
        y0 = event.touches[0].pageY;
        isMulti = (event.touches.length > 1);
        if (isMulti) {
            x1 = event.touches[1].pageX;
            y1 = event.touches[1].pageY;
        }
    }
}

function touchend(event) {
    if (useTouch && (!requireMulti || isMulti)) {
		var pinched = false;
		if (isMulti) {
			var dist = Math.sqrt(Math.pow((x1 - x0), 2) + Math.pow((y1 - y0), 2));
			pinched = Math.abs(dist - touchDistanceStart) > 32;
		}
		if (!pinched) {
			if ((touchStartX0 - x0) > 60)
				nextPage();
			else if ((touchStartX0 - x0) < -60)
				prevPage();
		}
		else {
			// apply stricter test if possible pinch was detected
			if (((touchStartX0 - x0) > 100) && ((touchStartX1 - x1) > 100))
				nextPage();
			else if (((touchStartX0 - x0) < -100) && ((touchStartX1 - x1) < -100))
				prevPage();
		}
    }
}

//  ---------------   Main page content generating methods  ------------------------

// Global function which generates the BODY HTML for the title frame
function writeTitle(doc) {
    var s = "";
    s += "<IMG SRC=\"" + imageURL + "top.png\" style=\"position:absolute; left:0px; top:0px; width:100%; height:1px\">";
    
    // The section name
    s += "<table VSPACE=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" style=\"border-style: none; width: 100%";
    if (isIPhone)
        s += "; -webkit-text-size-adjust:89%";
    else if (isIPad)
        s += "; -webkit-text-size-adjust:" + Math.round(100.0 * fontScaling) + "%";
    s += "\" ONMOUSEOUT=\"window.status=\'\'; return true;\"";
    if (isIPhone || isIPad)
		s += " ontouchstart=\"parent.parent.touchstart(event);\" ontouchmove=\"parent.parent.touchmove(event);\" ontouchend=\"parent.parent.touchend(event);\"";
    s += ">";
    s += "<TR><TD VALIGN=\"MIDDLE\">&nbsp;&nbsp;" + sectionName;
    
    // previous and next page buttons
    if (!isIPhone && !isIPad) {
		s += "<TD WIDTH=96 HEIGHT=28 ALIGN=\"MIDDLE\" VALIGN=\"MIDDLE\" NOWRAP style=\"-webkit-user-select:none\">";
		s += "<IMG name=\"Prev\" SRC=\"" + imageURL + "prev.png\" BORDER=\"0\" ALIGN=\"ABSMIDDLE\"  onMouseDown=\"javascript:parent.parent.prevDown(document, true);\" onMouseUp=\"javascript:parent.parent.prevDown(document, false);\" onclick=\"javascript:parent.parent.prevPage();\" style=\"cursor:pointer\"><IMG name=\"Next\" SRC=\"" + imageURL + "next.png\" BORDER=\"0\" ALIGN=\"ABSMIDDLE\" onMouseDown=\"javascript:parent.parent.nextDown(document, true);\" onMouseUp=\"javascript:parent.parent.nextDown(document, false);\" onclick=\"javascript:parent.parent.nextPage();\" style=\"cursor:pointer\">";
    }
    else {
        // Use larger buttons and also use touch events instead of mouse events to highlight the button.
		s += "<TD WIDTH=112 HEIGHT=32 ALIGN=\"MIDDLE\" VALIGN=\"MIDDLE\" NOWRAP style=\"-webkit-user-select:none\">";
		s += "<IMG name=\"Prev\" SRC=\"" + imageURL + "prev2.png\" BORDER=\"0\" ALIGN=\"ABSMIDDLE\"  ontouchstart=\"javascript:parent.parent.prevDown(document, true);\" ontouchend=\"javascript:parent.parent.prevDown(document, false);\" onclick=\"javascript:parent.parent.prevPage();\"><IMG name=\"Next\" SRC=\"" + imageURL + "next2.png\" BORDER=\"0\" ALIGN=\"ABSMIDDLE\" ontouchstart=\"javascript:parent.parent.nextDown(document, true);\" ontouchend=\"javascript:parent.parent.nextDown(document, false);\" onclick=\"javascript:parent.parent.nextPage();\">";
    }
    
    // page name
    s += "<TR><TD VALIGN=\"TOP\" style=\"text-indent: 20px\">" + pageName;
    
    // page number
    if (pageNumber > 0) {
		s += "<TD class=\"pageNumber\" NOWRAP ALIGN=\"CENTER\" VALIGN=\"TOP\">";
		if (!isIPhone)
			s += "Page ";
		s += (pageNumber + " of " + notebookPageCount);
    }
    s += "</table>";

    // add a little extra space at the bottom, then the border line
    s += "<div style=\"height: 2px\"></div><IMG SRC=\"" + imageURL + "divider.png\" style=\"position:absolute; bottom:0px; width:100%; height:2px\">";
    doc.write(s);
}

// Global function which generates the BODY HTML for the page (entry list) frame
function writePageBody(doc, entirePage) {
    bodyIsLoaded = 0;
    
    doc.open("text/html", "replace");
    if (entirePage) {
        //doc.write('<html><head><meta http-equiv="CACHE-CONTROL" content="NO-STORE, NO-CACHE" /><META http-equiv="expires" content="Sat, 1 Jan 2000 08:00:00 GMT"><SCRIPT type="text/javascript" SRC="contents.js"></SCRIPT></head><body>');
        doc.write('<html><head><meta http-equiv="CACHE-CONTROL" content="NO-STORE, NO-CACHE" /><META http-equiv="expires" content="Sat, 1 Jan 2000 08:00:00 GMT"></SCRIPT></head><body class="pageBackgroundStyle" style="height:100%" onload="parent.parent.bodyLoaded(' + pageNumber + ')" onresize="parent.parent.bodyResized()">');
    }

    if (notebookTitleHeight == 0) {
		doc.write("<IMG SRC=\"" + imageURL + "top.png\" style=\"position:absolute; left:0px; top:0px; width:100%; height:1px\">");
    }
    
    var includeTitle   = !useTitleFrame && (pageNumber > 0);
    var includeBgImage = (bgImageType >= 2) && (bgImageName != "");
    
    if (includeTitle) {
		doc.write("<DIV class=\"titleBg\" style=\"position:relative; left:0; top:0\">");
		writeTitle(doc);
		doc.write("</DIV>");
    }
    
    if (includeBgImage) {
		if (includeTitle)
			doc.write("<DIV style=\"position:relative; left:0; top:0\">");
		if (!isIPhone) {
			doc.write("<img name=\"pageBgImage\" src=\"" + baseURL + "backgrounds/" + bgImageName + "\" style=\"left:" + bgImageXOffset + 
					  "; top:0; z-index: -1; position:absolute; cursor:pointer\"  onclick=\"parent.parent.gotoPage(1, -1);\"");
		}
		else {
			doc.write("<img name=\"pageBgImage\" src=\"" + baseURL + "backgrounds/" + bgImageName + "\" style=\"" + bgImageSize(0) + 
					  "; z-index: -1; position:absolute; cursor:pointer\"  onclick=\"parent.parent.gotoPage(1, -1);\"");
		}
		if (pageNumber == 0) {
			doc.write(" onmousemove=\"window.status='Click to enter notebook'; return true;\"");
		}		
		doc.write(">");
    }
    
    if (selectedEntryId >= 0) {
        var entry = allEntries["id"+selectedEntryId];
        if (entry) {
		    entry.selected = true;
		    entry.selectedText = selectedText;
        }
    }
    
    if (pageNumber == 0) {
		doc.write("<div onclick=\"parent.parent.gotoPage(1, -1);\" onmousemove=\"window.status='Click to enter notebook'; return true;\" style=\"width:100%; height:100%; cursor:pointer\" >");
    }
        
    var s = "";
    for (i = 0; i < topEntries.length; i++) {
		s += topEntries[i].getHTML(0, doc);
    }
    doc.write(s);
    
    if (pageNumber == 0) {
		if ((showCoverTitle != 0) || (showCoverRevision != 0)) {
			doc.write("<TABLE class=\"coverTitleTableStyle\">");
			if (showCoverTitle != 0) {
                s = "<TR><TD";
                if (isIPad)
                    s += " style=\"-webkit-text-size-adjust:" + Math.round(107.0 * fontScaling) + "%\"";
                s += "><DIV onclick=\"parent.parent.gotoPage(1, -1);\" class=\"coverTitleStyle\">" + coverText + "</DIV>";
                doc.write(s);
            }
			if (showCoverRevision != 0) {
                s = "<TR><TD";
                if (isIPad)
                    s += " style=\"-webkit-text-size-adjust:" + Math.round(107.0 * fontScaling) + "%\"";
				s += "><DIV onclick=\"parent.parent.gotoPage(1, -1);\" class=\"coverRevisionStyle\">" + revision + "</DIV>";
                doc.write(s);
            }
			doc.write("</TABLE>");
		}
		
		if (showCoverBadge != 0) {
			if (!isIPhone)
				doc.write("<DIV style=\"position:absolute; top: 85%; width: 98%; text-align: center\"><a href=\"http://www.aquaminds.com\" target=\"aminds\"><img src=\"" + baseURL + "backgrounds/badge.png\" border=0></a></div>");
			else
				doc.write("<DIV style=\"position:absolute; top: 375; width: 98%; text-align: center\"><a href=\"http://www.aquaminds.com\" target=\"_top\"><img src=\"" + baseURL + "backgrounds/badge.png\" border=0></a></div>");
		}
		doc.write("</DIV>");
    }
    
    if (includeTitle && includeBgImage) {
		doc.write("</DIV>");
    }
    
    if (entirePage) {
        doc.write('</body></html>');
    }
    doc.close();
    if (entirePage)
        loadStyles(doc, 0);

    doc.body.className = "pageBackgroundStyle";
    doc.body.onresize=bodyResized;
    if (pageNumber == 0) {
		doc.body.onmousemove=function() {window.status='Click to enter notebook';};
    }
    if (useTouch) {
		doc.body.ontouchstart=function(event) {touchstart(event);};
        doc.body.ontouchmove=function(event) {touchmove(event);};
     	doc.body.ontouchend=function(event) {touchend(event);};
    }
}


