var OdxRotatingImage = new Class({	
	
	/*********************************
		Constructor
	*********************************/
	initialize: function(id, interval)
	{
		this.$_images	= new Array();
		this.$_current	= -1;
		this.$_timer	= null;
	
		this
			.SetId(id)
			.SetInterval(interval);
	},

	
	/*********************************
		Properties
	*********************************/
	GetId: function()
	{
		return this.$_id;
	},
	
	SetId: function(id)
	{
		this.$_id = id;
		this.$_element = $(id);
		this.$_element.setStyle('position', 'relative');
		return this;
	},
	
	GetInterval: function()
	{
		return this.$_interval;
	},
	
	SetInterval: function(interval)
	{
		this.$_interval = interval;
		return this;
	},
	
	
	/*********************************
		Public Methods
	*********************************/
	// Add an image to the collection
	AddImage : function(imageUrl)
	{
		var newImage = new Element('img');
		newImage.src = imageUrl;
				
		newImage.setStyles({
			'opacity'	: 0,
			'position'	: 'absolute',
			'top'		: 0,
			'left'		: 0
		});
		
		this.$_images.push(newImage);
		newImage.injectInside(this.$_element);
	},
	
	// Start rotating
	Start: function()
	{
		this._NextImage();
		this.$_timer = this._NextImage.bind(this).periodical(this.$_interval);
	},
	
	// Stop rotating
	Stop: function()
	{
		$clear(this.$_timer);
	},
		
	
	/*********************************
		Private Methods
	*********************************/
	// Cycle forwards
	_NextImage : function()
	{
		if (this.$_current >= this.$_images.length - 1)
			this._ShowImage(0);
		else
			this._ShowImage(this.$_current + 1);
	},
	
	// Cycle backwards
	_PreviousImage : function()
	{
		if (this.$_current <= 0)
			this.$_current = this.$_images.length - 1;
		else
			this.$_current--;
			
		this._ShowImage(this.$_current);
	},
	
	// Show image at specified index
	_ShowImage : function(index)
	{
		var newImage = this.$_images[index];
		
		if (this.$_element.childNodes.length == 0)
		{
			newImage.injectInside(this.$_element).setStyle('opacity', 1);
		}
		else
		{
			var oldImage = this.$_element.getChildren()[0];
			
			this._FadeOutImage(oldImage);
			this._FadeInImage.delay(300, this, [newImage]);
		}
			
		this.$_current = index;
	},
		
	_FadeOutImage: function(image)
	{
		var effect = new Fx.Styles(image, {duration:450, transition:Fx.Transitions.Quart.easeIn});
		effect.start({
			'opacity' : 0
		});
	},
	
	_FadeInImage: function(image)
	{
		var effect = new Fx.Styles(image, {duration:600, transition:Fx.Transitions.Quart.easeOut});
		effect.start({
			'opacity' : 1
		});
	}
});
