// Global variables for Alaska07
var strCaption;

// Generic onload function
function init() {
  SetupPhotos();
  var ifs = document.getElementsByTagName("iframe");
  for (var i=0; i<ifs.length; i++) {
    SetupNavButtons(ifs[i].id);
  }
  sNav("first", ifs[0].id);
}
if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);
else setTimeout(init, 2000);

function SetupPhotos() {
  var el, els, i;
  els = getElementsByClassName("div.thumbs", "");
  if (els.length == 0) return;
  // add event handlers to parent divs, event bubbles up
  for (i=0; i<els.length; i++) els[i].onclick = ShowPhoto;
}
function SetupNavButtons(ifId) {
  var divNew, spanNew;
  divNew = document.createElement("div");
  divNew.setAttribute("id", "buttons_"+ifId);
  divNew.className = "buttons";
  document.getElementById(ifId).parentNode.insertBefore(divNew, document.getElementById(ifId).nextSibling);
  spanNew = document.createElement("span");
  spanNew.setAttribute("action", "first");
  spanNew.innerHTML = "&lt;&lt;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.setAttribute("action", "prev");
  spanNew.innerHTML = "&nbsp;&lt;&nbsp;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.setAttribute("action", "play");
  spanNew.innerHTML = "Play";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.setAttribute("action", "next");
  spanNew.innerHTML = "&nbsp;&gt;&nbsp;";
  divNew.appendChild(spanNew);
  spanNew = document.createElement("span");
  spanNew.setAttribute("action", "last");
  spanNew.innerHTML = "&gt;&gt;";
  divNew.appendChild(spanNew);
  divNew.onclick = ProcessButton;
}

// 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
  doc.location.replace(thumb.parentNode.href); //Navigate iframe w/o history
  return true;
}
function ProcessButton(e) {
  var e, el, ifId;
  // find target (target is span in <div>, not the <div> 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() == "SPAN"
      && el.parentNode.nodeName.toUpperCase() == "DIV") {
    ifId = el.parentNode.parentNode.getElementsByTagName("iframe")[0].id;
    sNav(el.getAttribute("action"), ifId);
  }
}
function sNav(str, strId) {
  var thumbdiv, tlinks, frsrc, ifr, btn;
  // Get array of image links
  thumbdiv = document.getElementById(strId).parentNode.getElementsByTagName("div")[0];
  tlinks = thumbdiv.getElementsByTagName("a");
  switch (str) { 
    case "play":
      if (!document.all) window.setTimeout(showTime, 100, true, strId);
      else window.setTimeout(function(){showTime(true, strId);}, 100);
      break;
    case "stop":
      clearTimeout(tshow);
      ifr = document.getElementById(strId);
      btn = ifr.nextSibling;
      while (btn.className != "buttons") {btn = btn.nextSibling;}
      btn = btn.getElementsByTagName("span")[2];
      btn.innerHTML = "Play";
      btn.setAttribute("action", "play");
      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(strId).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(strId).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,iframeId) {
  var ifr, btn;
  if (blnFirst) {
    sNav("first", iframeId);
    ifr = document.getElementById(iframeId);
    btn = ifr.nextSibling;
    while (btn.className != "buttons") {btn = btn.nextSibling;}
    btn = btn.getElementsByTagName("span")[2];
    btn.innerHTML = "Stop";
    btn.setAttribute("action", "stop");
  } else {
    var retval = sNav("next",iframeId);
    if (retval == "stop") {
      ifr = document.getElementById(iframeId);
      btn = ifr.nextSibling;
      while (btn.className != "buttons") {btn = btn.nextSibling;}
      btn = btn.getElementsByTagName("span")[2];
      btn.innerHTML = "Play";
      btn.setAttribute("action", "play");
      return;
    }    
  }
  if (!document.all) tshow = window.setTimeout(showTime, 5000, false, iframeId);
  else tshow = window.setTimeout(function(){showTime(false, iframeId);}, 5000);
}
function preLoad(strHref) {
  var plImg = new Image();
  plImg.src = strHref + "&pre=load";
}


function resize(par) {
  // This powers the "^ Taller" control
  var pDiv=par.parentNode.parentNode;
  pDiv.getElementsByTagName('IFRAME')[0].style.height='720px';
  pDiv.getElementsByTagName('DIV')[0].style.height='720px';
  par.parentNode.style.display='none';  
}

// Utility functions
function getFirstChildImg(aNode) {
  var imgs = aNode.getElementsByTagName("img");
  if (imgs.length == 0) return;
  else return imgs[0];
}
function getElementsByClassName(strClass, strID) {
  var strTagName, strClassName, els, i, j
  if (strClass.indexOf(".") > 0) {   // e.g., div.myclass
    strTagName = strClass.split(".")[0];
    strClassName = strClass.split(".")[1];  
  } else {                          // e.g., myclass
    strTagName = "*";
    strClassName = strClass;
  }
  var elArray = new Array();
  if (strID.length > 0) { // limited to children of element
    els = document.getElementById(strID).getElementsByTagName(strTagName);
  } else {                // global
    els = document.getElementsByTagName(strTagName);
  }
  for (i=els.length-1; i>=0; i--) { // collect els that match
    if (els[i].className.indexOf(strClassName) > -1) j = elArray.push(els[i]);
  }
  return elArray;
}
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;
}

