// Fish files are stored like
//    fishImagePrefix + fishNum + "-" + imageNum + fishImagePostfix
// Example:
//    "images/fish" + "1" + "-" + "1" + ".gif"
//    Turns into --> "images/fish1-1.gif"
// imageNum:
//    3 = Upper Left                   1 = Upper Right
//    4 = Lower Left                   2 = Lower Right
var fishImagePrefix = "images/fish/fish";
var fishImagePostfix = ".gif";

// Width of the layer (really necessary?)
var width = 10;

// Fish objects
var fishTotal = 6;
var fishObjs = new Array(fishTotal);

// Maximum deviation from the mouse (eg. 100 is mouse_position plus/minus 100)
var yBase = 100;
var xBase = 100;

// Delay between animation frames, measured in milliseconds
var delay = 15;

// Amount to move along in the loop
var step = .08;

// Where to start in the loop
var currStep = 0;

// How far should a fish need to move before adding the distancePercent?
// if (fish needs to move > moveMax)
//    fish_moves = moveMax + (needs_to_move * distancePercent)
// Smooths out fish movements
var moveMax = 8;
var distancePercent = 0.001;

// Inertia -- Set this REALLY low for a MASSIVE fish.  Inertia of 1 means
// that inertia should NOT be taken into consideration
var inertiaPercent = 0.02;

// Where the fish are supposed to swim to.  This is set by MoveHandler
var Xpos = 100;
var Ypos = 100;

// Temporary variables
var i, j, fishDirection, newTop, newLeft;


// Write the CSS that we need for each fish.
for (i = 0; i < fishTotal; i ++)
{
    writeCSS(css("fish" + i, 0, 0, width, null, null, "show"));
}


if (min)
{
    // Initialize the fish
    function initFish()
    {
        for (i = 0; i < fishTotal; i ++)
        {
            if (NS)
            {
	        // Netscape uses layers
                document.layers["fish" + i] = new Layer(width);
	    }
	    if (IE)
	    {
	        // IE uses DIV tags
	        document.body.insertAdjacentHTML("BeforeEnd",
	            "<DIV ID='fish" + i + "' STYLE='position:absolute;'>" +
		    "</DIV>");
	    }
	    
	    // Make the fish, show the fish, add the image tag
	    fishObjs[i] = new dynObj("fish" + i);
	    fishObjs[i].show();
	    fishObjs[i].write("<img src=\"" + fishImagePrefix +
	        (i+1) + "-1" + fishImagePostfix + "\" name=\"fish" + i + 
		"\">");
	    fishObjs[i].fishName = "fish" + i;
	    
	    // Netscape gives me the proper window width and height, so
	    // make the fish appear randomly in the window.  :-)
	    if (NS)
	    {
	        fishObjs[i].moveTo(Math.random() * window.innerWidth,
	            Math.random() * window.innerHeight);
	    }
	    
	    // Set the initial values for inertia and the pattern
	    fishObjs[i].inertia = 0;
	    fishObjs[i].yinertia = 0;
	    fishObjs[i].pattern = i + 1;
	    fishObjs[i].ypattern = fishTotal - i;
	    
	    // Set up the fish images
	    fishObjs[i].imageArray = new Array(4);
	    for (j = 0; j < 4; j ++)
	    {
	        fishObjs[i].imageArray[j] = new Image;
		fishObjs[i].imageArray[j].src = fishImagePrefix + (i+1) + 
		    "-" + (j+1) + fishImagePostfix;
	    }
	}
	
	// Netscape needs to capture the events explicitly.
	if (NS)
	{
            window.captureEvents(Event.MOUSEMOVE);
	}

        // Set up the move handler, and start the fish swimming.
        window.onmousemove = MoveHandler;
        document.onmousemove = MoveHandler;
        swimFish();
    }
    
    window.onload = initFish;

    // If we move the mouse, set the things here.
    function MoveHandler(e) {
        if (NS)
	{
	    Xpos = e.pageX ;
            Ypos = e.pageY ;
	}
	else
	{
	    Xpos = event.x;
	    Ypos = event.y;
            if (IE5)
            {
                Xpos += document.body.scrollLeft;
                Ypos += document.body.scrollTop;
            }
	}
    }

    // Move the fish, then set up another call to swimFish
    function swimFish() {
        for ( j = 0 ; j < fishTotal ; j++ ) {
	    newTop = Ypos + 
	        Math.cos((20*Math.sin(currStep/(20+j)))+fishObjs[j].ypattern)
		*yBase*(
		    Math.sin(20+currStep/20)+0.2)
		*Math.cos((currStep + j*25)/10);
            newLeft = Xpos +
                Math.sin((20*Math.sin(currStep/20))+fishObjs[j].pattern)
		*xBase*(
		    Math.sin(20+currStep/(20+j))+0.2)
		*Math.cos((currStep + j*25)/10);

            // Change the newTop and newLeft from absolute positions to
	    // relative positions.
            newTop -= fishObjs[j].top;
	    newLeft -= fishObjs[j].left;

            // Find the fish direction
            fishDirection = 0;
	    if (newLeft < 0)
	        fishDirection += 2;
	    if (newTop > 0)
	        fishDirection += 1;

            // Change the image accordingly
	    fishObjs[j].doc.images[fishObjs[j].fishName].src =
	        fishObjs[j].imageArray[fishDirection].src;
	    
	    // Take into account inertia
	    newTop = physics(0, newTop, j);
	    newLeft = physics(1, newLeft, j);

            // Move the fish
            fishObjs[j].moveBy(newLeft, newTop);
        }
        currStep += step;
        setTimeout("swimFish()", delay) ;
    }
    
    // This takes in a few parameters
    //   isLeft = is the .left attribute?  True if it is
    //   wantedValue = the amount we want to move the fish by
    //   fishNumber = the number of the fish
    function physics(isLeft, wantedValue, fishNumber)
    {
        // First, move just moveMax pixels, with a slight increase determined
	// by distancePercent.
        if (wantedValue > moveMax)
	{
	    wantedValue = moveMax + 
	        (wantedValue - moveMax) * distancePercent;
	}
        if (wantedValue < - moveMax)
	{
	    wantedValue = - moveMax + 
	        (wantedValue + moveMax) * distancePercent;
	}
	
	// If it is the left, use the yinertia.  If not, use inertia.
	// This section then takes into account inertia, using the
	// inertiaPercent above to represent the 'mass' of the object.
	if (isLeft)
	{
	    wantedValue = (fishObjs[fishNumber].yinertia * 
	         (1 - inertiaPercent)) +
	        (wantedValue * inertiaPercent);
	    fishObjs[fishNumber].yinertia = wantedValue;
	}
	else
	{
	    wantedValue = (fishObjs[fishNumber].inertia * 
	         (1 - inertiaPercent)) +
	        (wantedValue * inertiaPercent);
	    fishObjs[fishNumber].inertia = wantedValue;
	}
	
	// Done -- return the new value we will move by
	return wantedValue;
    }    
}