//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 = 12 

// 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==2 || i==7 || i==9) { 
       simage[i]= new Image(148,225)
    } else {
       simage[i]= new Image(225,148)
    }
    simage[i].src="images/montagepix225_" + (i+1) + ".jpg"
}

//Define available portrait images
var numport=5
var portrand = new Array( )
portrand[0]=1
portrand[1]=2
portrand[2]=3
portrand[3]=8
portrand[4]=10

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

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

function image_swap() {

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

   ntime=0
   for ( var i = 0; i < numpix; i++) {   
     swapdoc="document.pic" + (i+1) + ".src=" + '"images/montagebck225_' + (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 >= 3)

  // 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/montagepix225_' + picnum + '.jpg"'
     eval(swapdoc)

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

 }  //End of ntime else loop

}   //End of image_swap function

 function setMsg (msg) {
             window.status = msg
             return true
 }

