
//
// AnimationNode Object by Eric Lindgren
// Copyright 2008 and on
//
// DESCRIPTION:
//
// USE:
//

//********************
//* GLOBAL VARIABLES *
//********************

var AN_idnum = 0;

//***********************************
//* AnimationNode object definition *
//***********************************
//
// CLASS CONTAINS THE FOLLOWING MEMBERS:
//
//  obj		= The image object which the AnimationNode is for
//  wid		= The window id number for the AnimationNode, for setTimeout
//  imgs	= An array of image files that make up the animation
//  numImgs	= The number of images in the array
//  curPos	= The current image animation position
//  dur		= An array of durations in milliseconds that each animation step takes
//  animate	= An integer that is set to -1 for backward, 0 for stop, and 1 for forward
//  triggers    = An array of strings to be executed by Javascript.  The array index represents the step to execute on.
//
// CLASS CONTAINS THE FOLLOWING METHODS:
//
//  addImage		= Adds a new image to the array, at the next position
//  addImageAt		= Adds/replaces a new image to the array at the specified position
//  removeImage		= Removes an image from the array at the specified position and shifts the array
//  forward		= Processes animation forward from the current position (not user command)
//  backward		= Processes animation backward from the current position (not user command)
//  stopAnimation	= User command to stop animation where it is at
//  animateForward	= User command to start animating forward from the current position
//  animateBackward	= User command to start animating backwards from the current position
//

//
// Constructor:
//  s_obj	= The object which this AnimationNode affects
//  s_imgs	= Can be an array or the number 0.  If it is an array, it must be the list of
//		  image URLs for the animation, in animation order.  if it is 0, it will be
//		  instantiated with no images and they must be added via addImage functions.
//  s_dur	= Can be an array of integers or a single integer.  If it is an array, there must
//		  be one number per image and each corresponds to the number of milliseconds before
//		  the next image is shown.  If it's a single integer, that value will be applied
//		  to every animation step duration.
//
function AnimationNode(s_obj, s_imgs, s_dur)
{
   this.wid 		= "AN_" + AN_idnum++; 
   window[this.wid] 	= this;
   this.obj = s_obj;
   if(typeof s_imgs == "object")
      this.imgs = s_imgs;
   else
      this.imgs = new Array();
   this.numImgs = this.imgs.length;
   if(typeof s_dur == "object")
      this.dur = s_dur;
   else if(typeof s_dur == "number")
   {
      this.dur = new Array();
      for(var i = 0; i < this.numImgs; i++)
         this.dur[i] = s_dur;
   }
   this.animate = 0;  //Stopped
   this.curPos = 0;
   this.triggers = new Array();
   for(var i = 0; i < this.numImgs; i++)
      this.triggers[i] = null;
   return this;
}

//
// Forward animation function.  This recursive function moves the animation forward until it reaches
// the end or the animation direction is changed or stopped.
//
AnimationNode.prototype.forward = function()
{
   this.obj.src = this.imgs[this.curPos];
   if(this.animate != 1)
      return;
   if(this.curPos >= (this.numImgs - 1))
   {
      this.animate = 0;
      this.curPos = this.numImgs - 1;
      return;
   }
   if(this.triggers[this.curPos] != null)
      eval(this.triggers[this.curPos]);
   this.curPos++;
   this.setTimeout('forward()', this.dur[this.curPos]);
}

//
// Backward animation function.  This recursive function moves the animation backward until it reaches
// the beginning or the animation direction is changed or stopped.
//
AnimationNode.prototype.backward = function()
{
   this.obj.src = this.imgs[this.curPos];
   if(this.animate != -1)
      return;
   if(this.curPos <= 0)
   {
      this.animate = 0;
      this.curPos = 0;
      return;
   }
   if(this.triggers[this.curPos] != null)
      eval(this.triggers[this.curPos]);
   this.curPos--;
   this.setTimeout('backward()', this.dur[this.curPos]);
}

//
// stopAnimation function - this simply stops the animation in its tracks
//
AnimationNode.prototype.stopAnimation = function()
{
   this.animate = 0;
}

//
// animateForward function - this will start an animation going forward from its current position
//
AnimationNode.prototype.animateForward = function()
{
   this.animate = 1;
   this.forward();
}

//
// animateBackward function - this will start an animation going backwards from its current position
//
AnimationNode.prototype.animateBackward = function()
{
   this.animate = -1;
   this.backward();
}

AnimationNode.prototype.setTimeout = function(f,t) 
{ 
    setTimeout("window."+this.wid+"."+f, t); 
} 


AnimationNode.prototype.addTrigger = function(s, str)
{
   this.triggers[s] = str;
}

