﻿function Animation(node, clocktick, step, delta)
{
	this.ClockID = 0;
	this.Node =$i(node);
	this.ClockTick = clocktick;
	this.Step = step;
	this.Delta = delta;
	this.MoveAttrib = null;
	this.Delim = null;
	this.Opac = 100;
}

Animation.prototype.CheckData = function()
{
	if(this.Delta%this.Step != 0)
	{
		return false;
	}
	if(null == this.Node)
	{
		return false;
	}
	
	return true;
};

Animation.prototype.GetMoveAttrib = function()
{
	return eval('this.Node.style.' + this.MoveAttrib);
};

Animation.prototype.SetMoveAttrib = function(i)
{
	eval('this.Node.style.' + this.MoveAttrib + '=i;');
};
Animation.prototype.mAttToInt = function()
{
	var att = this.GetMoveAttrib().replace('px','');
	return parseInt(att);
};
Animation.prototype.mIntToAtt = function(i)
{
	return '' + i + 'px';
};


Animation.prototype.ExpandHeight = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step < 0) this.Step = (-1)*this.Step;
	if(this.Delta < 0) this.Delta = (-1)*this.Delta;
	
	this.StretchUD();	

};

Animation.prototype.DecreaseHeight = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step > 0) this.Step = (-1)*this.Step;
	if(this.Delta > 0) this.Delta = (-1)*this.Delta;
	
	this.StretchUD();	

};

Animation.prototype.StretchUD = function()
{
	this.MoveAttrib = 'height';
	var ih = this.mAttToInt();		
	ih += this.Delta;
	
	if(ih < 0) ih = 0;
	this.Delim = this.mIntToAtt(ih);
	
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Move();}, this.ClockTick);
};


Animation.prototype.MoveUp = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step > 0) this.Step = (-1)*this.Step;
	if(this.Delta > 0) this.Delta = (-1)*this.Delta;
	
	this.MoveUD();

};
Animation.prototype.MoveDown = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step < 0) this.Step = (-1)*this.Step;
	if(this.Delta < 0) this.Delta = (-1)*this.Delta;
	
	this.MoveUD();	

};

Animation.prototype.MoveUD = function()
{
	this.MoveAttrib = 'top';
	var itop = this.mAttToInt();		
	itop += this.Delta;
	this.Delim = this.mIntToAtt(itop);
	
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Move();}, this.ClockTick);
};


Animation.prototype.MoveLeft = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step > 0) this.Step = (-1)*this.Step;
	if(this.Delta > 0) this.Delta = (-1)*this.Delta;
	
	this.MoveRL();

};
Animation.prototype.MoveRight = function()
{
	if(!this.CheckData())
		return;
		
	if(this.Step < 0) this.Step = (-1)*this.Step;
	if(this.Delta < 0) this.Delta = (-1)*this.Delta;
	
	this.MoveRL();	

};

Animation.prototype.MoveRL = function()
{
	this.MoveAttrib = 'left';
	var ileft = this.mAttToInt();		
	ileft += this.Delta;
	this.Delim = this.mIntToAtt(ileft);
	
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Move();}, this.ClockTick);
};

Animation.prototype.Move = function()
{
	if(this.GetMoveAttrib() == this.Delim)
	{
		clearInterval(this.ClockID);
		return;
	}
	
	var iatt = this.mAttToInt();
	iatt += this.Step;
	
	this.SetMoveAttrib(this.mIntToAtt(iatt));
	
};

Animation.prototype.FadeOut = function()
{
	if(this.Step > 0) this.Step = (-1)*this.Step;
	this.Node.style.opacity = '1';
	this.Node.style.filter = 'alpha(opacity=100)';
	this.Opac = 100;
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Fade();}, this.ClockTick);
};

Animation.prototype.FadeOut = function(time)
{
	this.Step = (-1)*(100 * this.ClockTick) / time;
	this.Step = Math.round(this.Step);
	this.Node.style.opacity = '1';
	this.Node.style.filter = 'alpha(opacity=100)';
	this.Opac = 100;
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Fade();}, this.ClockTick);
};

Animation.prototype.FadeIn = function(time)
{
	this.Step = (100 * this.ClockTick) / time;
	this.Step = Math.round(this.Step);
	this.Node.style.opacity = '0';
	this.Node.style.filter = 'alpha(opacity=0)';
	this.Opac = 0;
	var anim = this;
	this.ClockID = setInterval(function(){ anim.Fade();}, this.ClockTick);
};

Animation.prototype.Fade = function() {
    var absstep = this.Step < 0 ? (-1) * (this.Step) : this.Step;
    if (this.Step < 0) {
        if (this.Opac <= absstep || this.Opac == 0) {
            this.Opac = 0;
            this.Node.style.opacity = '0';
            this.Node.style.filter = 'alpha(opacity=0)';
            this.Node.display = 'none';
            clearInterval(this.ClockID);
            if (typeof this.onFadeOut != 'undefined')
                this.onFadeOut();
            return;
        }
    }
    else {
        if (this.Opac + absstep >= 100 || this.Opac == 100) {
            this.Opac = 100;
            this.Node.style.opacity = '1';
            this.Node.style.filter = 'none';
            clearInterval(this.ClockID);
            if (typeof this.onFadeIn != 'undefined')
                this.onFadeIn();
            return;
        }
    }
    this.Opac += this.Step;

    if (this.Opac < 10) {
        this.Node.style.opacity = '0.0' + this.Opac;
    }
    else {
        this.Node.style.opacity = '0.' + this.Opac;
    }
    this.Node.style.filter = 'alpha(opacity=' + this.Opac + ')';

};