function ImageRotator(imageContainer, waitingTime, fadeTime, startOffset) {
    this.imageContainer = typeof imageContainer != "undefined" ? imageContainer : "imagerotator_container";
    this.waitingTime = typeof waitingTime != "undefined" ? waitingTime : 5000;
    this.fadeTime = typeof fadeTime != "undefined" ? fadeTime : 500;
    this.startOffset = typeof startOffset != "undefined" ? startOffset : 0;
    
    this.timer = null;
    this.fadeInc = 20;
    this.fadeStep = 1/(this.fadeTime/this.fadeInc);
    this.current = this.startOffset;
    this.pause = false;
    this.transition = false;
    
    //init
    this.images = document.getElementById(this.imageContainer).getElementsByTagName("img");
    this.current = this.current % this.images.length;
	for (i = 0; i < this.images.length; ++i) {
        this.images[i].xOpacity = 0;
    }
	this.images[this.current].style.display = "block";
	this.images[this.current].xOpacity = .99;
    
    //start
    var self = this;
    this.timer = setTimeout(function() {self.next();}, this.waitingTime);
}

ImageRotator.setOpacity = function(obj) {
	if (obj.xOpacity > .99) {
		obj.xOpacity = .99;
		return;
	}
	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

ImageRotator.prototype.next = function() {
    if (this.transition) return;
    this.nextLoop();
}

ImageRotator.prototype.previous = function() {
    if (this.transition) return;
    this.previousLoop();
}

ImageRotator.prototype.nextLoop = function() {
	// Cancel the current timer
	clearTimeout(this.timer);
	this.timer = null;
	cOpacity = this.images[this.current].xOpacity;
	nIndex = this.images[this.current+1] ? this.current+1 : 0;
	nOpacity = this.images[nIndex].xOpacity;
	cOpacity -= this.fadeStep;
	nOpacity += this.fadeStep;
	this.images[nIndex].style.display = "block";
	this.images[this.current].xOpacity = cOpacity;
	this.images[nIndex].xOpacity = nOpacity;
	ImageRotator.setOpacity(this.images[this.current]); 
	ImageRotator.setOpacity(this.images[nIndex]);
    var self = this;
	if (cOpacity <= 0) {
		this.images[this.current].style.display = "none";
		this.current = nIndex;
		this.transition = false;
		this.timer = setTimeout(function() {self.nextLoop();}, this.waitingTime);
	} else {
		this.transition = true;
		this.timer = setTimeout(function() {self.nextLoop();}, this.fadeInc);
	}
}


ImageRotator.prototype.previousLoop = function() {
	// Cancel the current timer
	clearTimeout(this.timer);
	this.timer = null;
	
	cOpacity = this.images[this.current].xOpacity;
	if (this.current - 1 < 0)
	{
		nIndex = this.images.length - 1;
	}
	else
	{
		nIndex = this.current - 1;
	}
	nOpacity = this.images[nIndex].xOpacity;
	cOpacity -= this.fadeStep; 
	nOpacity += this.fadeStep;
	this.images[nIndex].style.display = "block";
	this.images[this.current].xOpacity = cOpacity;
	this.images[nIndex].xOpacity = nOpacity;
	ImageRotator.setOpacity(this.images[this.current]); 
	ImageRotator.setOpacity(this.images[nIndex]);
    self = this;
	if (cOpacity <= 0) {
		this.images[this.current].style.display = "none";
		this.current = nIndex;
		this.transition = false;
		this.timer = setTimeout(function() {self.previousLoop();}, this.waitingTime);
	} else {
		this.transition = true;		
		this.timer = setTimeout(function() {self.previousLoop();}, this.fadeInc);
	}
}
