//AnimObject = new Object();

var allAnimObjects = new Array();

function AnimObject(element, sl, st, el, et, speed, duration)
{
	this.init(element, sl, st, el, et, speed, duration);
}

AnimObject.prototype.init = function(element, sl, st, el, et, speed, duration)
{
	this.pos = allAnimObjects.length;
	allAnimObjects[this.pos] = this;
	
	
	//element to animate
	this.el = element;
	
	this.startLeft = sl;
	this.startTop = st;
	this.endLeft = el;
	this.endTop = et;

	this.time = 0;
	this.duration = duration;

	//timer delay between frames
	this.speed = speed;
	
	this.timer = null;
	
	//1 for out -1 for in
	this.direction = 0;	

	return this;
}	

AnimObject.prototype.animComplete = function (){}

AnimObject.prototype.setAction = function(eventName, action)
{
	this.el[eventName] = createMethodReference(this,action);
}

AnimObject.prototype.setForiegnAction = function(obj,eventName, action)
{
	this.el[eventName] = createMethodReference(obj,action);
}

AnimObject.prototype.anim = function(d)
{
	clearInterval(this.timer)
	this.direction = d;
	this.timer = setInterval("allAnimObjects["+this.pos+"].draw()", this.speed)
}

AnimObject.prototype.animOut = function() { this.anim(1); }

AnimObject.prototype.animIn = function() { this.anim(-1); }

AnimObject.prototype.checkInterval = function()
{
	if(this.time > this.duration)
	{
		clearInterval(this.timer);
		this.time = this.duration;
		this.animComplete()
	}
	if(this.time < 0)
	{
		clearInterval(this.timer);
		this.time = 0;
	}	
}


AnimObject.prototype.draw = function(pos)
	{
		if(pos == null) obj = this;
		else obj = allAnimObjects[pos];
		
		obj.time += obj.direction;
		
		var t = easeInOutQuad(obj.time, obj.startLeft, obj.endLeft, obj.duration) 

		obj.el.style.left = Math.round(t)+"px";
		
		var t = easeInOutQuad(obj.time, obj.startTop, obj.endTop, obj.duration) 
		obj.el.style.top = Math.round(t)+"px";		
		
		obj.checkInterval();
	}

function createMethodReference(object, methodName) 
{
    return function() 
	{
										//mac ie does not support apply
	     object[methodName](); //.apply(object, arguments);
	};
};

//t: current time, b: beginning value, c: change in value, d: duration
function easeInOutQuad(t, b, c, d) 
{
        if (t < d/2) return 2*c*t*t/(d*d) + b;
        var ts = t - d/2;
			var r = -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b
        return r;
}
