// Global variables
var strCaption, tshow;

// Global functions

// Generic onload function
function init() {
  SetupPhotos();
  SetupNavButtons();
  sNav("first");
}

function SetupPhotos() {
  var el, els, i;
  el = document.getElementById("thumbs");
  // add event handler to parent div, event bubbles up
  el.onclick = ShowPhoto;
  // add title attributes
  els = el.getElementsByTagName("img");
  for (i=0; i<els.length; i++) {
    els[i].setAttribute("title",els[i].getAttribute("alt"));
  }
/*  el = document.getElementById("home");
  var inpNew = document.createElement("input");
  inpNew.setAttribute("id", "chkBack");
  inpNew.setAttribute("type", "checkbox");
  var txtNew = document.createTextNode(" Enable Back button for images | ");
  el.insertBefore(txtNew, el.firstChild);
  el.insertBefore(inpNew, el.firstChild);
*/
  el = document.getElementsByTagName("h1")[0];
  var spanNew = document.createElement("span");
  spanNew.appendChild(document.createTextNode(" Enable Back button for images"));
  el.appendChild(spanNew);
  var inpNew = document.createElement("input");
  inpNew.setAttribute("id", "chkBack");
  inpNew.setAttribute("type", "checkbox");
  spanNew.insertBefore(inpNew, spanNew.firstChild);
}
function SetupNavButtons() {
  var divNew, spanNew;
  divNew = document.createElement("div");
  divNew.setAttribute("id", "buttons");
  document.getElementById("photo").parentNode.insertBefore(divNew, document.getElementById("photo").nextSibling);
  spanNew = document.createElement("span");
  spanNew.onclick = function() {sNav('first');};
  spanNew.innerHTML = "&lt;&lt;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.onclick = function() {sNav('prev');};
  spanNew.innerHTML = "&nbsp;&lt;&nbsp;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.onclick = function() {sNav('play');};
  spanNew.innerHTML = "Play";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.onclick = function() {sNav('next');};
  spanNew.innerHTML = "&nbsp;&gt;&nbsp;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.onclick = function() {sNav('last');};
  spanNew.innerHTML = "&gt;&gt;";
  divNew.appendChild(spanNew);
}

// Photo functions
function ShowPhoto(e) {
  var e, el, doc, targ
  // find target (target is image in <a>, not the <a> itself)
  if (!e) e = window.event;
  if (e.target) el = e.target;
  else if (e.srcElement) el = e.srcElement;
  // handle event
  if (e.type == "click" && el.nodeName.toUpperCase() == "IMG"
      && el.parentNode.nodeName.toUpperCase() == "A") {
    if (frNav(el)) return false;   // cancel the default link action
    else return true;              // execute default link action
  }
}
function frNav(thumb) {
  // Update page in iframe
  doc = getNamedIFrame(thumb.parentNode.getAttribute("target")) || 
    getSibIFrameDoc(thumb);  // Returns named or nearest sibling iframed doc
  if (doc == "null") return false;
  strCaption = thumb.getAttribute("alt"); // Read by the iframe
  if (document.getElementById("chkBack").checked)
    doc.location.href=thumb.parentNode.href; //Navigate iframe w/ history
  else
    doc.location.replace(thumb.parentNode.href); //Navigate iframe w/o history
  return true;
}
function sNav(str) {
  var tlinks, frsrc;
  // Get array of image links
  tlinks = document.getElementById("thumbs").getElementsByTagName("a");
  switch (str) { 
    case "play":
      if (!document.all) window.setTimeout(showTime, 100, true);
      else window.setTimeout("showTime(true);", 100);
      break;
    case "first":
      frNav(tlinks[0].firstChild);
      if (tlinks.length>1) {
        preLoad(tlinks[1].href);
      }
      break;
    case "last":
      frNav(tlinks[tlinks.length-1].firstChild);
      break;
    case "prev":
      var pos = -1;
      frsrc = document.getElementById("photo").contentWindow.location.href;
      for (var i=0; i<tlinks.length;i++) {
        if (frsrc == tlinks[i].href) pos = i-1;
      }
      if (pos >= 0) frNav(tlinks[pos].firstChild);
      if (pos-1 >= 0) {
        preLoad(tlinks[pos-1].href);
      }
      break;
    case "next":
      var pos = tlinks.length;
      frsrc = document.getElementById("photo").contentWindow.location.href;
      for (var i=0; i<tlinks.length;i++) {
        if (frsrc == tlinks[i].href) pos = i+1;
      }
      if (pos < tlinks.length) {
        frNav(tlinks[pos].firstChild);
        if (pos+1  < tlinks.length) {
          preLoad(tlinks[pos+1].href);
        }
        break;
      } 
      else return "stop";
  } 
}
function showTime(blnFirst) {
  if (blnFirst) {
    sNav("first");
    var btn = document.getElementById("buttons").getElementsByTagName("span")[2];
    btn.innerHTML = "Stop";
    btn.onclick = function() {
        clearTimeout(tshow); 
        this.innerHTML = "Play";
        this.onclick = function(){sNav('play');};
        };
  } else {
    var retval = sNav("next");
    if (retval == "stop") {
      var btn = document.getElementById("buttons").getElementsByTagName("span")[2];
      btn.innerHTML = "Play";
      btn.onclick = function(){sNav('play');};
      return;
    }    
  }
  if (!document.all) tshow = window.setTimeout(showTime, 5000, false);
  else tshow = window.setTimeout("showTime(false);", 5000);
}
function preLoad(strHref) {
  var plImg = new Image();
  plImg.src = strHref + "&pre=load";
}
function resize(par) { // This powers the "^ Taller" control
  document.getElementById('thumbs').style.height='720px';
  document.getElementById('photo').style.height='720px';
  par.parentNode.style.display='none';  
}
function keys(key)
{
 if (!key) {
  key = event;
  key.which = key.keyCode;
 }
 switch (key.which) {
  case 39: // rightkey
    // Try not to interfere with keyboard equivalent of Forward
    if (key.altKey) return true;
  case 40: // downkey
    sNav("next");
    break;
  case 37: // leftkey
    // Try not to interfere with keyboard equivalent of Back
    if (key.altKey) return true;
  case 38: // upkey
    sNav("prev");
    break;
  case 36: // home
    sNav("first");
    break;
  case 35: // end
    sNav("last");
    break;
 }
 return false;
}
document.onkeyup = keys;

// Utility functions
function getNamedIFrame(sName) { // locate iframe by name
  var ifs, i; 
  ifs = document.getElementsByTagName("iframe");
  for (i=0;i<ifs.length;i++) {
    if (ifs[i].getAttribute("name") == sName) return getIFrameDoc(ifs[i]);
  }
  return null;
}
function getSibIFrameDoc(aNode) {
  var sibs, i
  while (aNode.nodeName.toUpperCase() != "BODY") {
    sibs = aNode.parentNode.childNodes;
    for (i=0; i<sibs.length; i++) {
      if (sibs[i].nodeName.toUpperCase() == "IFRAME") {
        return getIFrameDoc(sibs[i]);
      }
    }
    aNode = aNode.parentNode;
  }
  return null;
}
function getIFrameDoc(aNode) {
  if (aNode.contentDocument) return aNode.contentDocument;
  if (aNode.contentWindow) return aNode.contentWindow.document;
  if (aNode.document) return aNode.document;
  return null;
}

