// Globals:
var tmDelay = 100;
var fishLeft = 400;
var rangetop = -400;
var rangebottom = -200;
var fishTop = rangetop;
var fishStep = 0;
var numFish = 18;
var flipper = 2; //variable to flip between ascending or descending images
var showme = 0;
var aryFish = new Array(numFish);

function dropFish() {
	initFish();
	shiftFish();
}

function shiftFish() {
	if ( (flipper % 2) == 0 ) {
		incrementFish();
	} else {
		decrementFish();
	}
	fishTop += 2;
	if (fishTop >= rangebottom) { fishTop = rangetop; }

	setTimeout('shiftFish()',tmDelay);
}

function showThisFish(id) {
	aryFish[id].style.top = fishTop+'px';
	aryFish[id].style.visibility = 'visible';
	aryFish[id].style.display = 'block';
}

function hideThisFish(id) {
	aryFish[id].style.visibility = 'hidden';
	aryFish[id].style.display = 'none';
	aryFish[id].style.top = rangetop+'px';
}

function incrementFish() {
	fishStep += 1;
	if (fishStep > numFish) { 
		if (flipper == 1) { flipper = 2; }
		else { flipper = 1; }
	}
	for (var y = 1; y < numFish; y++) { //This is 1-based because fish[0] was displayed by the decrement
		if (y == fishStep-1) { 			//loop and doesn't need to be repeated here.
			//hide the previous fish:
			if (y == 1) { hideThisFish(numFish-1); }
			else { hideThisFish(y-1); }
			showThisFish(y);
		} 
	}
}

function decrementFish() {
	fishStep -= 1;
	if (fishStep < 1) { 
		if (flipper == 1) { flipper = 2; }
		else { flipper = 1; }
	}
	for (var z = numFish-1; z > 0; z--) {
		if (z == fishStep-1) { 
			//hide the previous fish:
			if (z == numFish-1) { hideThisFish(0); }
			else { hideThisFish(z+1); }
			showThisFish(z);
		} 
	}
}

function initFish() {
	for (n = 1; n <= numFish; n++) {
		var fish = 'fish' + n;
		//alert ("fish is " + fish);
		aryFish[n-1] = getObject(fish);
		aryFish[n-1].style.left = fishLeft+'px';
	}
}

function getObject( sObj ) {
	var oElement;

	if ( document.getElementById ) {
		oElement = document.getElementById( sObj );
	} else if ( document.all ) {
		oElement = document.all.item( sObj );
	} else {
		oElement = null;
	}
	return oElement;
}

