var renderEngine;

document.observe('dom:loaded', function()
{
	renderEngine = new RenderEngine(25);
});

var RenderEngine = Class.create(
{
	initialize: function(frameRate)
	{
		this.frameDelay = (1 / frameRate) * 1000; 
		this.renderables = new Array();
		this.isRunning = false;
	},
	
	addRenderable: function(renderable)
	{
		this.renderables.push(renderable);
		
		if(!this.isRunning)
		{
			this.isRunning = true
			setTimeout(this.update.bind(this), this.frameDelay);
		};
	},
	
	update: function()
	{
		for(i = 0; i < this.renderables.length; )
		{
			if(this.renderables[i].update())
			{
				this.renderables = this.renderables.without(this.renderables[i]);
			} 
			else
			{
				++i;
			}
		}
		
		if(this.renderables.length > 0)
		{
			setTimeout(this.update.bind(this), this.frameDelay);
		}
		else
		{
			this.isRunning = false;
		}
	}
});


var OpacityInterpolator = Class.create(
{
	initialize: function(targetObject, targetValue, duration)
	{
		this.targetObject = targetObject;
		this.initialValue = parseFloat(targetObject.style.opacity);
		this.targetValue = targetValue;
		this.duration = duration * 1000;
		
		var d = new Date();
		this.startTime = d.getTime();
	},

	update: function()
	{
		var d = new Date();
		var isDead = false;
		elapsed = d.getTime() - this.startTime;
		
		if(elapsed > this.duration)
		{
			elapsed = this.duration;
			isDead = true;
		}
		
		opacity = (this.targetValue - this.initialValue) * (elapsed / this.duration) + this.initialValue;
		this.targetObject.style.opacity = opacity;
		this.targetObject.style.filter = 'alpha(opacity: ' + (opacity * 100) + ')';
		return isDead;
	}
});

