// Setting the index to "-1" will make the "0" image display first since
// the index is incremented before the image is displayed:
var iImgIndex=-1;

// An object array that will track the list of imgFrame divs:
var aImgDivs=new Array();

// Gather the list of div objects that contain the 
// images that we will cycle through:
function getImgDivs() {
  var inc=0;
  var alltags=document.all ? document.all.tags("DIV") : document.getElementsByTagName("*");
  for (i=0; i<alltags.length; i++) {
    if (alltags[i].className=="imgFrame")
      aImgDivs[inc++]=alltags[i];
  }
}

// Hide all of the image frames:
function hideAllImgDivs() {
  var inc=0;
  while (aImgDivs[inc]) {
    aImgDivs[inc].style.display="none";
    inc++;
  }
}

function cycleImage(sDirection) {
  if (sDirection == "prev")
    iImgIndex=(iImgIndex==0) ? aImgDivs.length-1 : iImgIndex-1;
  else
    iImgIndex=(iImgIndex==aImgDivs.length-1) ? 0 : iImgIndex+1;

  if (document.all) {
    imgDesc.innerHTML=aImgDesc[iImgIndex];
    
    var oSelectedImgDiv=aImgDivs[iImgIndex];
    hideAllImgDivs();
    oSelectedImgDiv.style.display="block";
  }

  if (sDirection == "auto")
    setTimeout("cycleImage('auto')", 5000);
}

function initImages() {
  getImgDivs();

  /* The first image will be randomly selected from the full list.  It has
  a base value of -1 since the value is incremented before the image is
  selected to be displayed. */
  iImgIndex = Math.floor(Math.random() * aImgDivs.length-1); 
    
  cycleImage('auto');
}

if (window.addEventListener)
  window.addEventListener("load", initImages, false);
else if (window.attachEvent)
  window.attachEvent("onload", initImages);
else if (document.getElementById)
  window.onload=initImages;
