var ImageRotator = function(imageContainer, textElement)
{
	var index = 0;
	var images = [];
	var timer = null;
	var initialized = false;
	var navigation;
	var zIndex = 2;
	
	var initialize = function()
	{
		initialized = true;
	
		imageContainer = (typeof(imageContainer) == "string") ? document.getElementById(imageContainer) : imageContainer;
		textElement = (typeof(textElement) == "string") ? document.getElementById(textElement) : textElement;
		
		imageContainer.style.overflow = "hidden";
		imageContainer.style.position = "relative";

		buildNavigation();
	};
	
	var buildNavigation = function()
	{
		navigation = document.createElement("div");
		navigation.style.position = "absolute";
		navigation.style.left = "18px";
		navigation.style.top = (imageContainer.clientHeight - 36) + "px";
		navigation.style.zIndex = 10000;
		
		var toPrevious = document.createElement("img");
		toPrevious.style.cursor = "pointer";
		toPrevious.style.marginRight = "10px";
		toPrevious.src = "images/previous.png";
		toPrevious.onclick = gotoPrevious;
		navigation.appendChild(toPrevious);
		
		var pausePlay = document.createElement("img");
		pausePlay.style.cursor = "pointer";
		pausePlay.src = "images/pause.png";
		pausePlay.style.marginRight = "10px";
		navigation.appendChild(pausePlay);
		
		pausePlay.onclick = function()
		{
			if(timer != null)
			{
				stop();
				pausePlay.src = "images/play.png";
			}
			else
			{
				start();
				pausePlay.src = "images/pause.png";
			}
		};
		
		var toNext = document.createElement("img");
		toNext.style.cursor = "pointer";
		toNext.src = "images/next.png";
		toNext.onclick = gotoNext;
		navigation.appendChild(toNext);
		
		imageContainer.appendChild(navigation);
	};
	
	var addImage = function(imageUrl, imageText)
	{
		if(!initialized)
			initialize();	
	
		var image = new Image();
		image.src = imageUrl;
		
		image.style.position = "absolute";
		image.style.display = "none";
		image.style.top = "0px";
		image.style.left = "0px";
		
		imageContainer.appendChild(image);
		
		images.push({
			element : image,
			text : imageText
		});
	};
	
	var start = function()
	{
		stop();
		timer = window.setInterval(gotoNext, pub.speed);
	};
	
	var stop = function()
	{
		if(timer != null)
			window.clearInterval(timer);
			
		timer = null;
	};
	
	var gotoNext = function()
	{
		var currentImage = images[index];
		index = (index == images.length - 1 ? 0 : index + 1);
		var newImage = images[index];
		
		var position = imageContainer.clientWidth;
		
		newImage.element.style.left = position + "px";
		newImage.element.style.display = "";
		newImage.element.style.zIndex = zIndex++;

		var slideIn = function()
		{
			position = Math.max(0, position * 0.82 - 0.5);
			newImage.element.style.left = position + "px";
			
			if(position == 0)
			{
				textElement.innerHTML = newImage.text;
			}
			else
			{
				window.setTimeout(slideIn, 30);
			}
		};
		
		slideIn();
	};
	
	var gotoPrevious = function()
	{
		if(!initialized)
			initialize();
			
		var currentImage = images[index];
		index = (index == 0 ? images.length - 1 : index - 1);
		var newImage = images[index];
		
		var position = -imageContainer.clientWidth;
		
		newImage.element.style.left = position + "px";
		newImage.element.style.display = "";
		newImage.element.style.zIndex = zIndex++;

		var slideIn = function()
		{
			position = Math.min(0, position * 0.82 + 0.5);
			newImage.element.style.left = position + "px";
			
			if(position == 0)
			{
				textElement.innerHTML = newImage.text;
			}
			else
			{
				window.setTimeout(slideIn, 30);
			}
		};
		
		slideIn();
	};
	
	var pub = {
		addImage : addImage,
		start : start,
		stop: stop,
		gotoNext : gotoNext,
		gotoPrevious : gotoPrevious,
		speed : 5000
	};
	
	initialize();
	
	return pub;
};
