var currentPoem = 1;
var W3CDOM = (document.getElementById && document.createElement);

window.onload = hideOnLoad;

function hideOnLoad() {
  if (!W3CDOM) return;
  addLinks();
  loadPoem(currentPoem);
  changeLinks();
}

/* Hide all the poems except the one specified */
function loadPoem( poemNum ) {
  var i=1;
  var poemDiv = getObj("poem" + frmtInt(i))
  while (poemDiv.obj != null) {
    if (i != poemNum) {
      poemDiv.style.visibility = 'hidden';
      poemDiv.style.overflow = 'hidden';
      poemDiv.style.height = '0';
      poemDiv.style.padding = '0';
      poemDiv.style.border = 'none';
    } else {
      poemDiv.style.visibility = 'visible';
      poemDiv.style.overflow = 'auto';
      poemDiv.style.height = 'auto';
      poemDiv.style.padding = '0 0 13px 0';
      poemDiv.style.borderBottom =  "dashed 1px #ccc";
    }
    i++;
    poemDiv = getObj("poem" + frmtInt(i));
  }
  currentPoem = poemNum;
  
  // If there is no next poem then make the next poem link grey
  if (getObj("poem" + frmtInt(poemNum+1)).obj == null)
    getObj("nxtLnk").style.color = "#CCC";
  else
    getObj("nxtLnk").style.color = "#005447";

  // If there is no previous poem then make the previous poem link grey
  if (getObj("poem" + frmtInt(poemNum-1)).obj == null)
    getObj("prvLnk").style.color = "#CCC";
  else
    getObj("prvLnk").style.color = "#005447";
}

/* Show the next poem */
function nextPoem() {
  var nxt = currentPoem + 1;
  var nextPm = getObj("poem" + frmtInt(nxt));
  if (nextPm.obj != null) loadPoem(nxt);
}

/* Show the previous poem */
function prevPoem() {
  var prv = currentPoem - 1;
  var prevPm = getObj("poem" + frmtInt(prv));
  if (prevPm.obj != null) loadPoem(prv);
}

/* Change the sidebar links to hide/show links */
function changeLinks() {
  var i=1;
  var lnk = getObj("link" + frmtInt(i));
  while (lnk.obj) {
    lnk.obj.href = "javascript:loadPoem(" + i + ");";
    i++;
    lnk = getObj("link" + frmtInt(i));
  }
}

/* Add links for forward and next */
function addLinks() {
  x = document.getElementById("textDiv");
  newDiv = document.createElement("div");
  newDiv.className = "poetryNav";
  newDiv.style.textAlign = "center";
  newDiv.style.fontFamily = "\"Trebuchet MS\", Trebuchet, sans-serif";
  newDiv.innerHTML = "<span id=\"prev\"><a id=\"prvLnk\" href= \"javascript:prevPoem();\">prev</a></span><span id=\"next\"><a id=\"nxtLnk\" href=\"javascript:nextPoem();\">next</a></span><a id=\"upLnk\" href=\".\">up</a>";
  x.appendChild(newDiv);
  getObj("upLnk").style.color = "#005447";
}

function frmtInt( i ) {
  if (i.toString().length == 1) {
    return "0" + i.toString();
  } else
    return i;
}

/* Get an object */
function getObj(name)
{
  if (document.getElementById)
  {
  	if (this.obj = document.getElementById(name))
	  this.style = document.getElementById(name).style;
  }
  return this;
}


