var ROWS = 12;
var COLS = 12;

var LILLYPAD_START_COUNT = 70;
var BUG_START_COUNT = 10;
var FROG_START_COUNT = 2;

var BLANK = "&nbsp;";

var NORTH = 0;
var SOUTH = 1;
var EAST = 2;
var WEST = 3;

var POINTS_PER_NEW_FROG = 2;

var GAME_MODE_NORMAL = 0
var GAME_MODE_JUMP   = 1
var GAME_MODE_CHECK  = 2
var GAME_MODE_EAT    = 3
var GAME_MODE_MENU   = 4
var GAME_MODE_ADD    = 5
var GAME_MODE_PAUSE  = 6

var BACKGROUND = "url('images/water/water_480x480.png')";


function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

	
function Game(elementName, msgsElementName) {

	this.counter = 0;
	this.secondsCounter = 0;
	this.numSeconds = 5;
	this.secondsUntilJump = 5;
	this.score = 0;
	this.oldScore = 0;
	this.eatenThisRound = 0;
	this.mode = GAME_MODE_NORMAL;
	
	this.messagesElement = document.getElementById(msgsElementName);
	this.messagesElement.style.width = "320px";
	this.messagesElement.style.height = "100px";
	this.messagesElement.style.position = "absolute";
	this.messagesElement.style.top = "25px";
	this.messagesElement.style.left = "30px";
	
	this.messagesElement.innerHTML = "<p>Score: <span id=\"score_span\">" +
		this.score + "</span></p><p>Frogs jump in <span id=\"secs_span\">" +
		this.secondsUntilJump + "</span> seconds!</p>";
	
	this.gameElement = document.getElementById(elementName);
	
	this.gameElement.style.width = "480px";
	this.gameElement.style.height = "480px";
	this.gameElement.style.backgroundImage = BACKGROUND;
	this.gameElement.style.position = "absolute";
	this.gameElement.style.top = "25px";
	this.gameElement.style.left = "360px";
	this.gameElement.onselectstart = function () { return false; };
	
	this.lastUpdate = (new Date()).getTime();
	
	this.lillypads = new Array();
	this.frogs = new Array();
	this.bugs = new Array();
	
    // randomly position lillypads
    for (var i = 0; i < LILLYPAD_START_COUNT; i++) {
    	var lp = new LillyPadSprite(this);
    	this.lillypads[this.lillypads.length] = lp;
    }
    
    // make one rock
    var r = new LillyPadSprite(this, ROCK_TYPE);
	this.lillypads[this.lillypads.length] = r;
    
    // randomly position frogs
	for (var i = 0; i < FROG_START_COUNT; i++) {
		var f = new FrogSprite(this);
		this.frogs[this.frogs.length] = f;
	}

    // randomly position bugs
    for (var i = 0; i < BUG_START_COUNT; i++) {
    	var b = new BugSprite(this);
    	this.bugs[this.bugs.length] = b;
    }

   	
}

Game.prototype.updateScoreboard = function() {
	
	var scoreSpan = document.getElementById("score_span");
	var secsSpan = document.getElementById("secs_span");
	
	scoreSpan.innerHTML = this.score;
	
	var s = this.numSeconds - this.secondsCounter;
	if (s < 0) s = 0;
	secsSpan.innerHTML = s;
	
}

