/*
rotateImg.js 

Will rotate through a list of predefined images

*/

var images = new Array();
var numImages = 4; //number of images in array.
var rotateSpeed = 5000; // # in milliseconds to wait before going to the next image
 
//Images to rotate through
images[0] = new Image();
images[0].src = "images/OutdoorStudy1_Sample.jpg";
images[1] = new Image();
images[1].src = "images/IndoorStudy_Sample.jpg";
images[2] = new Image();
images[2].src = "images/IndoorStudy2_Sample.jpg";
images[3] = new Image();
images[3].src = "images/Mesh3D_Sample.JPG";

var curIMG = -1;  //index of current image

function RotateImages()
{
   if(document.images) //are there images?
   {
     var nextIMG = curIMG + 1;
 
     if(nextIMG >= numImages) nextIMG = 0;
   
        //Check if the image is valid and has completed preloading.
     if (images[nextIMG] && images[nextIMG].complete) 
     {
        var target =null;
        
        if (document.getElementById("studyImages"))
        {
           target=document.getElementById("studyImages");
        }
        
       //if the desired element exists, switch the image and wait some time before going to the next image.
      if (target)
      {
        target.src=images[nextIMG].src;
        curIMG=nextIMG;
      }

      setTimeout("RotateImages()", rotateSpeed);
     }
      else
    {
      setTimeout("RotateImages()", 500); // check after half a second if the next image has been preloaded if previous check failed.
    }
     
 }
}//end ChangeImage()

window.onload = RotateImages; //when the page loads, start the ChangeImage Function.