// Copyright (c) 2002-2004 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 showCoverBadge      = 0;
var revision            = "";

var isIE = (window.navigator.appName.toUpperCase().indexOf("EXPLORER") >= 0);
var isNetscape = !isIE && (window.navigator.appName.toUpperCase().indexOf("NETSCAPE") >= 0) && 
((window.navigator.vendor.toUpperCase().indexOf("NETSCAPE") >= 0) || 
 (window.navigator.vendor.toUpperCase().indexOf("CAMINO") >= 0) ||
 (window.navigator.vendor.toUpperCase().indexOf("FIREFOX") >= 0));

// Following functions emulate the new encodeURI(), for use in older browsers
function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~*'();/?:@&=+$,%";

function encodeURINew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function ntEncodeURI(s)
{
	if ((typeof encodeURI == "function") && !isIE) {
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		return encodeURI(s);
	}
	else {
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		return encodeURINew(s);
	}
}

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("?");

    if (!isNetscape) {
        location.href = ntEncodeURI((i >= 0) ? location.href.substring(0, i) : location.href)  + "?" + pageNum + ((entryId < 0) ? "" : ("#" + entryId));
    }
    else {
        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() {
    gotoPage((pageNumber == (hasCoverPage ? 0 : 1)) ? notebookPageCount : (pageNumber - 1), -1);
}

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

function go(pn, en) {
    parent.gotoPage(pn, en);
}

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('<META http-equiv=Content-Type content="text/html; charset=UTF-8">');
    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=\"" + ((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>");
    doc.write('<META http-equiv=Content-Type content="text/html; charset=UTF-8">');
    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";
    }
}

