// *** This is a JavaScript file ***

// *** All code has been written by Martin Sykes ***
// *** This code may be freely copied and modified as long as this comment is included. ***

// *** Globals ***
// For some reason objects cannot be passed into functions. To get around this, 
// I use a global arrays to hold the objects and pass the index as a parameter instead

// HQImages - Holds High Quality images while they load
var HQImages;
HQImages=new Array();

// nHQ - the number of elements currently in HQImages
var nHQ;
nHQ=0;

// *** Functions ***

// Function to start an image loading into HQImages. 
// NOTE: The function returns before the image has finished loading
function preLoadImage( src )
{
  // Allocate the next element
  nHQ++;
  HQImages[nHQ] = new Image;

  // Start the load
  HQImages[nHQ].src = src;

  // Return the index of this image
  return nHQ;
}

// Function to wait until an image finishes loading and then display it
// This is used to stop the image being displayed before it is finished
function setImageWhenComplete(nIndex,name,src)
{
  if (HQImages[nIndex].complete == true)
  {
    // Image is complete
    document.images[name].src = src;
  }
  else
  {
    // Image is not ready so wait another 100ms
    setTimeout("setImageWhenComplete("+nIndex+",\""+name+"\",\""+src+"\")",100);
  }
}

function fadeImageWhenComplete(nIndex,name,src)
{
  if (HQImages[nIndex].complete == true)
  {
    // Image is complete
    document.images[name].style.filter="blendTrans(duration=4)"
    document.images[name].style.filter="blendTrans(duration=crossFadeDuration)"
    document.images[name].filters.blendTrans.Apply()      
    document.images[name].src = src;
    document.images[name].filters.blendTrans.Play()

  }
  else
  {
    // Image is not ready so wait another 100ms
    setTimeout("fadeImageWhenComplete("+nIndex+",\""+name+"\",\""+src+"\")",100);
  }
}





// Function to change an named image for a better one
function improveImage( name, src )
{
  // Start the better image loading
  var nIndex;
  nIndex = preLoadImage(src);

  // When ready, display it
  setImageWhenComplete(nIndex,name,src);
}

// Function to change an named image for a better one
function fadeImproveImage( name, src )
{
  // Start the better image loading
  var nIndex;
  nIndex = preLoadImage(src);

  // When ready, display it
  fadeImageWhenComplete(nIndex,name,src);
}

// Function to fade an named image for a better one
function fadeImage( name, src )
{
  // Start the better image loading
  var nIndex;
  nIndex = preLoadImage(src);

  // When ready, display it
  fadeImageWhenComplete(nIndex,name,src);
}

// Simple function to change an image. Assumes the images are loaded.
function changeIcon(name,src)
{
  document.images[name].src=src;
}

// Simple function to change an image. Assumes the images are loaded.
function fadeIcon(name,src)
{
	fadeImage(name,src);
}

