var fish_number = 0;
var aFishes = new Array();
var cycle_max = 10;
var min_timeout = 20;
var avg_timeout = 20;
var start_timeout = 10;

function fish(aquarium_name, fish_name) {
	this.aquarium_name = aquarium_name;
	this.fish_name = fish_name;
	this.aquarium = document.getElementById(aquarium_name);
	this.fish = document.getElementById(fish_name);
	this.w_aquarium = 0;
	this.h_aquarium = 0;
	this.directionY = "";
	this.directionX = "";
	this.newLeft = 0;
	this.newTop = 0;
	this.cycle = 0;
	
	ShowFish(fish_name);
	this.w_aquarium = this.aquarium.offsetWidth - this.fish.width;
	this.h_aquarium = this.aquarium.offsetHeight - this.fish.height;
	this.fish.style.left = GetNewLeft(this.w_aquarium) + "px";
	this.fish.style.top = GetNewTop(this.h_aquarium) + "px";
}
function InitFish(aquarium_name, fish_name) {
	aFishes[fish_number] = new fish(aquarium_name, fish_name);
	fish_number++;
	setTimeout("FishGo('" + fish_name + "')", start_timeout*1000);
}
function GetObjByName(fish_name) {
	for (i = 0; i < fish_number; i++) {
		if (aFishes[i].fish_name == fish_name) {
			return aFishes[i];
		}
	}
}
function FishGo(fish_name) {
	var obj = GetObjByName(fish_name);
	obj.newLeft = GetNewLeft(obj.w_aquarium);
	obj.newTop = GetNewTop(obj.h_aquarium);

	var proportion = (obj.newTop - parseInt(obj.fish.style.top))/(obj.newLeft - parseInt(obj.fish.style.left));
	if (proportion < 0) { proportion = proportion * -1; }
	var n = 100;

	if (proportion > 1) {
		MoveFishY(fish_name, n);
		MoveFishX(fish_name, parseInt(n*proportion));
	}
	else {
		MoveFishY(fish_name, parseInt(n/proportion));
		MoveFishX(fish_name, n);
	}
	if (obj.newTop - parseInt(obj.fish.style.top) > 0) {
		obj.directionY = "down";
	}
	else {
		obj.directionY = "up";
	}
	if (obj.newLeft - parseInt(obj.fish.style.left) > 0) {
		obj.directionX = "right";
	}
	else {
		obj.directionX = "left";
	}
}
function MoveFishY(fish_name, interval) {
	var obj = GetObjByName(fish_name);
	if (parseInt(obj.fish.style.top) == obj.newTop) {
		if (parseInt(obj.fish.style.left) == obj.newLeft) {
			obj.cycle++;
			if (obj.cycle <= cycle_max) {
				setTimeout("FishGo('" + fish_name + "')", GetTimeout());
			}
		}
		return;
	}
	if (obj.directionY == "up") {
		obj.fish.style.top = parseInt(obj.fish.style.top) - 1 + "px";
	}
	else {
		obj.fish.style.top = parseInt(obj.fish.style.top) + 1 + "px";
	}
	setTimeout("MoveFishY('" + fish_name + "', " + interval + ")", interval);
}
function MoveFishX(fish_name, interval) {
	var obj = GetObjByName(fish_name);
	if (parseInt(obj.fish.style.left) == obj.newLeft) {
		if (parseInt(obj.fish.style.top) == obj.newTop) {
			obj.cycle++;
			if (obj.cycle <= cycle_max) {
				setTimeout("FishGo('" + fish_name + "')", GetTimeout());
			}
		}
		return;
	}
	if (obj.directionX == "right") {
		obj.fish.style.left = parseInt(obj.fish.style.left) + 1 + "px";
	}
	else {
		obj.fish.style.left = parseInt(obj.fish.style.left) - 1 + "px";
	}
	setTimeout("MoveFishX('" + fish_name + "', " + interval + ")", interval);
}
function GetTimeout() {
	var n = 1000*avg_timeout;
	return min_timeout*1000+Math.round(Math.random()*n)
}
function GetNewLeft(w_aquarium) {
	return Math.round(Math.random()*w_aquarium);
}
function GetNewTop(h_aquarium) {
	return Math.round(Math.random()*h_aquarium);
}
function HideFish(fish_name) {
	document.getElementById(fish_name).style.visibility = "hidden";
	document.getElementById(fish_name).style.display = "none";
}
function ShowFish(fish_name) {
	document.getElementById(fish_name).style.visibility = "visible";
	document.getElementById(fish_name).style.display = "block";
}

