﻿function GLAW()
{
	//global/////////////////////////////////////
	this.ID=            0;	    //set on go
	this.Pos=			0;	    //set on go
	this.ToPos=			0;	    //set on go
	
	this.isRunning=     false;  //check if its running

	this.TargetAccel=	.3;	    //adjuster
	this.RealSpeed=		.25;	    //adjuster
	this.TargetSpeed=	.15;	    //adjuster
	this.Threshhold=	.2;	    //adjuster

	this.CurrentSpeed=	0;	    //private
	this.TimeOutID=		0;	    //private
	/////////////////////////////////////////////
}




//public methods/////////////////////////////
GLAW.prototype.start = function()
{
	this._TargetAccel=	this.TargetAccel;	    //reset
	this._RealSpeed=	this.RealSpeed;	        //reset
	this._TargetSpeed=	this.TargetSpeed;	    //reset
	this._Threshhold=	this.Threshhold;	    //reset
	this._CurrentSpeed=	this.CurrentSpeed;	    //reset
	
    if(this.ID == 0)
        this.ID = context.createID(this);

    this.isRunning = true;
    this.moveIt();
}

GLAW.prototype.stop = function()
{
    clearTimeout(this.TimeOutID);
    this.isRunning = false;
    if(this.ID != 0)
    {
        context.removeID(this.ID);
        this.ID = 0;
    }
}
/////////////////////////////////////////////





//events/////////////////////////////////////
GLAW.prototype.onTick = function(CurrentValue)
{
    //event fired on each tick (internal movement tick)
    //passes back the value for use in whatever means you wish
    //the user then doesnt have to think about setting a timeout. 
}

GLAW.prototype.onFinish = function()
{
    //event only fired when it gets to the postion, and has ceased any activity.
    //i guess we should clean up?
}
/////////////////////////////////////////////






//internal methods///////////////////////////
GLAW.prototype.moveIt = function()
{
    //main engine.

    clearTimeout(this.TimeOutID);
	
    if((Math.round(this.Pos) == Math.round(this.ToPos)) && (this._CurrentSpeed < this._Threshhold))
    {
        //clear the context lookup
        if(this.ID != 0)
        {
            context.removeID(this.ID);
            this.ID = 0;
        }

        //set last vars
	    this.Pos = this.ToPos;
        this.isRunning = false;
		
	    //events
        this.onTick(this.Pos);
        this.onFinish();
	    
        //get the fuck out
	    return;
    }
		
    if(this.Pos != this.ToPos)
	    this.TimeOutID = setTimeout('context.getElement(' + this.ID + ').moveIt()', 1);
		
    this._TargetSpeed = ((this.ToPos - this.Pos) * this._TargetAccel);
    this._CurrentSpeed = this._CurrentSpeed + ((this._TargetSpeed - this._CurrentSpeed) * this._RealSpeed);
		
    this.Pos += this._CurrentSpeed;
	
    this.onTick(this.Pos);
}
/////////////////////////////////////////////

