function ImageRotator(divId, w, h)
{
	if (divId)
	{
		this.divId = divId;
		this.state = "INIT";
		this.imgFg = null;
		this.divBg = null;
		this.opacity = 1;
		this.opacityStep = 0;
		this.imgs = new Array();
		this.imgsCache = new Array();
		this.imgInd = 0;
		this.nextInd = 1;
		this.ticker = 0;
		this.fadeDuration = 1000;
		this.holdDuration = 2000;
		this.inc = 100;
		this.setBG = false;
		this.width = w;
		this.height= h;
		this.loops = 0;
		this.loopCount = 0;
		ImageRotator.rots[divId] = this;
	}
}
new ImageRotator(null);
ImageRotator.rots = new Array();

ImageRotator.prototype.add = function(src)
{
	var img;
	this.imgs.push(src);
	img = new Image();
	img.src = src;
	this.imgsCache.push(img);
}


if (document.getElementById)
{
	ImageRotator.rotate = function(divId)
	{
		ImageRotator.rots[divId].rotate();
	}

	ImageRotator.prototype.print = function()
	{
		document.write
		(
			'<div id="'+this.divId+'" '+
					 'style="'+
					 	'padding: 0px; '+
					 	'margin: 0px; '+
					 	'width: '+this.width+'px; '+
					 	'height: '+this.height+'px; '+
					 	'background-repeat: no-repeat;'+
			'">'+
				'<img style="'+
							'padding: 0px; '+
							'filter: alpha(opacity = 100); '+
							'margin: 0px; '+
							'moz-opacity: 1; '+
							'border: 0px;'+
						'" width="'+this.width+'" height="'+this.height+'" alt="" '+
						'src="'+this.imgs[0]+'" />'+
			'</div>'
		);
	}

	ImageRotator.prototype.rotate = function()
	{
		switch (this.state)
		{
			case "INIT":
				if (this.imgs.length < 2)
				{
					return;
				}
				this.init(document.getElementById(this.divId));
				this.opacity = 1;
				this.ticker = 0;
				this.opacityStep = this.inc / this.fadeDuration;
				this.setBG = false;
				this.state = "HOLDING";
				break;
			case "HOLDING":
				if (this.ticker >= this.holdDuration)
				{
					this.opacity = 1;
					this.ticker = 0;
					this.state = "FADING";
				}
				else if (!this.setBG)
				{
					this.divBg.style.backgroundImage = "url('"+this.imgs[this.nextInd]+"')";
					this.setBG = true;
				}
				break;
			case "FADING":
				this.opacity -= this.opacityStep;
				if (this.opacity <=	0)
				{
					this.imgFg.style.display = "none";
					this.prepNext();
					if ((this.loops != 0) &&
					    (this.loopCount == this.loops))
					{
						this.state = "DONE";
					}
					else
					{
						this.imgFg.src = this.imgs[this.imgInd];
						this.state = "SWITCHING";
					}
				}
				else
				{
					this.setOpacity();
				}
				break;
			case "SWITCHING":
				this.opacity = 1;
				this.setOpacity();			 
				this.setBG = false;
				this.imgFg.style.display = "block";
				this.ticker = 0;
				this.state = "HOLDING";
				break;
		}
		this.ticker += this.inc;
		setTimeout("ImageRotator.rotate('"+this.divId+"');", this.inc);
	}

	ImageRotator.prototype.setOpacity = function()
	{
		if (this.imgFg.filters)
		{
			this.imgFg.filters.alpha.opacity = ""+parseInt(this.opacity * 100);
		}
		else
		{
			this.imgFg.style.MozOpacity = ""+(parseInt(this.opacity * 10) / 10);
		}
	}

	ImageRotator.prototype.prepNext = function()
	{
		if (this.imgs.length == 2)
		{
			nextInd = this.imgInd ? 0 : 1;
			this.imgInd = this.imgInd ? 0 : 1;
		}
		else
		{
			this.imgInd++;
			if (this.imgInd >= (this.imgs.length))
			{
				this.imgInd = 0;
				this.loopCount++;
			}

			this.nextInd = this.imgInd + 1;
			if (this.nextInd >= (this.imgs.length))
			{
				this.nextInd = 0;
			}
		}
	}

	ImageRotator.prototype.init = function(root)
	{
		var i, c, node;
		
		if (!this.divBg)
		{
			this.divBg = root;
		}
		
		for (i = 0, c = root.childNodes.length; i < c; i++)
		{
			node = root.childNodes[i];
			if (node)
			{
				if (node.nodeName == "IMG")
				{
					if (!this.imgFg)
					{
						this.imgFg = node;
					}
					else
					{
						return;
					}
				}
				else if (node.hasChildNodes())
				{
					this.init(node);
				}
			}
		}
	}
}
else
{
	ImageRotator.rotate = function(divId)
	{
	}
}