function hideCaption() {
  oBox = document.getElementById('transbox');
  if (oBox) {
    oBox.style.display = 'none';
  }
}

function showPhoto(sImage, sDate, sText, iWidth, iHeight) {
  var oTarget = (document.getElementById('divDisplay') || parent.document.getElementById('divDisplay'));
  if (oTarget) {
    oTarget.innerHTML = '<img src="photographs/' + sImage + '" width="' + iWidth + 
      '" height="' + iHeight + '" border="0" />';
    oTarget.style.clientHeight = iHeight + 'px';  
  }

  oBox = document.getElementById('transbox');
  if (oBox) {
    oBox.style.display = '';
  }  
  var oCaption = document.getElementById('divCaption');   
  if (oCaption) {
    oCaption.innerHTML = sDate + '<br>' + sText;
  }
}

function loadThumbnails(sFolder) {
  ajaxFunction('/photographs/' + sFolder + '.php', 'scroll', function(){afterLoad();});
}

function afterLoad() {
  setScroll();
  var oLink, oImage;
  var oDiv = document.getElementById('scrollme');
  if (oDiv) {
    // Check whether there's a filename stored on the page
    var oPhoto = (document.getElementById('photoName') || parent.document.getElementById('photoName'));
    if (oPhoto && oPhoto.innerHTML.length > 1) {
      var iPos = 0;
      while (iPos < oDiv.childNodes.length && !oImage) {
        oLink = oDiv.childNodes[iPos];
        if (oLink.tagName == 'A' && oLink.firstChild.tagName == 'IMG' && oLink.firstChild.src.indexOf(oPhoto.innerHTML) > 0) {
          oImage = oLink.firstChild;
          oPhoto.innerHTML = "";
        }
        else {
          iPos++;
        }
      }
    }
    // If we've not found an image, get the first one
    if (!oImage) {
      oLink = oDiv.firstChild;
      while(oLink && oLink.tagName != 'A') {
        oLink = oLink.nextSibling;
      }
      if (oLink) {
        oImage = oLink.firstChild;
      }
    }  
    // If we have an image display it
    if (oImage && oImage.tagName == 'IMG') {
      if (document.dispatchEvent) { // W3C
        var oEvent = document.createEvent("MouseEvents");
        oEvent.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, oImage);
        oImage.dispatchEvent(oEvent);
      }
      else if (document.fireEvent) { // IE
        oImage.fireEvent("onclick");
      }   
    }
  }
}

function loadCell(oCell) {
  var oTable = oCell.parentNode.parentNode.parentNode.parentNode;
  if (oTable) {
    var sMonth = (oTable.id == '2007') ? (oCell.parentNode.cellIndex + 11) + '' : (oCell.parentNode.cellIndex + 1) + '';
    while (sMonth.length < 2) {
      sMonth = '0' + sMonth;
    }
    loadThumbnails(oTable.id + '' + sMonth);
    var oList = document.getElementsByTagName('TD');
    if (oList) {
      for (var iPos = 0; iPos < oList.length; iPos++) {
        if (oList[iPos].className == 'active') {
          oList[iPos].className = 'inactive';
          oList[iPos].style.borderTop = '1px #ababab dotted';
        }  
      }
    }
    oTable.rows[0].cells[0].className = 'active'; // Year
    oTable.rows[1].cells[oCell.parentNode.cellIndex].className = 'active'; // Month
    oTable.rows[1].cells[oCell.parentNode.cellIndex].style.borderTop = 'none';
  }
}

// --------------------------------------------------
// Drag procedures
// Note that we need to capture the situation where a
// user drags beyond the edge of the target, which is
// why we temporarily configure the global onMouseUp
// event.
// --------------------------------------------------

//var iOffset;
var iStartPos;

function getChildImage(oParent) {
  if (oParent) {
    var oNode = oParent.firstChild;
    while (oNode && oNode.tagName != 'IMG') {
      oNode = oParent.nextSibling;
    }
  }
  return oNode;
}

