var DRAGONFLY = "url('images/bugs/dragonfly.png')";

var bugIdCounter = 0;

var BUG_MOVE_RATE = 0.08;


function BugSprite(g) {

	this.game = g;
	this.id = ++bugIdCounter;
	this.moving = false;
	this.direction = getRandomInt(0, 3);
	
	this.counter = 0;
	
	var bg = this.game.getBugGrid();
	var fg = this.game.getFrogGrid();
	
	this.x = getRandomInt(0, ROWS - 1);
    this.y = getRandomInt(0, COLS - 1);
    while (bg[this.x][this.y] || fg[this.x][this.y]) {
    	this.x = getRandomInt(0, ROWS - 1);
        this.y = getRandomInt(0, COLS - 1);
    }
    
    this.setPosition();

    this.contentDiv = document.createElement("div");
    this.contentDiv.onmousedown = function(event) {
    	var lp = this.bug.game.getLillyPadGrid()[this.bug.x][this.bug.y];
    	if (lp) {
    		dragStartLillyPad(event, lp.contentDiv.id);
    	}
    }
    this.contentDiv.bug = this;
    this.contentDiv.id = "bug" + this.id;
    this.contentDiv.style.width = "40px";
    this.contentDiv.style.height = "40px";
    this.contentDiv.style.position = "absolute";
    this.contentDiv.style.top = this.posX + "px";
    this.contentDiv.style.left = this.posY + "px";
    this.contentDiv.style.zIndex = "3";
    this.contentDiv.style.backgroundImage = DRAGONFLY;

    this.game.gameElement.appendChild(this.contentDiv);
	
}

BugSprite.prototype.setPosition = function() {
	
    this.posX = (this.x) * 40;
    this.posY = (this.y) * 40;

}

BugSprite.prototype.eat = function(frog) {
	
	this.contentDiv.style.display = "none";
	this.game.removeBug(this);
	
}

BugSprite.prototype.startMove = function() {
	
	this.direction = getRandomInt(0, 3);
	this.moving = true;
	
	if (this.direction == NORTH && this.x == 0) {
		this.direction = SOUTH;
	} else if (this.direction == SOUTH && this.x == ROWS - 1) {
		this.direction = NORTH;
	} else if (this.direction == EAST && this.y == COLS - 1) {
		this.direction = WEST;
	} else if (this.direction == WEST && this.y == 0) {
		this.direction = EAST;
	}
		
	this.setPosition();
	
}

BugSprite.prototype.stopMove = function() {
	this.moving = false;
	this.setPosition();
}


BugSprite.prototype.update = function(elapsed) {
	
	if (this.moving) {
	
		if (this.direction == NORTH) {
		
			this.posX = this.posX - (elapsed * BUG_MOVE_RATE);
		
			if (this.posX <= ((this.x - 1) * 40)) {
				this.x -= 1;
				this.stopMove();
			}
		
		} else if (this.direction == SOUTH) {
				
			this.posX = this.posX + (elapsed * BUG_MOVE_RATE);
			
			if (this.posX >= ((this.x + 1) * 40)) {
				this.x += 1;
				this.stopMove();
			}
			
		} else if (this.direction == EAST) {
			
			this.posY = this.posY + (elapsed * BUG_MOVE_RATE);
			
			if (this.posY >= ((this.y + 1) * 40)) {
				this.y += 1;
				this.stopMove();
			}
			
		} else if (this.direction == WEST) {
			
			this.posY = this.posY - (elapsed * BUG_MOVE_RATE);
			
			if (this.posY <= ((this.y - 1) * 40)) {
				this.y -= 1;
				this.stopMove();
			}
			
		}
	
	}
	
	this.contentDiv.style.top = Math.floor(this.posX) + "px";
	this.contentDiv.style.left = Math.floor(this.posY) + "px";

}


