function Animation(params)
{
	/**
	 * individual animation sequence
	 */
	
	var self = this;
	if (typeof params.tweenType == "undefined")
	{
		params.tweenType = "default";
	}
	
	this.onTween = (params.onTween || null);
	this.onComplete = (params.onComplete || null);
	this.tween = animator.createTween(params.from, params.to, params.tweenType);
	this.frameCount = animator.tweenTypes[params.tweenType].length;
	this.frame = 0;
	this.active = false;
	
	this.animate = function()
	{
		if (self.active)
		{
			if(self.onTween && self.tween[self.frame])
			{
				self.onTween(self.tween[self.frame].data);
			}
			
			if (self.frame++ >= self.frameCount - 1)
			{
				self.active = false;
				self.frame = 0;
				if (self.onComplete)
				{
					self.onComplete();
				}
				return false;
			}
			return true;
		}
		else
		{
			return false;
		}
	}
	
	this.start = function()
	{
		/**
		 * add this to the main animation queue
		 */
		animator.enqueue(self, self.animate, self.onComplete);
		if(!animator.active)
		{
			animator.start();
		}
	}
	
	this.stop = function()
	{
		self.active = false;
	}
}

function Animator()
{
	var self = this;
	var intervalRate = 20;
	this.tweenTypes = {
			"default": [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1],
			"blast": [12,12,11,10,10,9,8,7,6,5,4,3,2,1],
			"linear": [10,10,10,10,10,10,10,10,10,10]
	};
	
	this.queue = [];
	this.queueHash = [];
	this.active = false;
	this.timer = null;
	
	this.createTween = function(start, end, type)
	{
		type = type || "default";
		var tween = [start];
		var tmp = start;
		var diff = end - start;
		var x = self.tweenTypes[type].length;
		for(var i = 0; i < x; i++)
		{
			tmp += diff * self.tweenTypes[type][i] * 0.01;
			tween[i] = {};
			tween[i].data = tmp;
			tween[i].event = null;
		}
		return tween;
	}
	
	this.enqueue = function(object, method, onComplete)
	{
		/**
		 * add object and associated methods to animation queue
		 */
		if (!method)
		{
			
		}
		self.queue.push(object);
		object.active = true;
	}
	
	this.animate = function()
	{
		/**
		 * interval-driven loop: process queue, stop if done
		 */
		var active = 0;
		for (var i = 0, j = self.queue.length; i < j; i++)
		{
			if (self.queue[i].active)
			{
				self.queue[i].animate();
				active++;
			}
		}
		
		if (active == 0 && self.timer)
		{
			/**
			 * all animations finished
			 */
			self.stop();
		}
		else
		{
			
		}
	}
	
	this.start = function()
	{
		if(self.timer || self.active)
		{
			return false;
		}
		
		self.active = true;
		self.timer = setInterval(self.animate, intervalRate);
	}
	
	this.stop = function()
	{
		/**
		 * reset some things, clear for next batch of animations
		 */
		clearInterval(self.timer);
		self.timer = null;
		self.active = false;
		self.queue = [];
	}
}

var animator = new Animator();
