/*==================================================*
  Filename: animationBasic-01.js
  JavaScript object oriented animation routines

  Date     Description                          By
  --------|------------------------------- ----|----
  16Jul07  Split from real useage for tutorial  arc
 *==================================================*/

//Globals
var tickInt = 25;    // tick interval in msec avoid multiple of other timers
var PIon180 = Math.PI / 180;


function pos(x, y)
{
  this.x = Math.round(x);
  this.y = Math.round(y);
}

  function animation(id, path, steps)
  {
    this.elem = document.getElementById(id);
    this.active = 0;
    this.timer = null;
    this.path = path;      // pointer to path object
    this.pathIdx = 0;      // step counter
    this.numSteps = steps; // total steps, 0 = go forever
  }

  animation.prototype.start = function()
  {
    if (this.active)
      return;

    var saveThis = this;   /* create a closure */

    this.step();
    this.active = 1;
    this.timer = setInterval(function(){saveThis.step()}, tickInt);
  }

  animation.prototype.stop = function()
  {
    if (!this.timer)
      return false;
    clearInterval(this.timer);
    this.active = 0;
  }

  animation.prototype.step = function()
  {
    var nextPos = this.path.nextStep(this.pathIdx);

    this.moveTo(nextPos.x, nextPos.y);
    if ((this.numSteps > 0) && (this.pathIdx >= this.numSteps))
      this.stop();
    else
      this.pathIdx++;
  }

  animation.prototype.moveTo = function(x, y)
  {
    this.elem.style.top = y + 'px';
    this.elem.style.left = x + 'px';
  }

