var fwk = {
	className: "fader",
	viewTime: 5000,
	fadeStep: 1,
	random: false,
	autostart: true,
	baseURL: "",
	oldWinOnLoad: null,
	inits: new Array(),
	faders: new Object(),
	start: function () {
		this.oldWinOnLoad = window.onload;
		window.onload = function () {
			if (typeof(fwk.oldWinOnLoad) == "function") {
			}
			fwk.onload();
		};
	},
	onload: function () {
		var i, fader, css, scripts = document.getElementsByTagName("script");
		for (i = 0; i < scripts.length; i++) {
			if (scripts[i].src && scripts[i].src.match(/fader\.js/)) {
				this.baseURL = scripts[i].src.replace(/(^|\/)fader\.js$/, "");
			}
		}
		if (this.baseURL) {
			css = document.createElement("link");
			css.rel = "stylesheet";
			css.type = "text/css";
			css.href = this.baseURL + "/fader.css";
			document.getElementsByTagName("head")[0].appendChild(css);
		}

		fader = this.inits;
		delete this.inits;
		for (i = 0; i < fader.length; i++) {
			this.init(fader[i]);
		}
	},
	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;
				if (fader.autostart) {
					window.setTimeout(function () {	fader.start(); }, fader.viewTime);
				}
			}
		}
	},
	Fader: function (einstellungen) {
		if (
			// keine ID (oder ein Leerstring) übergeben
			!einstellungen.id
			||
			!document.getElementById(einstellungen.id)
			||
			fwk.faders[einstellungen.id]
			||
			einstellungen.images.length < 2
		) {
			return new Boolean(false);
		}
		this.id = einstellungen.id;
		this.images = new Array();
		this.random = (typeof einstellungen.random != "undefined") ? einstellungen.random : fwk.random;
		this.autostart = (typeof einstellungen.autostart != "undefined") ? einstellungen.autostart : fwk.autostart;
		this.viewTime = einstellungen.viewTime || fwk.viewTime;
		this.fadeStep = einstellungen.fadeStep || fwk.fadeStep;
		this.stopped = false;
		this.playList = new Array();
		this.counter = 0;
		this.dir = "";
		this.fading = false;
		this.element = document.createElement("span");
		this.element.className = fwk.className;
		if (window.opera) {
			this.element.style.display = "inline-table";
		}
		var i;
		i = document.getElementById(this.id);
		i.parentNode.replaceChild(this.element, i);
		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.createPlayList = function () {
			var i, r;
			this.playList = new Array();
			if (this.random) {
				while (this.playList.length < this.images.length) {
					vorhanden = false;
					r = Math.floor(Math.random() * (this.images.length));
					for (i = 0; i < this.playList.length; i++) {
						if (r == this.playList[i]) {
							vorhanden = true;
					}	}
					if (!vorhanden) {
						this.playList[this.playList.length] = r;
				}	}
			} else {
				for (i = 0; i < this.images.length; i++) {
					this.playList[i] = i;
				}
			}
		};
		this.start = function () {
			this.stopped = false;
			this.next();
		};
		this.stop = function () {
			this.stopped = true;
		};
		this.next = function (single, dir) {
			if (single) {
				this.stopped = true;
			}
			if (typeof dir == "string") {
				this.dir = dir;
			}
			if ((this.stopped && !single) || this.fading) {
				return; 
			}
			if (this.dir != "backwards") {
				this.counter = (this.counter < this.playList.length -1) ? this.counter +1 : 0;
				if  (this.counter == 0) {
					this.createPlayList();
				}
			} else {
				this.counter = (this.counter > 0) ? this.counter -1 : this.playList.length -1;
				if  (this.counter == this.playList.length -1) {
					this.createPlayList();
			}	}
			this.element.appendChild(this.images[this.playList[this.counter]]);
			this.images[this.playList[this.counter]].className = "next";
			this.fade();
		};
		this.fade = function (step) {
			var fader = this, imgs = this.element.getElementsByTagName("img");
			this.fading = true;
			step = step || 0;
			imgs[1].style.opacity = step/100;
			imgs[1].style.filter = "alpha(opacity=" + step + ")";
			step += this.fadeStep;
			if (step <= 100) {
				window.setTimeout(function () { fader.fade(step); }, 1);
			} else {
				this.fading = false;
				imgs[1].className = "";
				this.element.removeChild(imgs[0]);
				window.setTimeout(function () { fader.next(); }, this.viewTime);
			}
		};
		this.createPlayList();
	}
}
fwk.start();

