/*HTMLElement.prototype.animations = Array();
HTMLElement.prototype.startAnimation = function()
{
	if(this.animations.length > 0 && this.animations[0].status == 0)
		{ this.animations[0].start(); }
};
HTMLElement.prototype.animationEnd = function() {};
HTMLElement.prototype._animationEnd = function()
{
	if(this.animations.length > 0 && this.animations[0].status == 2)
	{
		if(this.animations.length == 1 && this.animations[0].repeat != 0)
		{
			if(this.animations[0].repeat == 2)
			{
				var newAnim = this.animations[0];
				var newFrom = newAnim.to;
				var newTo = newAnim.from;
				newAnim.from = newFrom;
				newAnim.to = newTo;
				newAnim.status = 0;
				this.animations.push(newAnim);
				this.animations.shift();
			}
			else
			{
				this.animations[0].status = 0;
			}
		}
		else
			{ this.animations.shift(); }
	}
	
	if(this.animations.length > 0 && this.animations[0].status == 0)
		{ this.animations[0].start(); }
	
	if(this.animations.length == 0)
	{
		this.animationEnd();
	}
};*/

function Animator(element, from, to, duration)
{
	this.element = element;
	this.status = 0;
	this.frequence = parseFloat(1/24);
	this.from = from;
	this.to = to;
	this.change = null;
	this.duration = parseFloat(duration);
	this.repeat = 0;//0 : aucune//1: infini dans le même sens//2: infini alterné

	this._etape = Array();
	this._position = parseFloat(0);
	
	this._timer = null;
	this._added = false;
}

Animator.prototype.setEtape = function(change)
	{ this.change = change; }

Animator.prototype.start = function()
{
	this.status = 1;
	this._makeEtape();
	this._position = 0;
	this._timer = window.setInterval(function(obj) { obj._doAnimation(); }, this.frequence*1000, this);
};

Animator.prototype.addToElement = function()
{
	this.element.animations = Array();
	this.element.startAnimation = function()
	{
		if(this.animations.length > 0 && this.animations[0].status == 0)
			{ this.animations[0].start(); }
	};
	this.element.animationEnd = function() {};
	this.element._animationEnd = function()
	{
		if(this.animations.length > 0 && this.animations[0].status == 2)
		{
			if(this.animations.length == 1 && this.animations[0].repeat != 0)
			{
				if(this.animations[0].repeat == 2)
				{
					var newAnim = this.animations[0];
					var newFrom = newAnim.to;
					var newTo = newAnim.from;
					newAnim.from = newFrom;
					newAnim.to = newTo;
					newAnim.status = 0;
					this.animations.push(newAnim);
					this.animations.shift();
				}
				else
				{
					this.animations[0].status = 0;
				}
			}
			else
				{ this.animations.shift(); }
		}
		
		if(this.animations.length > 0 && this.animations[0].status == 0)
			{ this.animations[0].start(); }
		
		if(this.animations.length == 0)
		{
			this.animationEnd();
		}
	};
	this.element.animations.push(this);
	this._added = true;
}

Animator.prototype._doAnimation = function()
{
	var iteration = this._position;
	if(iteration > 1) { iteration = 1; }

	for(var tour in this._etape)
	{
		var valeur = parseFloat(this._etape[tour].startValue + (this._etape[tour].endValue-this._etape[tour].startValue)*iteration)+this._etape[tour].unite;
		this._setStyleElement(this.element, this._etape[tour].cssName, valeur);
	}
	
	if(this._position >= 1)
	{
		this.status = 2;
		window.clearInterval(this._timer);
		this._timer = null;
		if(this._added == true)
		{
			this.element._animationEnd();
		}
	}
	else
		{ this._position += parseFloat(this.frequence/this.duration); }
};

Animator.prototype._makeEtape = function()
{
	this._etape = Array();

	if(this.change != null)
	{
		this._etape = this.change;
		return(0);
	}

	for(var tour in this.from)
	{
		var reg=new RegExp("([-]?[\.0-9]+)","g");
		var nombre = reg.exec(this.from[tour]);
		if(nombre == null) { nombre = 0; } else { nombre = parseFloat(nombre[1]); }

		var reg=new RegExp("[0-9\.]+(.+)[;]?","g");
		var unit = reg.exec(this.from[tour]);
		if(unit == null) { unit = ''; } else { unit = unit[1]; }

		var reg=new RegExp("(.+)\s?:.+","g");
		var propriete = reg.exec(this.from[tour]);
		var tempProp = {cssName: propriete[1], startValue: nombre, unite:unit, endValue: 0};
		
		this._etape.push(tempProp);
	}

	for(var tour in this.to)
	{
		var reg=new RegExp("([-]?[\.0-9]+)","g");
		var nombre = reg.exec(this.to[tour]);
		if(nombre == null) { nombre = 0; } else { nombre = parseFloat(nombre[1]); }

		var reg=new RegExp("(.+)\s?:.+","g");
		var propriete = reg.exec(this.from[tour]);
		
		for(var lap in this._etape)
		{
			if(this._etape[lap].cssName == propriete[1]) { this._etape[lap].endValue = nombre; continue; }
		}
	}
};

Animator.prototype._setStyleElement = function(withObject, cssName, cssValue)
{
	switch(cssName.toLowerCase())
	{
		case 'border-bottom-width': withObject.style.borderBottomWidth = cssValue; break;
		case 'border-top-width': withObject.style.borderTopWidth = cssValue; break;
		case 'border-left-width': withObject.style.borderLeftWidth = cssValue; break;
		case 'border-right-width': withObject.style.borderRightWidth = cssValue; break;
		case 'border-width': withObject.style.borderWidth = cssValue; break;

		case 'outline-width': withObject.style.outlineWidth = cssValue; break;
		
		case 'bottom': withObject.style.bottom = cssValue; break;
		case 'top': withObject.style.top = cssValue; break;
		case 'left': withObject.style.left = cssValue; break;
		case 'right': withObject.style.right = cssValue; break;

		case 'height': withObject.style.height = cssValue; break;
		case 'max-height': withObject.style.maxHeight = cssValue; break;
		case 'min-height': withObject.style.minHeight = cssValue; break;

		case 'width': withObject.style.width = cssValue; break;
		case 'max-width': withObject.style.maxWidth = cssValue; break;
		case 'min-width': withObject.style.minWidth = cssValue; break;

		case 'opacity': withObject.style.opacity = cssValue; break;

		case 'font-size': withObject.style.fontSize = cssValue; break;
		case 'font-size-adjust': withObject.style.fontSizeAdjust = cssValue; break;
		case 'letter-spacing': withObject.style.letterSpacing = cssValue; break;
		case 'line-height': withObject.style.lineHeight = cssValue; break;
		case 'text-indent': withObject.style.textIndent = cssValue; break;
		case 'word-spacing': withObject.style.wordSpacing = cssValue; break;
		
		case 'margin-bottom': withObject.style.marginBottom = cssValue; break;
		case 'margin-top': withObject.style.marginTop = cssValue; break;
		case 'margin-left': withObject.style.marginLeft = cssValue; break;
		case 'margin-right': withObject.style.marginRight = cssValue; break;
		case 'margin': withObject.style.margin = cssValue; break;
		
		case 'padding-bottom': withObject.style.paddingBottom = cssValue; break;
		case 'padding-top': withObject.style.paddingTop = cssValue; break;
		case 'padding-left': withObject.style.paddingLeft = cssValue; break;
		case 'padding-right': withObject.style.paddingRight = cssValue; break;
		case 'padding': withObject.style.padding = cssValue; break;
	}
};
