var timeout_id = null;  // Allows us to stop the animation with a function to be added later...
function animate(image_node, num_images, millsec, images, image_idx)
{
    var local_millsec = millsec;
    image_idx = image_idx % num_images;
    if (image_idx == 0)
    {
        if (millsec <= 500)
            local_millsec = local_millsec * 3/2;
    }
    if (image_idx == num_images-1)
    {
        if (millsec <= 500)
            local_millsec = local_millsec * 3;
    }
//document.writeln("<p>image_idx = " + image_idx + " num_images = " + num_images + "</p>");
    image_node.src = images[image_idx].src;
//document.writeln("<p>image src = " + image_node.src + "</p>");
    image_idx++;

    // recursive call
//document.writeln("<p>animate</p>");
    timeout_id = setTimeout(function () {animate(image_node, num_images, millsec, images, image_idx); }, local_millsec);
//document.writeln("<p>animate</p>");
}

//
// loadImages(id_of_image, time_each_image, list of image files to show)
//
function loadImages(/* variable number of arguments */)
{
//document.writeln("<p>loadImages called</p>");
    var image_node_name = arguments[0];
    var millsec = arguments[1];
    var frames = new Array;
//document.writeln("<p>arguments.length = " + arguments.length + "</p>");
    for (var i = 2; i < arguments.length; i++)
    {
        frames[i-2] = new Image();
//document.writeln("<p>LoadImages i = " + i + "</p>");
        frames[i-2].src = arguments[i];
//document.writeln("frames[i] = " + frames[i-2].src + "</p>");
    }
//document.writeln("image_node_name = " + image_node_name + "</p>");
    var image_node = document.getElementById(image_node_name);
//if (image_node)
//document.writeln("image_node found</p>");
//else
//document.writeln("image_node NOT found</p>");

    animate(image_node, arguments.length-2, millsec, frames, 0);
}

