function slideshow(elementid, imageArr, dofade, timeForFrame, timeForFade){
  var maindiv = document.getElementById(elementid);
  if (!maindiv){return}
  this.images = new Array();
  for (var i=0;i<imageArr.length;++i){
    var imgelem = document.createElement('img');
    imgelem.src = imageArr[i];
    imgelem.style.position = 'absolute';
    imgelem.style.top = '0';
    imgelem.style.left = '0';
    imgelem.style.display = i == 0 ? 'block' : 'none';
    maindiv.appendChild(imgelem);
    this.images.push(imgelem);
  }
  if (imageArr.length < 2){return}
  if (timeForFrame == null){timeForFrame = 1000;}
  if (timeForFade == null){timeForFade = timeForFrame * 0.25}
  if (timeForFade > timeForFrame * 0.5){timeForFade = timeForFrame * 0.5}
  this.timeForFrame = timeForFrame;
  this.timeForFade = timeForFade;
  this.currentFrame = 0;
  this.lastShownFrame = -1;
  this.thisFrameBegin = this.getTimeMs();
  this.dofade = dofade;
  var me = this;
  mytimeout = setInterval(function(){ me.onFrame(); }, 50);
}

slideshow.prototype.calculateFade = function(now){
  var fadeintime = now - this.thisFrameBegin;
  var currentImageAlpha = 1.0;
  var lastImageAlpha = 1.0;
  if (this.dofade && fadeintime >= 0 && fadeintime <= this.timeForFade){
    if (fadeintime != 0){currentImageAlpha = fadeintime / this.timeForFade}else{currentImageAlpha = 0}
    lastImageAlpha = 1.0 - currentImageAlpha;    
  }else{
    lastImageAlpha = 0;
    currentImageAlpha = 1;
  }
  this.setAlpha(currentImageAlpha, this.images[this.currentFrame]);
  if (this.lastShownFrame != -1){this.setAlpha(lastImageAlpha, this.images[this.lastShownFrame])}
}

slideshow.prototype.onFrame = function(){
  var now = this.getTimeMs();
  var delta = now - this.thisFrameBegin;
  if (delta > this.timeForFrame){
    this.thisFrameBegin = now;
    this.gotoNextFrame();
  }else{
    this.calculateFade(now);
  }
}

slideshow.prototype.setAlpha = function(alpha, img){
  img.style.filter="Alpha(Opacity="+(alpha*100)+")";
  img.style.MozOpacity = alpha;
  img.style.opacity = alpha;
}

slideshow.prototype.getTimeMs = function(){
  return (new Date()).getTime();
}

slideshow.prototype.gotoNextFrame = function(){
  if (this.lastShownFrame != -1){this.images[this.lastShownFrame].style.display = 'none'}
  this.lastShownFrame = this.currentFrame;
  ++this.currentFrame;
  if (this.currentFrame >= this.images.length){this.currentFrame = 0}
  this.images[this.currentFrame].style.display = 'block';
  this.images[this.lastShownFrame].style.display = 'block';
  this.thisFrameBegin = this.getTimeMs();
  this.calculateFade(this.thisFrameBegin);
}

slideshow.prototype.gotoPreviousFrame = function(){
  if (this.lastShownFrame != -1){this.images[this.lastShownFrame].style.display = 'none'}
  this.lastShownFrame = this.currentFrame;
  --this.currentFrame;
  if (this.currentFrame < 0){this.currentFrame = this.images.length - 1}
  this.images[this.currentFrame].style.display = 'block';
  this.images[this.lastShownFrame].style.display = 'block';
  this.thisFrameBegin = this.getTimeMs();
  this.calculateFade(this.thisFrameBegin);
}

