//Swimming Fish - by SavetheDolphins.org
//Modified from: Flying planes - by John Ely
//Modified from: Autumn leaves script- by Kurt Grigg (kurt.grigg@virgin.net)
//Modified by Dynamic Drive for NS6 functionality
//visit http://www.dynamicdrive.com for this script

//Pre-load your image below!
//CHANGE 5 to the number of images listed below
grphcs=new Array(8) 

//PRELOAD the involved images (extend or contract variables according to # of images used)
Image0=new Image();
Image0.src=grphcs[0]="./images/fish0.gif"
Image1=new Image();
Image1.src=grphcs[1]="./images/fish1.gif"
Image2=new Image();
Image2.src=grphcs[2]="./images/fish2.gif"
Image3=new Image();
Image3.src=grphcs[3]="./images/fish3.gif"
Image4=new Image();
Image4.src=grphcs[4]="./images/fish4.gif"
Image5=new Image();
Image5.src=grphcs[5]="./images/fish5.gif"
Image6=new Image();
Image6.src=grphcs[6]="./images/fish6.gif"
Image7=new Image();
Image7.src=grphcs[7]="./images/fish7.gif"


//SPECIFY number of images to randomly display concurrently from list above. Less the more efficient
Amount=4; 
MAX_RANDOMS = 50;
Ypos=new Array();
Xpos=new Array();
Speed=new Array();
Step=new Array();

Sinewave = new Array( Amount );
nextSine = new Array( Amount );

Random = new Array( MAX_RANDOMS );

// don't let the fish fly over the banner on islandsendstjohn.com
IslandOffset = 200;

ns = ( document.layers ) ? 1 : 0;
ns6 = ( document.getElementById && !document.all ) ? 1 : 0;
ie = document.all

// render html, pick the fish to use
if( ns ){
  for( i = 0; i < Amount; i++ ){
    var P = Math.floor( Math.random() * grphcs.length );
    rndPic = grphcs[P];
    document.write( "<LAYER NAME='sn" + i + "' LEFT=0 TOP=0><img src=" + rndPic + "></LAYER>" );
  }
}
else{
  document.write( '<div style="position:absolute;top:-30px;left:0px;z-index:1"><div style="position:relative">' );
    for( i = 0; i < Amount; i++ ){
      var P = Math.floor( Math.random() * grphcs.length );
      rndPic = grphcs[P];
      document.write( '<img id="si'+i+'" src="'+rndPic+'" style="position:absolute;top:-30px;left:0px">' );
  }
  document.write('</div></div>');
}

// get geometry
WinHeight =( ns || ns6 ) ? window.innerHeight - 85 : window.document.body.clientHeight;
WinWidth = ( ns || ns6 ) ? window.innerWidth - 85 : window.document.body.clientWidth;
var hscrll = ( ns || ns6) ? window.pageYOffset : document.body.scrollTop;
var wscrll = ( ns || ns6) ? window.pageXOffset : document.body.scrollLeft;

// build starting params
for (i=0; i < Amount; i++){                                                                

  Ypos[i] = Math.round( Math.random() * WinHeight );
  Ypos[i] += IslandOffset;
 // if( Ypos[i] > WinHeight ) {
 //   Ypos[i] = WinHeight;
 // }
  Xpos[i] = Math.round(Math.random() * WinWidth );

  Speed[i] = Math.random() * 5 + 1;  

	// resolution for sine wave
  Step[i] = (Math.random() * .1 ) + 0.05;
}

// build sine tables
for( i = 0; i < Amount; ++i ) {

  Sinewave[i] = new Array();

  for( j = 0; j < MAX_RANDOMS; ++j ) {
    Random[j] = Math.random();
  }

	Cstep = 0.0;
  while( Cstep <= 6.28329 ) {
    Sinewave[i].push( Math.sin( Cstep ) );
    Cstep += Step[i];
  }
  
  nextSine[i] = 0;
}

nextRandom = 0;

function fly(){  

  WinHeight =( ns || ns6 ) ? window.innerHeight - 85 : window.document.body.clientHeight;
  WinWidth = ( ns || ns6 ) ? window.innerWidth - 85 : window.document.body.clientWidth;
  var hscrll = ( ns || ns6) ? window.pageYOffset : document.body.scrollTop;
  var wscrll = ( ns || ns6) ? window.pageXOffset : document.body.scrollLeft;
 
  for (i=0; i < Amount; i++){

    if( nextSine[i] >= Sinewave[i].length ) {
      nextSine[i] = 0;
    }
    if( nextRandom >= MAX_RANDOMS ) {
      nextRandom = 0;
    }  
  
    sy = Speed[i] * Sinewave[i][nextSine[i]];
    sx = Speed[i] * 1;
    Ypos[i] += sy;
    Xpos[i] += sx; 
		
    if( Xpos[i] > ( WinWidth + 100 ) ){
      Ypos[i] = Math.floor( Random[nextRandom] * WinHeight  );
      Ypos[i] += IslandOffset;
      if( Ypos[i] > WinHeight ) {
        Ypos[i] = WinHeight;
      }
      Xpos[i] =- 60;
      Speed[i] = Random[nextRandom] * 1 + 1;
      
      ++nextRandom;
    }

    if( ns ){
      document.layers['sn'+i].left = Xpos[i] + wscrll;
      document.layers['sn'+i].top = Ypos[i] + hscrll;
    }

    else if( ns6 ){
      document.getElementById( "si" + i ).style.left = Xpos[i] + wscrll;
      document.getElementById( "si" + i ).style.top = Math.min( WinHeight, Ypos[i] ) + hscrll;
    }

    else{
      eval( "document.all.si" + i ).style.left = Xpos[i] + wscrll;
      eval( "document.all.si" + i ).style.top = Ypos[i] + hscrll;
    } 
    ++nextSine[i]; 
  }
  setTimeout( 'fly()', 60 );
}

if( ie||ns||ns6 )
  window.onload=fly
//-->