var sections = ["one","two","three"];
var pageCharacterLimit = 1600;
var numParagraphs = 0;
var pageCount = 1;
var currentPage = 1;
var pageIntervals;

function newinit() {
    if (document.all && document.getElementById) {
      for (var i=0; i < sections.length; i++) {
         newInitMouseEvents(sections[i]);
      }
   }
}

function newInitMouseEvents(s) {
   var el = document.getElementById("newmenucontainer" + s);
   el.onmouseover = function () {
      this.className += " newjshover" + s;
   }
   el.onmouseout = function () {
      this.className = this.className.replace(" newjshover" + s,"");
   }
}


function init() {
   if (document.all && document.getElementById) {
      for (var i=0; i < sections.length; i++) {
         initMouseEvents(sections[i]);
      }
   }
   hideParagraphs();
}

function initMouseEvents(s) {
   var el = document.getElementById("sectionhead" + s);
   el.onmouseover = function () {
      this.className += " jshover" + s;
   }
   el.onmouseout = function () {
      this.className = this.className.replace(" jshover" + s,"");
   }
}

//http://www.oreilly.com/catalog/jscript4/chapter/ch17.html
if (!window.Node) {
    var Node = {            // If there is no Node object, define one
        ELEMENT_NODE: 1,    // with the following properties and values.
        ATTRIBUTE_NODE: 2,  // Note that these are HTML node types only.
        TEXT_NODE: 3,       // For XML-specific nodes, you need to add
        COMMENT_NODE: 8,    // other constants here.
        DOCUMENT_NODE: 9,
        DOCUMENT_FRAGMENT_NODE: 11
    }
}

function hideParagraphs() {
   var charCount = 0;
   var node = document.getElementById("paragraphs");
   if (!node) {
      return;
   }
   var paragraphs = node.childNodes;
   var n = paragraphs.length;
   for (var i=0; i < n; i++) {
      if (paragraphs[i].nodeType == Node.ELEMENT_NODE) {
         numParagraphs++;
      }
   }
   
   var pagebreak = new Array(numParagraphs);
   var numBreaks = 0;
   for (var i=0,p=0; i < n; i++) {
      var children = paragraphs[i].childNodes;
      var nc = children.length;
      
      for (var j=0; j < nc; j++) {
         var currentNode = children[j];
         if (currentNode.nodeType == Node.TEXT_NODE) {
            var text = currentNode.data;
            if (text.length > 0) {
               charCount += text.length;
               //alert("Paragraph: " + p + "\n" + "Count: " + charCount);
               if (charCount > pageCharacterLimit && i < n - 1) {
                  pagebreak[p + 1] = true;
                  numBreaks++;
                  charCount = 0;//text.length;
               }
               p++;
            }
         }
      }
   }
   
   //create pager
   var pagernode = document.getElementById("pager");
   var pagerhtml = "";
   var lastbreak = 0;
   var firstbreak = 0;
   pageIntervals = new Array(numBreaks);
   
   for (var i=0; i<numParagraphs; i++) {
      if (pagebreak[i]) {
         if (pageCount > 1) {
            pagerhtml += " | ";
         }
         if (pageCount == 1) {
            firstbreak = i;
         }
         var interval = new Array(2);
         interval[0] = lastbreak;
         interval[1] = i;
         pageIntervals[pageCount - 1] = interval;
         //pagerhtml += "<a id=\"pager" + pageCount + "\" href=\"#top\" onclick=\"javascript:showParagraphs(" + pageCount + "," + lastbreak + "," + i + ")\">Page " + pageCount + "</a>";
         pagerhtml += "<a id=\"pager" + pageCount + "\" href=\"#top\" onclick=\"javascript:showPage(" + pageCount + ")\">Page " + pageCount + "</a>";
         lastbreak = i;
         pageCount++;
      }
   }
   if (pageCount > 1) {
      var interval = new Array(2);
      interval[0] = lastbreak;
      interval[1] = i;
      pageIntervals[pageCount - 1] = interval;
      pagerhtml += " | <a id=\"pager" + pageCount + "\" href=\"#top\" onclick=\"javascript:showPage(" + pageCount + ")\">Page " + pageCount + "</a>";
      pagerhtml += " | <a id=\"pagerall\" href=\"#top\" onclick=\"javascript:showAll()\">All</a>";
   }
   pagernode.innerHTML = "<p>" + pagerhtml + "</p>";
   //alert(pagerhtml);
 
   if (firstbreak > 0) {
      //showParagraphs(1, 0, firstbreak);
      showPage(1);
   }
}

function showAll() {
   for (var i=1; i<=pageCount; i++) {
      document.getElementById("pager" + i).style.fontWeight = "normal";
   }
   for (var i=1; i<=numParagraphs; i++) {
      document.getElementById("paragraph" + i).style.display = "block";
   }
   document.getElementById("pagerall").style.fontWeight = "bold";
}

function showPage(page) {
   var start = pageIntervals[page - 1][0];
   var end = pageIntervals[page - 1][1];
   document.getElementById("pagerall").style.fontWeight = "normal";
   for (var i=1; i<=pageCount; i++) {
      if (page == i) {
         document.getElementById("pager" + i).style.fontWeight = "bold";
      } else {
         document.getElementById("pager" + i).style.fontWeight = "normal";
      }
   }
   for (var i=0; i<numParagraphs; i++) {
      elementName = "paragraph" + (i + 1);
      var el = document.getElementById(elementName);
      if (i >= start && i < end) {
         el.style.display = "block";
      } else {
         el.style.display = "none";
      }
   }
}