Game.prototype.update = function() {
	
	var t = (new Date()).getTime();
	var elapsedTime = t - this.lastUpdate;

	if (this.mode != GAME_MODE_CHECK) {
		
		this.counter += elapsedTime;
		if (this.counter >= 1000) {
			this.counter -= 1000;
			this.secondsCounter++;
			this.updateScoreboard();
		}
		
	}

	// update objects
	
	for (var i = 0; i < this.frogs.length; i++) {
		this.frogs[i].update(elapsedTime);
	}

	for (var i = 0; i < this.bugs.length; i++) {
		this.bugs[i].update(elapsedTime);
	}

	// check if we need to start "eat mode"
	
	if (this.secondsCounter == this.numSeconds && this.mode == GAME_MODE_NORMAL) {
		
		this.mode = GAME_MODE_EAT;
		
		this.oldScore = this.score;
		this.feedFrogs();
		this.score += this.eatenThisRound;
		this.updateScoreboard();

	}
	
	// check if we need to start "jump mode"
	
	if (this.counter >= 500 && this.mode == GAME_MODE_EAT) {

		this.mode = GAME_MODE_JUMP;
		this.jumpFrogs();
		this.moveBugs();
		
	}
	
	// check to see if jumping is done
	
	if (this.secondsCounter == this.numSeconds + 1 && this.mode == GAME_MODE_JUMP) {
		
		this.mode = GAME_MODE_CHECK;
		
	}
	
	// check mode is the end of the round. determine if we need to add more
	// of anything and then reset the counters for the next round.

	if (this.mode == GAME_MODE_CHECK) {
		
		// check to see if we should add things

		// add lillypads?
		if (this.lillypads.length < LILLYPAD_START_COUNT) {
	    	var lp = new LillyPadSprite(this);
	    	this.lillypads[this.lillypads.length] = lp;
		}
		
		// add frogs?
		var addFrogs = 0;
		if (this.oldScore < this.score) {
			if (this.eatenThisRound == 1 && (this.score % POINTS_PER_NEW_FROG == 0)) {
				addFrogs = 1;
			} else {
				addFrogs = Math.floor(this.eatenThisRound / POINTS_PER_NEW_FROG);
			}
		}

		for (var i = 0; i < addFrogs; i++) {
		   	var f = new FrogSprite(this);
		   	this.frogs[this.frogs.length] = f;
		}

		// add bugs?
		if (this.bugs.length < this.frogs.length + 1) {
	    	var b = new BugSprite(this);
	    	this.bugs[this.bugs.length] = b;
		}

		// how long until the frogs jump again?
		this.numSeconds = this.frogs.length + 2;

		// reset counters
		this.secondsCounter = 0;
		this.eatenThisRound = 0;
		
		// go back to normal game mode
		this.mode = GAME_MODE_NORMAL;
		
	}
		
	this.lastUpdate = t;
	
}

Game.prototype.feedFrogs = function() {

	for (var i = 0; i < this.frogs.length; i++) {
		this.frogs[i].eat();
	}
	
}

Game.prototype.jumpFrogs = function() {
	
	for (var i = 0; i < this.frogs.length; i++) {
		this.frogs[i].startJump();
	}
	
}

Game.prototype.moveBugs = function() {
	
	for (var i = 0; i < this.bugs.length; i++) {
		this.bugs[i].startMove();
	}
	
}

Game.prototype.getLillyPadGrid = function() {

	var ret = new Array();
	for (var i = 0; i < ROWS; i++) {
		ret[i] = new Array();
		for (var j = 0; j < COLS; j++) {
			ret[i][j] = null;
		}
	}
	
	for (var i = 0; i < this.lillypads.length; i++) {
		var lp = this.lillypads[i];
		ret[lp.x][lp.y] = lp;
	}
	return ret;
	
}

Game.prototype.getFrogGrid = function() {

	var ret = new Array();
	for (var i = 0; i < ROWS; i++) {
		ret[i] = new Array();
		for (var j = 0; j < COLS; j++) {
			ret[i][j] = false;
		}
	}
	
	for (var i = 0; i < this.frogs.length; i++) {
		var f = this.frogs[i];
		ret[f.x][f.y] = true;
	}
	return ret;
	
}

Game.prototype.getBugGrid = function() {

	var ret = new Array();
	for (var i = 0; i < ROWS; i++) {
		ret[i] = new Array();
		for (var j = 0; j < COLS; j++) {
			ret[i][j] = null;
		}
	}
	
	for (var i = 0; i < this.bugs.length; i++) {
		var b = this.bugs[i];
		ret[b.x][b.y] = b;
	}
	return ret;
	
}

Game.prototype.removeFrog = function(frog) {
	
	for (var i = 0; i < this.frogs.length; i++) {
		if (this.frogs[i] == frog) {
			this.frogs.splice(i, 1);
			return;
		}
	}
	
}

Game.prototype.removeLillyPad = function(lp) {
	
	for (var i = 0; i < this.lillypads.length; i++) {
		if (this.lillypads[i] == lp) {
			this.lillypads.splice(i, 1);
			return;
		}
	}
	
}

Game.prototype.removeBug = function(bug) {
	
	for (var i = 0; i < this.bugs.length; i++) {
		if (this.bugs[i] == bug) {
			this.eatenThisRound++;
			this.bugs.splice(i, 1);
			return;
		}
	}
	
}

	
function playRibbit() {
	
	ribbitSound.play();
	
}



