
//
// DOM Emulation
//

var GS_UI = new Array();

GS_UI.splitArticlePages = false;


GS_UI.noOp = function ( value ) {
  return true; // Pass event handling normally
}

// If it's not a DOM Event-aware browser
if (!window.addEventListener) {
  // we simulate it
  GS_UI.onloadHandlers = new Array();
  window.addEventListener = function (type, handler, bubble) {
    if (type != "load") return; // not supported
    // unfortunately, some browsers don't have Array.push()
    GS_UI.onloadHandlers[GS_UI.onloadHandlers.length] = handler;
  };
  window.onload = function ( e ) {
    // the onloadHandlers array should exist if init() is called
    for (var i=0; i<GS_UI.onloadHandlers.length; ++i) {
      GS_UI.onloadHandlers[i](e); // treat the entry as a function object
    }
  };
}

//
// Content dynamism
//

// If it's a DOM-aware browser
if (GS_UI.splitArticlePages && document.getElementById) {

  var setPage = function ( e ) {

    var linkTarget = e.target;
    while (linkTarget != null) {
      if (linkTarget.nodeType == 1) { // Element node
        if (linkTarget.tagName == "A") {
          linkTarget = linkTarget.href;
          break;
        }
      }
      linkTarget = linkTarget.parentNode;
    }
    var i;
    var currentPage;
    if (linkTarget.indexOf("#singlepage") != -1) {
      for (i=0; i<GS_UI.pages.length; ++i) {
        GS_UI.pages[i].style.display = "block";
      }
    } else {
      var pageIndex = linkTarget.indexOf("#page");
      if (pageIndex != -1) {
        var viewPage = linkTarget.substr(pageIndex + "#page".length);
        for (i=0; i<GS_UI.pages.length; ++i) {
          GS_UI.pages[i].style.display = (viewPage == (i + 1)) ? "block" : "none";
        }
      } else { // invalid
        throw "Invalid entry called setPage()";
      } 
    }
    return true;
  };

  var articleInit = function ( e ) {

    var pagelist = document.getElementById("pagelist");
    var pages = pagelist.getElementsByTagName("LI");
    var numPages = pages.length;

      if (document.location.href.indexOf("#singlepage") == -1) {
        var currentPageIndex = document.location.href.indexOf("#page");
        var currentPageNum = (currentPageIndex == -1) ? 1 :
              document.location.href.substr(currentPageIndex + "#page".length);

        // Make a page id placeholder so that page views start from the top of
        //  the page instead of just below the logo, clean up whitespace,
        //  and make only the requested page visible.
        GS_UI.pages = new Array();
        for (var i=1; i<=numPages; ++i) {
          var nextPage = document.getElementById("page" + i);
          GS_UI.pages.push(nextPage);
          var id = nextPage.getAttribute("id");
          nextPage.removeAttribute("id");
          var placeholder = document.createElement("span");
          nextPage.appendChild(placeholder);
          placeholder.setAttribute("id", id);
          placeholder.className = "placeholder";
          if (currentPageNum != i) {
            nextPage.style.display = "none";
          }
        }

      }

      // Now update the page controls
      var listitem;
      var link;
      for (i=0; i<numPages; ++i) {
        link = pages.item(i).firstChild;
        while (link != null && link.tagName != "A") {
          link = link.nextSibling;
        }
        link.addEventListener("click", setPage, false);
      }

    // Allow users to see all pages at once
    if (numPages > 1) {
      listitem = document.createElement("li");
      link = document.createElement("a");
      link.setAttribute("href", "#singlepage");
      link.appendChild(document.createTextNode("Single Page"));
      link.addEventListener("click", setPage, false);
      listitem.appendChild(link);
      pages.item(1).parentNode.appendChild(listitem);
    }
  };
  window.addEventListener("load", articleInit, true);
}

