var secondsPerPic = 3;
var fps = 20;
var step = 0.05;

var currentPic, nextPic;
var elements;
var nextOp;

function initSlideshow() {
   elements = document.getElementsByName("screen");
   nextPic = Math.floor(Math.random() * elements.length);

   for (i = 0; i < elements.length; i++) {
      if (i == nextPic) {
         setPicOpacity(i, 1);
      }
   }

   loadNextPic();
}

function loadNextPic() {
   currentPic = nextPic;
   while ((nextPic = Math.floor(Math.random() * elements.length)) ==
         currentPic);

   nextOp = 0;
   setTimeout("slide()", secondsPerPic * 1000);
}

function slide() {
   if (nextOp + step >= 1) {
      setPicOpacity(currentPic, 0);
      setPicOpacity(nextPic, 1);
      loadNextPic();
   } else {
      nextOp += step;
      setPicOpacity(currentPic, 1 - nextOp);
      setPicOpacity(nextPic, nextOp);
      setTimeout("slide()", 1000 / fps);
   }
}

function setPicOpacity(pic, opacity) {
   elements[pic].style.opacity = opacity;
   elements[pic].style.filter = "alpha(opacity=" + opacity * 100 + ")";
}


