var FISH_IMG = ["img/fish-1.png", "img/fish-2.png", "img/fish-3.png", "img/fish-4.png"];

/**
 *	Initialize the fish
 */
function fish_instance() {
	for (var idx in Fish) {
		this[idx] = Fish[idx];
	}
	
	this.__construct();
	
	return this;
}


var Fish = {
	
	s_fish_nr : -1,
	image : null,
	reverse_image : null,
	x : 0,
	y : 0,
	dx : 0,
	dy : 0,
	bubbles : [],
	nBubbles : 0,
	angle : 0,
	swim_to_x : 0,
	swim_to_y : 0,
	till_next_bubble : 0,
	speed : 0.02,

	__construct : function() {
		
		Fish.s_fish_nr = (Fish.s_fish_nr + 1) % FISH_IMG.length;
			
		this.image = document.createElement("img");
		this.image.src = TANK_BASE + FISH_IMG[Fish.s_fish_nr];

		// current position
		this.x = this.new_position_x();
		this.y = this.new_position_y();
		
		// swim to location
		this.swim_to_x = this.new_position_x();
		this.swim_to_y = this.new_position_y();

		this.nBubbles = 0;
		this.bubbles = []; 
				
		this.new_bubble();
		this.setup_direction_vector();

	},
	
	/**
	 *	Determine the direction they should swim to
	 */
	setup_direction_vector : function() {

		this.dx = (this.swim_to_x - this.x);
		this.dy = (this.swim_to_y - this.y);

		var len = Math.sqrt(this.dx * this.dx + this.dy * this.dy);

		this.dx /= len;
		this.dy /= len;
	},
	
	new_bubble : function() {
		this.till_next_bubble = 300 + Math.round(Math.random() * 2000);
	},
		
	new_position_x : function() {
		return Math.round(Math.random() * (600 - this.image.width));
	},
	
	new_position_y : function() {
		return this.image.height + Math.round(Math.random() * (480 - DRAW_HEIGHT - this.image.height));
	},
	
	/**
	 *	This method moves the fish a step into the right direction
	 */
	move_step : function(steps) {
	
		// move the fist
		this.x += this.dx * steps * this.speed;
		this.y += this.dy * steps * this.speed;
		
		if (this.y > this.swim_to_y - 2 && this.y < this.swim_to_y + 2 &&
			this.x > this.swim_to_x - 2 && this.x < this.swim_to_x + 2) {
				this.swim_to_x = this.new_position_x();
				this.swim_to_y = this.new_position_y();
	
				this.setup_direction_vector();
		}

		// move the bubbles
		for (var idx = 0; idx < this.nBubbles; ++idx) {
		
			if (this.bubbles[idx] == null) {
				continue;
			}
			
			var result = this.bubbles[idx].move_step(steps);
			
			// if no longer necessary, move last to current position and decrease nBubbles: 
			// O(1) remove 
			if (!result) {
				if (this.nBubbles == this.idx) {
					this.bubbles[idx] = null;
					--this.nBubbles;
				}
				else {
					this.bubbles[idx] = this.bubbles[this.nBubbles - 1];
					this.bubbles[this.nBubbles - 1] = null;
					--this.nBubbles;
					--idx;
				}
			}
		}

		// is there a new bubble needed?
		this.till_next_bubble -= steps;
		if (this.till_next_bubble < 0 && this.nBubbles < 5) {

			// add bubble to array		
			if (this.dx > 0) {
				this.bubbles[this.nBubbles] = new bubble_instance(this.x + this.image.width / 2, this.y);
			}
			else {
				this.bubbles[this.nBubbles] = new bubble_instance(this.x - this.image.width / 2, this.y);
			}
			++this.nBubbles;
			this.new_bubble();
		}
	},
	
	/**
	 *	Draw the fish on the screen
	 */
	draw_fish : function(context) {
	
		// draw the bubbles
		for (var idx = 0; idx < this.nBubbles; ++idx) {
			this.bubbles[idx].draw(context);
		}
	
		context.save();
		if (this.dx >= 0) {
			context.translate(this.x, this.y + DRAW_HEIGHT);
			context.drawImage(this.image, -this.image.width / 2, -this.image.height / 2);
		}
		else {
			this.angle += 0.01;
			context.translate(this.x, this.y + DRAW_HEIGHT);
			context.scale(-1, 1);
			context.drawImage(this.image, -this.image.width / 2, -this.image.height / 2);
		}
		context.restore();
	}

};

