/*
Rotater 1.1.1 (c) 2008 by J. Reijers

### EXAMPLE ###

<body>
<script type="text/javascript">
rot = new Rotater();
with (rot) {
	images = [
		"pics/picture1.gif",
		"pics/picture2.gif",
		{ img: "pics/picture3.jpg", url: "http://www.webby.com", target: "_blank" },
		"pics/picture4.gif"
	];
	pause = 3000;
	init();
}
</script>
*/

function Rotater(id) {
	if (typeof id == "undefined") {
		if (typeof rotOccurence == "undefined") rotOccurence = 0; else rotOccurence++;
		this.id = "rot_" + rotOccurence;
	} else this.id = id;
	this.images = [];
	this.cursor = 0;
	this.position = "relative";
	this.x = 0;
	this.y = 0;
	this.width = 0;
	this.height = 0;
	this.align = "";
	this.valign = "";
	this.colour = "";
	this.fillColour = "blue";
	this.opacity = 100;
	this.pause = 3000; // milliseconds
	this.fadeDelay = 30; // milliseconds
	this.fadeStep = 5;
	this.autostart = true;
	this.stopped = true;
	this.hideUntilStart = true;

	eval(this.id + "_globVar = this;");

	this.init = function() {
		if (this.images.length == 0) return false;

		var detectWidth = (this.width == 0);
		var detectHeight = (this.height == 0);
		if (detectWidth || detectHeight) for (var i = 0; i < this.images.length; i++) {
			var img = new Image;
			img.src = (typeof this.images[i] == "object") ? this.images[i].img : this.images[i];
			if (detectWidth && img.width > this.width) this.width = img.width;
			if (detectHeight && img.height > this.height) this.height = img.height;
		}
		if (!isNaN(this.width)) this.width += "px";
		if (!isNaN(this.height)) this.height += "px";

		this.origOpacity = this.opacity;
		document.write("<div id='" + this.id + "' style='position: " + this.position + "; left: " + this.x + "; top: " + this.y + "; width: " + this.width + "; height: " + this.height + "; background-color: " + this.colour + "'><table width=100% height=100% cellpadding=0 cellspacing=0 border=0><tr><td id='" + this.id + "_cnt' align='" + this.align + "' valign='" + this.valign + "'><img id='" + this.id + "_img' src='' style='display: none'></td></tr></table></div>");
		this.divShell = document.getElementById(this.id);
		if (this.hideUntilStart) this.hide();
		this.container = document.getElementById(this.id + "_cnt");
		this.img = document.getElementById(this.id + "_img");
		this.put();
		this.setOpacity();
		this.fadingOut = false;
		this.fadingIn = false;
		if (this.autostart) this.start();
	}

	this.start = function() {
		if (this.stopped) {
			this.stopped = false;
			if (this.hideUntilStart) this.show();
			this.startPause();
		}
	}

	this.stop = function() {
		this.stopped = true;
	}

	this.hide = function() {
		this.divShell.style.visibility = "hidden";
	}

	this.show = function() {
		this.divShell.style.visibility = "";
	}

	this.setX = function(x) {
		this.divShell.style.left = x;
	}

	this.setY = function(y) {
		this.divShell.style.top = y;
	}

	this.put = function() {
		this.img.style.display = "";
		if (typeof this.images[this.cursor] == "object") {
			var img = this.images[this.cursor].img;
			var url = this.images[this.cursor].url;
			var target = this.images[this.cursor].target ? this.images[this.cursor].target : "_blank";
		} else {
			var img = this.images[this.cursor];
			var url = "";
		}
		this.img.src = img;
		this.img.onclick = (url == "") ? "" : new Function("window.open(\"" + url + "\", \"" + target + "\");");
		this.img.style.cursor = (url == "") ? "" : "pointer";
	}

	this.setOpacity = function(value) {
		if (typeof value == "undefined") var value = this.opacity;
		if (value < 0) value = 0;
		if (value > 100) value = 100;
		this.divShell.style.filter = "alpha(opacity=" + value + ")";
		this.divShell.style.opacity = value / 100;
		this.divShell.style.mozOpacity = value / 100;
		this.opacity = value;
	}

	this.startPause = function() {
		if (!this.stopped && this.images.length > 1) setTimeout(this.id + "_globVar.fadingOut = true; " + this.id + "_globVar.next()", this.pause);
	}

	this.next = function() {
		if (this.fadingOut == true) {
			if (this.opacity > 0) {
				this.opacity -= this.fadeStep;
				this.setOpacity();
			}
			if (this.opacity == 0) {
				this.fadingOut = false;
				this.cursor++;
				if (this.cursor >= this.images.length) this.cursor = 0;
				this.put();
				this.fadingIn = true;
			}
			setTimeout(this.id + "_globVar.next()", this.fadeDelay);
		} else if (this.fadingIn == true) {
			if (this.opacity < this.origOpacity) {
				this.opacity += this.fadeStep;
				this.setOpacity();
			}
			if (this.opacity == this.origOpacity) {
				this.fadingIn = false;
				this.startPause();
			} else setTimeout(this.id + "_globVar.next()", this.fadeDelay);
		}
	}
}
