
function Animation(obj, property, start, end, duration, delay)
{
	this.running = false;
	this.property = property;
	this.obj = obj;
	this.startValue = start;
	this.endValue = end;
	this.interval = duration / 70;
	this.duration = duration;
	this.delay = delay > 0 ? delay : 0;
	this.endTime = 0;
	this.totalTime = 0;
	this.startTime;
	this.animationId;
	this.done = false;
	this.type = "";

	this.start = function()
	{
		//this.setProperty(this.startValue);

		this.startTime = new Date().getTime() + this.delay;
		this.endTime = new Date().getTime() + this.duration + this.delay;
		this.totalTime = this.endTime - this.startTime;
		this.running = true;
	}

	this.update = function()
	{
		var timeRemaining = this.endTime - new Date().getTime();

		//abort if delay time has not passed
		if(timeRemaining > this.totalTime)
			return;

		var percentComplete = 1 - Math.abs(timeRemaining / this.totalTime);
		var totalChangeAmount = this.endValue - this.startValue;

		if(new Date().getTime() > this.endTime)
		{
			this.running = false;
			this.done = true;
			this.setProperty(this.endValue);
			stopInterval(this.animationId);
			return;
		}

		this.setProperty(this.startValue + totalChangeAmount * percentComplete);
	}

	this.setProperty = function(val)
	{
		if(browserType == 'ie' && this.property == 'opacity')
			this.obj.filter = "alpha(opacity=" + val * 100 + ")";	
		else
			this.obj[this.property] = val + this.type;
	}
}

function Animator()
{
	this.runningThreads = 0;
	this.animations = new Array();
	this.threadId = null;
	//getBrowserType();

	this.add = function(a)
	{
		var pos = this.animations.push(a) - 1;
		return pos;
	}

	this.startAnimation = function(pos)
	{
		if(this.runningThreads == 0)
			this.startAnimationThread();

		this.runningThreads++;
		this.animations[pos].start();
	}

	this.stopAnimation = function(pos)
	{
		this.animations[pos].running = false;
		this.animations[pos] = null;

		this.runningThreads--;
		if(this.runningThreads == 0)
			this.stopAnimationThread();
	}

	this.startAll = function()
	{
		for(i=0; i<this.animations.length; i++)
		{
			if(!this.animations[i].running && !this.animations[i].done)
			{
				this.animations[i].start();
				this.runningThreads++;
			}
		}
		this.startAnimationThread();
	}

	this.update = function()
	{
		nothingRunning = true;
		updated = '';
		for(i=0; i < this.animations.length; i++)
		{
			if(this.animations[i].running && !this.animations[i].done)
			{
				updated = updated + i + ' ';
				nothingRunning = false;
				this.animations[i].update();
			}
		}

		if(nothingRunning)
			this.stopAnimationThread();
	}

	this.stopAnimationThread = function()
	{
		stopInterval(this.threadId);
		this.running = 0;
		this.threadId = null;
	}

	this.startAnimationThread = function()
	{
		if(this.threadId == null)
			this.threadId = startInterval(this, 'update()', 40);
	}
}


var browserType;
function getBrowserType()
{
	browserType = "unknown";

	if(navigator.userAgent.indexOf("Opera") > -1)
	{
		browserType = "opera";
		return;
	}
	
	if(navigator.userAgent.indexOf("MSIE") > -1)
	{
		browserType = "ie";
		return;
	}
}