function getFirstLink() {
  var oFirst;
  if (oScrollMe) {
    oFirst = oScrollMe.firstChild;
    while (oFirst && oFirst.tagName != 'A') {
      oFirst = oFirst.nextSibling;  
    }
  }
  return oFirst;
}

function getLastLink() {
  var oLast;
  if (oScrollMe) {
    oLast = oScrollMe.lastChild;
    while (!oLast || oLast.tagName != 'A') {
      oLast = oLast.previousSibling;  
    }
  }
  return oLast;
}

function scrollImages(e) {
  var oNode;
  var iLeft = getMousePos(e).left;
  var iMove = iLeft - iStartPos;
  if (iMove > 24) { 
    oScrollMe.insertBefore(getLastLink(), getFirstLink());
    setImageVisibility();
    iStartPos = iLeft;
  }
  else if (iMove < -24) { 
    oScrollMe.appendChild(getFirstLink()); 
    setImageVisibility();
    iStartPos = iLeft;
  }
}

function showPos(e, sContext) {
  var ptMouse = getMousePos(e);
  var oDiv = obj('temp');
  oDiv.innerHTML = oDiv.innerHTML + ', ' + sContext + '=' + ptMouse.left;
}

function mouseDown(e) {
  iStartPos = getMousePos(e).left;
  document.onmousemove = mouseMove;
  document.onmouseup = mouseUp;
//  oScrollMe.style.cursor = 'move';
  return false;
}

function mouseUp(e) {
  document.onmousemove = null;
  document.onmouseup = null;
//  oScrollMe.style.cursor = 'auto';
}

function mouseMove(e) {
  scrollImages(e);
  return false;
}

// --------------------------------------------------
// Scroll routines
// --------------------------------------------------

var scrollTime;
var bPause;
var iSpeed;
var oScrollMe;

//window.onload = setScroll;

function setScroll(){
  // First cancel any planned activity
  if(scrollTime) {
    clearTimeout(scrollTime);
  }  

  var oOuter = document.getElementById('scroll');
  oScrollMe = document.getElementById('scrollme');
  
  // We can't do anything if the page doesn't have the right controls
  if (oOuter && oScrollMe) {
    var iOuterWidth = parseInt(oOuter.offsetWidth);
    var iInnerWidth = parseInt(oScrollMe.offsetWidth);

    // Set the attributes and methods
    oOuter.style.overflow = 'hidden';
    oScrollMe.style.float = 'left';
    oScrollMe.style.position = 'relative';
    oScrollMe.style.cursor = 'move';

    // If the content fits, we don't need to scroll
    if (iInnerWidth > 579) {
      oScrollMe.onmouseover = mouseIn;
      oScrollMe.onmouseout = mouseOut;
      iSpeed = 2000;
      bPause = false;
      setScrollTime();
    }  
  }  
}

function mouseIn() {
  bPause = true;
  document.onmousedown = mouseDown;  
}

function mouseOut() {
  document.onmousedown = null;
  bPause = false;
  setScrollTime();
}

function setScrollTime() {
  if(scrollTime) {
    clearTimeout(scrollTime);
  }  
  scrollTime = setTimeout('scrollStep()', iSpeed);
}

function setImageVisibility() {
  var oNode;
  for (var iPos = 0; iPos < oScrollMe.childNodes.length; iPos++) {
    if (oScrollMe.childNodes[iPos].tagName == 'A') {
      oNode = oScrollMe.childNodes[iPos].firstChild;
      if (oNode && oNode.tagName == 'IMG') {
        oNode.style.visibility = ((oNode.offsetLeft + oNode.offsetWidth) < 540) ? 'visible' : 'hidden';
      }
    }
  }
}

function scrollStep() { 
  // If we're pausing, do nothing
  if (bPause) {
    return;
  }
  if (oScrollMe) {
    var oFirst;
    while (!oFirst || oFirst.tagName != 'A') {
      oFirst = oScrollMe.firstChild;  
      oScrollMe.appendChild(oFirst);
    } 
    setImageVisibility();
  }
  setScrollTime();
}
