//Written by Kevin E. Kohler <kevin at nova dot edu>

// Define time (msecs) to wait between image swaps
var WaitTime = 1500
var WaitTime2= 2*WaitTime

//Number of images to fill
var numpix = 5

//Number of images available
var availpix = 19

// Define showimage array. 
// Set to -999 initially
  shownimage = new Array(numpix)
  for ( var i=0; i<numpix; i++) {
      shownimage[i] = -999
  }

//Cache needed images
var simage = new Array()

for ( var i=0; i<availpix; i++) {
    if (i==0 || i==1 || i==8 || i==9 || i==10 || i==16 || i==17 || i==18) { 
       simage[i]= new Image(100,150)
    } else {
       simage[i]= new Image(150,100)
    }
    simage[i].src="images/montage150_" + (i+1) + ".jpg"
}

//Define available portrait images
var numport=7
var portrand = new Array( )
portrand[0]=1
portrand[1]=2
portrand[2]=9
portrand[3]=10
portrand[4]=11
portrand[5]=17
portrand[6]=19

//Define available landscape images
var numland=10
var landrand = new Array( )
landrand[0]=3
landrand[1]=4
landrand[2]=5
landrand[3]=6
landrand[4]=7
landrand[5]=8
landrand[6]=12
landrand[7]=13
landrand[8]=14
landrand[9]=15

var lastimg=-999
var prand=-999
var ntime=0
var to
function getrndm(min,max) {
  return Math.round(Math.random()* max)+ min
}

function image_swap() {

 ntime=ntime+1
 if (ntime % 10 == 0) {  //Fill with reef image every 10 times

   ntime=0
   for ( var i = 0; i < numpix; i++) {   
     swapdoc="document.pic" + (i+1) + ".src=" + '"images/montagereef_' + (i+1) + '.jpg"'
     eval(swapdoc)
   }         
   
   // Reset showimage array to -999
       shownimage = new Array(numpix)
       for ( var i=0; i<numpix; i++) {
        shownimage[i] = -999
       }

   // Wait and execute again (wait twice as long if have filled with reef image)
     to = setTimeout("image_swap()",WaitTime2)

 } else  {               //Randomly fill images

  // Get random number for image to change. Do not use previous image.  

     while (prand==lastimg) {
       prand=getrndm(0,numpix-1)
     }

     lastimg=prand

  // Is this landscape or portrait
     var land = (prand >= 2)

  // Get random number according to landscape or portrait
     if (land) {
       maxt=numland-1
     }  else  {
       maxt=numport-1 
     }
     trand=getrndm(0,maxt)

  // See if this image is already shown
     var doit=1
     while (doit==1) {

       if (land) {
         picnum=landrand[trand]
       } else {
         picnum=portrand[trand]
       }

       match=0
       for ( var i = 0; i < numpix; i++) {   
         if (picnum == shownimage[i]) {
            match=match+1  
         }
       } 
       
       // Check if there was a match. If so, add one and try again
          if (match!=0) {
             trand=trand+1
             trand= (trand % (maxt+1))
          }  else {    //no matches, so we can break out of while loop
             doit=0    //this exits the while loop
          }
     }  //end of while loop

  // Fill shownimage array with new image number
     shownimage[prand]=picnum

     swapdoc="document.pic" + (prand+1) + ".src=" + '"images/montage150_' + picnum + '.jpg"'
     eval(swapdoc)

  //Wait and execute again
     to= setTimeout("image_swap()",WaitTime)

 }  //End of ntime else loop

}   //End of image_swap function


