/**
 *
 * Base class for for creating animated effects
 * @class Base animation class for creating animated effects
 * @param window Reference to the window object that called the animation
 * <p>Usage: var myAnim = new W.Anim();</p>
 * @constructor
 */
W.Anim = function(window)
{
	this.init(window);
}

W.Anim.prototype = {
 	
 	/**
 	 * Number of frames per second
 	 * @type Number
 	 */
	framerate	: 50,
 	
 	
 	
 	/**
 	 * current frame of the animation
 	 * @type Number
 	 */
	step		: 0,
 	
 	
 	
 	/**
 	 * reference to setInterval time
 	 * @type Object
 	 */
	timer		: null,
 	
 	
 	
 	/**
 	 * HtmlElement the animation is acting on
 	 * @type HtmlElement
 	 */
	target		: null,
 	
 	
 	
 	/**
 	 * Current value of the style property that the animation will be changing
 	 * @type Number
 	 */
	start		: null,
 	
 	
 	
 	/**
 	 * The ending value of the style property that the animation will be changing
 	 * @type Numer
 	 */
	change		: null,
 	
 	
 	
 	/**
 	 * The duration of the animation in milliseconds
 	 * @type Number
 	 */
	duration	: null,
	
	
	/**
	 * Reference to the window object that called the animation
	 */
	window		: null,
	
	
	
	init : function(window)
	{
		if (window) {
			this.window = window;
			this.window.onUpdate = new Function();
		}
	},
	
	
	windowshade : function(target, end, duration)
	{
		var obj			= this;
		
		if (this.window && this.window.inMotion) return;
		this.setMotionState(true);
		
		this.target 	= W.Util.$(target);
		this.start		= parseInt(W.Util.getStyle(target, 'height'));
		this.change		= end - this.start;
		this.duration	= duration;
		
		this.timer = setInterval(function() { obj.update.call(obj)}, this.framerate);
	},
	
	
	
	toggle : function(target, start, end, duration)
	{
		var obj			= this;
		
		this.target		= W.Util.$(target);
		this.start		= start;
		this.change		= end - this.start;
		this.duration	= duration;
		
		this.timer = setInterval(function() { obj.update.call(obj)}, this.framerate);
	},
	
	
	update : function()
	{
		var obj		= this;
		var val		= null;
		
		if (++this.step <= this.duration) {
			val = this.easeInOutQuart(this.step, this.start, this.change, this.duration) + 'px';
			this.target.style.height = val;
			//if (this.window) this.window.onUpdate(val);
			//W.Util.$('debug').innerHTML = this.step + ' : ' + this.target.style.height;
			//this.timer = setInterval(function() { obj.update.call(obj)}, this.framerate);
		} else {
			clearInterval(this.timer);
			this.setMotionState(false);
			this.target.onAnimEnd();
		}
	},
	
	
	setMotionState : function(state)
	{
		if (this.window) {
			this.window.inMotion = state
		}
	},
	
	
	/**
	 * Easing Equations v1.5
	 * May 1, 2003
	 * (c) 2003 Robert Penner, all rights reserved. 
	 * This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.  
	 *
	 * @param {Number} t current time
	 * @param {Number} b beginning value
	 * @param {Number} c change in value
	 * @param {Number} d duration
	 */
	easeInOutQuart : function(t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	}
	
}