
$(document).ready(function()
{
	ContentRotatorController.initialise();
});

var ContentRotatorController = new function()
{
       this.rotators = {};
       
       this.add = function(id, contentCount)
       {
              this.rotators[id] = new ContentRotator(id, contentCount);
       },
       
       this.initialise = function()
       {
              
              for (var id in this.rotators)
              {
                     var rotator = this.rotators[id];
                     
                     if (rotator.count <= 1)
                     {
                           continue;
                     }
                     
                     $(rotator.getPreviousLink()).click(function(event)
                     {
                           event.preventDefault();
                           rotator.previous();
                     });
                     
                     $(rotator.getNextLink()).click(function(event)
                     {
                           event.preventDefault();
                           rotator.next();
                     }); 
              }
       }
}

function ContentRotator(id, contentCount)
{
	this.id = id;
	this.count = contentCount;
	this.current = 0;
}

ContentRotator.prototype.next = function()
{
	if (this.current >= this.count - 1) return;
	
	var rotator = this;
	$(rotator.getCurrent()).fadeOut('fast', function()
	{
		rotator.current++;
		
		$(rotator.getPreviousLink()).removeClass('disabled');
			
		if (rotator.current == rotator.count - 1) 
		{
			$(rotator.getNextLink()).addClass('disabled');
		}
			
		$(rotator.getCurrent()).fadeIn('fast');
	});
}

ContentRotator.prototype.previous = function()
{
	if (this.current <= 0) return;
	
	var rotator = this;
	$(rotator.getCurrent()).fadeOut('fast', function()
	{
		rotator.current--;
		
		$(rotator.getNextLink()).removeClass('disabled');
			
		if (rotator.current == 0) 
		{
			$(rotator.getPreviousLink()).addClass('disabled');
		}
		
		$(rotator.getCurrent()).fadeIn('fast');
	});
}

ContentRotator.prototype.getNextLink = function()
{
	return $get(this.id + '_Next');
}

ContentRotator.prototype.getPreviousLink = function()
{
	return $get(this.id + '_Previous');
}

ContentRotator.prototype.getContent = function(index)
{
	return $get(this.id + '_' + index);
}

ContentRotator.prototype.getCurrent = function()
{
	return this.getContent(this.current);
}


