// Nemo Image Carousel Classes
// Owen Hindley 2011
// www.owenhindley.co.uk

function NemoImageCarouselController(carouselElementID, speed, tagType)
{
	if (tagType == null)
		tagType = "*";
	this.tagType = tagType;
	this.index = 0;
	this.speed = speed;
	this.carouselElementID = carouselElementID;
	this.init();
}

function NemoCarouselUpdate(carousel)
{
	
	if (carousel)
		carousel.update();	
	
}

NemoImageCarouselController.prototype.init = function()
{
	this.carouselElement = document.getElementById(this.carouselElementID);
	// hide all images except top
	var elements = this.carouselElement.getElementsByTagName(this.tagType);
	$("#" + this.carouselElementID).children().hide();
	
	this.index = elements.length;
	
	// Start timer
	NemoCarouselUpdate(this);
	
}

NemoImageCarouselController.prototype.update = function()
{
	var elements = this.carouselElement.getElementsByTagName(this.tagType);	
	var nextIndex = 0;
	if(this.index < (elements.length - 1)) nextIndex = this.index + 1;		
	// swop images 
	$(elements[this.index]).fadeOut(this.speed);
	$(elements[nextIndex]).fadeIn(this.speed);
	//console.log("index = " + this.index);
	this.index = nextIndex;
	
	setTimeout(NemoCarouselUpdate, (this.speed * 2), this);
}
