// +------------------------------------------------------------------------------------+
// | Animation Script                                                                   |
// | Tom Mancine                                                                        |
// |                                                                                    |
// | This script creates an animation on a page using static images and                 |
// | object-oriented JavaScript.  The user creates an object of type                    |
// | Animation, specifying the length in frames of the animation:                       |
// |        var myAnim = new Animation(5);                                              |
// |                                                                                    |
// | The user then enters the names of each image N in the animation by setting         |
// | frames[N-1] to reflect the file path:                                              |
// |        myAnim.frames[3] = "http://imagepath";                                      |
// |                                                                                    |
// | The user may adjust the speed of the animation by calling the setDelay function.   |
// | The speed is given in milliseconds.                                                |
// |        myAnim.setDelay(150);                                                       |
// |                                                                                    |
// | Finally, the user will call the animate() function to start the animation:         |
// |        myAnim.animate();                                                           |
// +------------------------------------------------------------------------------------+



// This is the Animation object definition.
// The member functions will be added later, which explains why, for example, the
// call to this.initialize() doesn't appear to match anything.

function Animation(frameCount)
{
    this.length=frameCount;                      // this.length is the number of frames in the animation
    this.frames = new Array(this.length);        // this.frames is an array holding the *locations* of images
    this.images = new Array(this.length);        // this.images is an array holding the actual preloaded images
    this.location=document.images.length;        // this.location allows the script to access the proper animation;
                                                 // the animation will appear as part of the images array,
                                                 // so this will keep track of where in the array a given animation
                                                 // appears.
    this.delay=0;                                // this.delay controls the speed of the animation
    this.exists=false;                           // this.exists is used by the animate() function to tell if it
                                                 // must write the <img> tag to the file or not.  It is set to
                                                 // true once the <img> tag is written.
    this.currentImg=0;                           // this.currentImg tracks the images currently being displayed    this.align;                                  // this.align is the "align" tag for the image.  if left blank,                                                 // no tag is generated.	this.stylestring;							 // used to add style attributes to the animation	this.altstring;								 // used to add "ALT" text to the animation

    this.initialize();                           // see documentation for setup() function
}



// delayset()
// the delayset(arg) function is linked to the Animation object as the setDelay(arg) function.
// It allows the user to set the speed of the animation.

function delayset(newval)
{
    this.delay = newval;                        // set this.delay equal to the argument passed
}
Animation.prototype.setDelay = delayset;        // links this function to the Animation object as setDelay()



// alignset()
// the alignset(arg) function is linked to the Animation object as the setAlign(arg) function.
// It allows the user to set the alignment of the animation.

function alignset(newval)
{
    this.align = newval;                        // set this.delay equal to the argument passed
}
Animation.prototype.setAlign = alignset;        // links this function to the Animation object as setDelay()
// styleset()// the styleset(arg) function is linked to the Animation object as the setStyle(arg) function.// Pass in a valid CSS formatting string and it will be applied to the animation object
function styleset(newval)
{	this.stylestring=newval;
}
Animation.prototype.setStyle = styleset;


// altset()// the altset(arg) function is linked to the Animatio object as the setAlt(arg) function.// It sets the ALT tag replacement for the animation object.function altset(newval){
	this.altstring=newval;}Animation.prototype.setAlt = altset;

// go()
// the go() function is linked to the Animation object as the animate() function.
// it is responsible for (a) writing the image code to the page, and (b) performing
// the actual task of animation.

function go()
{
    // If the image has not yet been written, then the following must occur:
    if(!this.exists)
    {
        for(ct=0;ct<this.length;ct++){this.images[ct].src=this.frames[ct];} // preload the images (all of them)
        document.write("<img src=\"" + this.frames[0] + "\"");
        if(this.align)
			document.write(" align=\"" + this.align + "\"");					// apply optional align attribute
		if(this.stylestring)
			document.write(" style=\"" + this.stylestring + "\"");			// apply optional style attribute
		if(this.altstring)
			document.write(" alt=\"" + this.altstring + "\"");				// apply optional alt text attribute		document.writeln(">");        this.exists = true;                                                 // set this.exists, so these operations                                                                            // are only performed once.
    }

    document.images[this.location].src=this.frames[this.currentImg];        // update the Animation with the current Image
    this.currentImg++;                                                      // increment the current image,
    this.currentImg%=this.length;                                           // then mod by this.length to keep
                                                                            // currentImg a valid subscript

    // A big tip of the hat to Jonathan Griffith, who pointed me towards this method of getting my callbacks
    // to work.  Thank you Jon!

    var dyn_instruction = "Animation.callback" + this.location + " = this;";
    eval(dyn_instruction);                      // these lines set up a static member of the Animation object called
                                                // callbackN where N is the number of the Animation's location.

    dyn_instruction = "setTimeout('Animation.callback" + this.location;
    dyn_instruction+= ".animate()',this.delay);";
    eval(dyn_instruction);                      // these lines use the static member above to call the animate()
                                                // method of the proper Animation object after a given amount
                                                // of time has passed.
}
Animation.prototype.animate = go;               // links this function to the Animation object as animate()



// setup()
// the setup() function is linked to the Animation object as the initialize() function.
// It is responsible for initializing the this.frames and this.images arrays.

function setup()
{
    for(ct=0;ct<this.length;ct++)
    {
        this.frames[ct]="";                       // set each element of this.frames equal to a blank string
        this.images[ct] = new Image();            // set each element of this.images equal to a new blank Image
    }
}
Animation.prototype.initialize = setup;           // links this function to the Animation object as initialize()
