// Multiple images in an HTML document may be animated, with
// a random (but constrained) delay between animations, and a
// fixed frame rate. A fixed size pool of animations is defined
// and a random subset of these are chosen. Different pools of
// animations may be used for each image, although the pools must
// currently all contain the same number of images.

// This is important, must be new enough
browserVer = parseInt(navigator.appVersion);

if ( browserVer >= 4)
{
  // 'Constants'
  numframes = 16;
  totalanims = 7;              // Available
  numanims = 7;                // Number to use
  framedelay = 50;
  pauselowerdelay = 1500;      // Shortest delay between animations
  pauseupperdelay = 5000;      // Longest ...
  animimgext = ".png";
  animbasepath = "";
  animinit = "fadein";           // Always begin with this animation
  var animprefixes = new Array( totalanims );  // Different animations
  animprefixes[0] = animinit;
  animprefixes[1] = "flare";
  animprefixes[2] = "spin1_";
  animprefixes[3] = "surface1_";
  animprefixes[4] = "spin1_";
  animprefixes[5] = "flare";
  animprefixes[6] = "surface1_";
  // Create and initialise image names (for array refs)
  numimages = 1;
  var imgs = new Array( numimages );  // Different pools
  imgs[0] = 'logo_1';
  var imgprefixes = new Array( numimages ); // Each different image
  imgprefixes[0] = "y_";
  // Create and initialise animation filenames
  var animfiles = new Array( numimages );
  for( image = 0; image < numimages; image++ )  
  {
    animfiles[image] = new Array( totalanims );
    for( anim = 0; anim < totalanims; anim++ )
    {
      animfiles[image][anim] = new Array( numframes );
      for( frame = 0; frame < numframes; frame++ )
      {
        animfiles[image][anim][frame] = animbasepath + imgprefixes[image];
        animfiles[image][anim][frame] +=  animprefixes[anim] + (frame+1*1) + animimgext;
      }
    }
  }
  // Pre load images for a subset of defined animations
  var frames = new Array( numimages );
  for( image = 0; image < numimages; image++ )
  {
    var uselist = SelectAnimations( numanims );
    frames[image] = new Array( numanims );
    for( anim = 0; anim < numanims; anim++ )
    {
      frames[image][anim] = new Array( numframes );
      for( frame = 0; frame < numframes; frame++ )
      {
        frames[image][anim][frame] = new Image();
        frames[image][anim][frame].src = animfiles[image][uselist[anim]][frame];
      }
    }
  }
  // Let's go
  var curanims = new Array( numimages );
  var curframes = new Array( numimages );
  var pausedelays = new Array( numimages );
  var animtimers = new Array( numimages );
  var frametimers = new Array( numimages );
  // Force initial animation
  for( image = 0; image < numimages; image++ )
      curanims[image] = 0;
  SetAnimTimers();
}
/////// End of initialisation ////////////////////////

// Select a random subset of all animations
function SelectAnimations( num )
{
   var choices = new Array( num+1 );
   choices[0] = 0;  // Force initial animation
   for( choice = 1; choice < num; choice++ )
   {
     done = 0;
     while( done == 0 )
     {
       choices[choice] = Math.floor(Math.random()*(totalanims-1))+(1*1);
       done = 1;
       for( test = 0; test < choice; test++ )
         if ( choices[choice] == choices[test] )
           done = 0;
     }
   }
   return choices;
}

function SetAnimTimer( i )
{
  animtimers[i] = setTimeout('Animate('+i+')', pausedelays[i] );
}

function SetAnimTimers()
{
  pausedelays = NewPauseDelays();
  for( i = 0; i < numimages; i++ )
     SetAnimTimer( i );
}

function SetFrameTimer( i )
{
  frametimers[i] = setTimeout('NextFrame('+i+')', framedelay );
}

function SetFrameTimers()
{
  for( i = 0; i < numimages; i++ )
     SetFrameTimer( i );
}

// Returns a random number bounded by some constants
function NewPauseDelay()
{
  var diff = pauseupperdelay-pauselowerdelay;
  var newdelay = Math.floor(Math.random()*diff)+(1*pauselowerdelay);
  return newdelay;
}

// Returns an array of random numbers bounded by some constants
function NewPauseDelays()
{
  var newdelays = new Array( numimages );
  for( i = 0; i < numimages; i++ )
    newdelays[i] = NewPauseDelay();
  return newdelays;
}

// Returns an array of random animations
function NewAnimation()
{
  // Ensure we don't choose the introductory animation
  var newanim = Math.floor(Math.random()*(numanims-1))+(1*1);
  return newanim;
}

// Starts the next animation
function Animate( i )
{
  if ( curanims[i] < 0 )
    curanims[i] = NewAnimation();
  curframes[i] = 0;
  SetFrameTimer( i );
}

// Loads the next frame and decides what happens thereafter
function NextFrame( i )
{
  document.images[imgs[i]].src = frames[i][curanims[i]][curframes[i]].src;  
  curframes[i]++;
  if ( curframes[i] == numframes )
  { // Stop animating, choose new delay
    curframes[i] = -1;
    curanims[i] = -1;
    pausedelays[i] = NewPauseDelay();
    SetAnimTimer( i );
  }
  else
    SetFrameTimer( i );
}

function CleanUp()
{
  for( i = 0; i < numimages; i++ )
  {
    clearTimeout( animtimers[i] );
    clearTimeout( frametimers[i] );
  }
}
