var fading = {
  oldWinOnLoad: false,
  inits: new Array(),
  faders: new Object(),

  init: function (einstellungen) {
    var fader;
    if (this.inits) {
      this.inits[this.inits.length] = einstellungen;
    } else {
      fader = new this.Fader(einstellungen);
      if (fader != false && !this.faders[einstellungen.id]) {
        this.faders[fader.id] = fader;
        window.setTimeout(function () {fader.next();}, 5000);
      }
    }
  },

  start: function () {
    this.oldWinOnLoad = window.onload;
    window.onload = function () {
      if (typeof(fading.oldWinOnLoad) == "function") fading.oldWinOnLoad();
      fading.onload();
    };
  },

  onload: function () {
    var i, fader, e, scripts = document.getElementsByTagName("script");
    for (i = 0; i < scripts.length; i++) {
      if (scripts[i].src && scripts[i].src.match(/(^|\/)fading\.js$/)) {
        this.baseURL = scripts[i].src.replace(/(^|\/)fading\.js$/, "");
      }
    }
    if (this.baseURL) {
      e = document.createElement("link");
      e.type = "text/css";
      e.rel = "stylesheet";
      e.href = this.baseURL + "/../style/fading.css";
      document.getElementsByTagName("head")[0].appendChild(e);
    }
    fader = this.inits;
    delete this.inits;
    for (i = 0; i < fader.length; i++) this.init(fader[i]);
  },

  Fader: function (einstellungen) {
    if (!einstellungen.id || !document.getElementById(einstellungen.id)
        || fading.faders[einstellungen.id]
        || einstellungen.images.length < 2)  return false;
    var i, original = document.getElementById(einstellungen.id);
    this.id = einstellungen.id;
    this.images = new Array();
    this.counter = false;
    this.element = document.createElement("span");
    this.element.className = "fader";
    original.parentNode.replaceChild(this.element, original);
    for (i = 0; i < einstellungen.images.length; i++) {
      this.images[i] = document.createElement("img");
      this.images[i].src = einstellungen.images[i];
      this.images[i].alt = "";
      if (i == 0) this.element.appendChild(this.images[i]);
    }
    this.fade = function (step) {
      var fader = this, imgs = this.element.getElementsByTagName("img");
      step = (!step) ? 0 : step;
      imgs[1].style.opacity = step/100;
      imgs[1].style.filter = "alpha(opacity=" + step + ")"; // IE?
      step = step + 2;
      if (step <= 100) {
        window.setTimeout(function () { fader.fade(step); }, 1);
      } else {
        imgs[1].className = "";
        this.element.removeChild(imgs[0]);
        window.setTimeout(function () { fader.next(); }, 5000);
      }
    };
    this.next = function () {
      this.counter = (this.counter < this.images.length -1) ? this.counter +1 : 0;
      this.element.appendChild(this.images[this.counter]);
      this.images[this.counter].className = "next";
      this.fade();
    };
  }
};

fading.start();
