// First setup the array of images to be shown
var randomimages=new Array()
var i = 0;

var count = 1;
var imageprefix = "";
var noImages = 0;

var now1 = now2 = new Date(); // will be used in timeout checking - both will change
var timeAtLoad = new Date(); // will be used in timeout checking - fixed
var timeSinceLoad=0;

var maxtimeout=20 // set maximum time (sec) before aborting loading of an image
var stopScriptAfter=60*5 //time in seconds after which to stop running the image changing script

var doChangeImages = true; // If 1 then images will be changed, otherwise they will not

// A random number generator with bounds set by parameters
function getRandomNum(lbound, ubound) {
  return (Math.round(Math.random() * (ubound - lbound)) + lbound);
}

// Toggle image changing
function toggleImageChange()
{
	if (doChangeImages)
		{doChangeImages = false;
		//alert("Image changing now OFF")
		}
	else
		{doChangeImages = true;
		//alert("Image changing now ON");
		setTimeout("dorotate()",2000);};		
}


// this function chooses a new image and starts to preload it
function loadNewImage(posnName)
{
	// now start choose the next image to load and start doing so
	if (posnName=="imgPosn1"){lowNo=imgPosn1Range[0];highNo=imgPosn1Range[1]};
	if (posnName=="imgPosn2"){lowNo=imgPosn2Range[0];highNo=imgPosn2Range[1]};
	if (posnName=="imgPosn3"){lowNo=imgPosn3Range[0];highNo=imgPosn3Range[1]};
	if (posnName=="imgPosn4"){lowNo=imgPosn4Range[0];highNo=imgPosn4Range[1]};
	
	nextPosnName=posnName;
	nextImageNo=getRandomNum(lowNo, highNo);
	
	//alert("Posn = " + posnName + "\rimage = " + imageNo);
	
	if (document.images) {
		nextimg.src = randomimages[nextImageNo]; // start loading the next image
		now1 = new Date();
	}
}

// this function does the image swapping
function rotateimage(posnName)
{
// if the next image has loaded, show it and start loading another, otherwise 
// exit rotateimage so the current next image continues loading
//   alert("Do rotateiamge");

if (nextimg.complete)
{
//   alert("nextimg.complete = TRUE");
//   window.status='Swap image';
   window.status='    Anglia Airports Express - Taxis to and from Stansted, Heathrow, Gatwick, and Luton Airports';
   document.images[nextPosnName].src=randomimages[nextImageNo];
   loadNewImage(posnName);
}
else 
{   
//   alert("nextimg.complete = FALSE");
   now2 = new Date();
   if ((Date.parse(now2) - Date.parse(now1)) > maxtimeout*1000) 
   {
      window.status = 'Give up on ' + randomimages[nextImageNo] + ' - try a new image';
      loadNewImage(posnName);
   }
   else
   {
      window.status = 'Still trying to load ' + randomimages[nextImageNo];
   }
}
}

function wait()  // junk function that just returns the value true
{
   return true
}

// Objective: If this page is loaded from http location, stop changing images after a specified time.
// Solution: Repeatedly calls itself to change the images.
// Stops calling after a time 'stopScriptAfter', so images freeze.
// Does not freeze if this page is loaded from a 'file'.
function dorotate()
{
	a=Math.floor(Math.random()*imagePosns)+1; // change one of the image positions
	rotateimage('imgPosn' + a);
	b=getRandomNum(mindelay,maxdelay); // now wait a bit before doing it again
	timeSinceLoad=(Date.parse(new Date()) - Date.parse(timeAtLoad))/1000 ;
	// alert('timeSinceLoad = ' + timeSinceLoad );
	if (doChangeImages && ((timeSinceLoad < stopScriptAfter) || (pageTypeStr=='file')))   // change images if TRUE
		{
			setTimeout("dorotate()",b);
		}
		else
		{
			// alert("No image changing");
			return;
		}
	
	}