window.addEvent('domready',function() {
  /* settings */
  var firstDuration = 5000;
  var showDuration = 7500;
  var container = $('slideshow-container');
  var images = container.getElements('img');
  var currentIndex = 0;
  var newIndex = currentIndex;
  var interval;
  var count=0;

  /* opacity and fade */
  images.each(function(img,i){ 
    if(i > 0) {
      img.set('opacity',0);
    } else if (i == 0) {
      img.set('opacity',1);
    }
  });
  /* worker */
  var show = function() {
    count++;
    images.set('tween', {duration: '4000'});
    images[currentIndex].fade('out');
    //currentIndex = $random(0, (images.length - 1));
    while (newIndex == currentIndex) {
      newIndex = $random(0, (images.length - 1));
    }
    images[newIndex].fade('in');
    currentIndex = newIndex;
    if(count == 1) {
      $clear(interval);
    }
  };
  
  var pause = function() {
      interval = show.periodical(showDuration);
  }
  /* start once the page is finished loading */
  window.addEvent('load',function(){
    interval = show.periodical(firstDuration); //do the transition between the first two images

    // now do the transition for the rest of the images, this needs to be seperated because the first image does not include the long fade in its duration
    var interval2 = pause.delay(firstDuration);
  });
});
