function FlimmerBilder(theWindow, theId) {
  this.images_ = [];
  this.imagePathes_ = [];
  this.loadedCtr_ = 0;
  this.window_ = theWindow;
  this.thisId_ = "__FlimmerBilder__this__" + (new Date().valueOf());
  this.window_[this.thisId_] = this;

  this.currImage_ = -1;
  this.maxImages_ = -1;
  this.currImageState_ = -1;
  this.maxImageStates_ = 3;
  this.timeout_ = 100;
  this.stripe_ = 1;

  this.image_ = this.window_.document.getElementById(theId);
  this.width_ = this.image_.width;
  this.height_ = this.image_.height;

  this.dummies_ = [];
  this.type_ = 1;

}

FlimmerBilder.prototype._checkObject = function (theObject) {
  var str = "";
  for (var i in theObject) {
    str += i + " (" + (typeof theObject).substr(0, 1) + ") ";
    
  }
  alert(str);
}

FlimmerBilder.prototype._setImageStyle = function (theImageNumber, theStyleName, theStyleValue) {
  this.dummies_[theImageNumber].style[theStyleName] = theStyleValue;
}

FlimmerBilder.prototype._setOneImageFinished = function () {
  this.loadedCtr_ ++;

  if (this.loadedCtr_ < this.images_.length) {
    return;
  }

  this.maxImages_ = this.images_.length;
  this.images_[this.images_.length] = this.images_[0];
  
  this.currImage_ = 0;
  this.currImageState_ = 0;

  this["_next" + this.type_]();
}

FlimmerBilder.prototype._loadImages = function () {
  for (var i = this.imagePathes_.length - 1; i --;) {
    var img = new Image(this.width_, this.height_);
    img.src = this.imagePathes_[i];
    img.style.position = "absolute";
    img.onload = new Function(this.thisId_ + "._setOneImageFinished()");
    this.images_[this.images_.length] = img;
  }
}

FlimmerBilder.prototype.addImage = function (theImagePath) {
  this.imagePathes_[this.imagePathes_.length] = theImagePath;
}

FlimmerBilder.prototype._writeDummy = function (theId) {
  var id = "_dummy" + theId + "_";

  this.window_.document.writeln("<img id='" + id + "' style='position: absolute; display: none;' />");
  this.dummies_[theId] = this.window_.document.getElementById(id);
}

FlimmerBilder.prototype.start1 = function () {
  this._loadImages();

  this._writeDummy(0);
  this._writeDummy(1);

  this.dummies_[0].src = this.image_.src;
  this.dummies_[1].src = this.image_.src;
  
  this.dummies_[0].width = this.width_;
  this.dummies_[0].height = this.height_;
  this.dummies_[1].width = this.width_;
  this.dummies_[1].height = this.height_;
  
  var t = this._computeOffset(this.image_, "offsetTop");
  var l = this._computeOffset(this.image_, "offsetLeft");

  this._setImageStyle(0, "position", "absolute");
  this._setImageStyle(0, "top", t);
  this._setImageStyle(0, "left", l);
  this._setImageStyle(0, "display", "inline");

  this._setImageStyle(1, "position", "absolute");
  this._setImageStyle(1, "top", t);
  this._setImageStyle(1, "left", l);
  this._setImageStyle(1, "display", "inline");
}

FlimmerBilder.prototype.start2 = function () {
  this.type_ = 2;
  this._loadImages();

  var ot = this._computeOffset(this.image_, "offsetTop");
  var ol = this._computeOffset(this.image_, "offsetLeft");

  for (var i = 0; i < this.images_.length; i ++) { // one image more than added
    this._writeDummy(i);
    this._setImageStyle(i, "top", ot);
    this._setImageStyle(i, "left", ol);
    this._setImageStyle(i, "display", "none");

    this.dummies_[i].src = this.images_[i].src;
  }

}

FlimmerBilder.prototype._next1 = function () {
  this._process();

  var h1 = Math.round(this.currImageState_ / this.maxImageStates_ * this.height_);
  var h2 = this.stripe_ + h1;
  
  this.dummies_[0].src = this.images_[this.currImage_].src;
  this.dummies_[1].src = this.images_[this.currImage_ + 1].src;

  this.dummies_[1].style.clip = "rect(" + h2 + "px " + this.width_ + "px " + this.height_ + "px 0px);";
  this.dummies_[0].style.clip = "rect(0px " + this.width_ + "px " + h1 + "px 0px);";
  
  this.window_.setTimeout(this.thisId_ + "._next1();", this.timeout_);
}

FlimmerBilder.prototype._next2 = function () {
  this._process();

  this._setImageStyle(this.currImage_, "display", "none");
  this._setImageStyle(this.currImage_ < this.maxImages_ - 1 ? this.currImage_ + 1 : 0, "display", "inline");

  this.window_.setTimeout(this.thisId_ + "._next2();", this.timeout_);
}

FlimmerBilder.prototype._process = function() {
  this.currImageState_ ++;
  if (this.currImageState_ >= this.maxImageStates_) {
    this.currImageState_ = 0;
    
    this.currImage_ ++;
    if (this.currImage_ >= this.maxImages_) {
      this.currImage_ = 0;
    }
  }
}

FlimmerBilder.prototype._computeOffset = function (elem, kind)
{
  var off = elem[kind];
  while ((elem = elem.offsetParent) != null) {
    if (elem.tagName=="BODY")
      break;
    off += elem[kind];
  }

  // Correction for iMac MSIE
  if (navigator.userAgent.toLowerCase().indexOf("mac") != -1 &&
      navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
    if (kind == "offsetLeft")
      off += parseInt(this.window_.document.body.leftMargin);
    else
      off += parseInt(this.window_.document.body.topMargin);
  }
  return off;
}

