// game console  ( from javascriptgamer.com )
//
// requires: prototype.js (tested with v. 1.4.0)

// key codes
var KEY_LEFT = 37;
var KEY_RIGHT = 39;
var KEY_UP = 38;
var KEY_P = 80; // "P".charCodeAt(0)
var KEY_ENTER = 13;

var SWAP_SPEED = 5000; // milliseconds to show game over/title/high scores



// Stage ///////////////////////////////////////////////////////

Stage = Class.create();
Stage.prototype = {
	initialize : function (id) {
		this.id = id;
	},
	
	keyDown : function (e) {
	},
	
	keyUp : function(e) {
	},
	
	keyPress : function(e) {
	},

	tick : function () {
	},
	
	show : function () {
		$(this.id).style.display = 'block';
	},
	
	hide : function () {
		$(this.id).style.display = 'none';
	}
}


// Console /////////////////////////////////////////////////////////

Console = Class.create();
Console.prototype = {
	initialize : function (startStage, tickSpeed) {
		this.tickSpeed = tickSpeed || 1; // milliseconds between ticks
		this.stage = startStage;
	},

	swap : function (toStage) {
		this.stage = toStage;
		this.stage.show();
	},
	
	keyDown : function(e) {
		this.stage.keyDown(e);
	},
	
	keyUp : function(e) {
		this.stage.keyUp(e);
	},

	keyPress : function(e) {
		this.stage.keyPress(e);
	},
	
	tick : function () {
		this.stage.tick();
	},
	
	start : function() {
		_con = this;
		window.setInterval(function () { _con.tick() }, this.tickSpeed);
		document.onkeydown = function (e) { _con.keyDown(e) };
		document.onkeyup = function (e) { _con.keyUp(e) };
		document.onkeypress = function (e) { _con.keyPress(e) };
		this.stage.show();
	},
	
	scheduleSwap : function (next, speed) {
		_old = this.stage;
		_con = this;
		window.setTimeout(
			function () {
				if (_con.stage == _old) {
					_old.hide();
					_con.swap(next);
				}
			}, speed || SWAP_SPEED);
	}
}


// Sprite /////////////////////////////////////////////////////////

var Sprite = Class.create();
Sprite.prototype = {
	initialize : function(id) {
		this.id = id;
		this.node = $(id);
	},
	
	getX : function() {
		return this.node.offsetLeft;
	},
	
	setX : function(x) {
		this.node.style.left = x + 'px';
	},
	
	getY : function () {
		return this.node.offsetTop;
	},
	
	setY : function(y) {
		this.node.style.top = y + 'px';
	},	
	
	getW : function() {
		return this.node.offsetWidth;
	},
	
	getH : function() {
		return this.node.offsetHeight;
	},
	
	show : function() {
		this.node.style.display = 'block';
	},

	hide : function() {
		this.node.style.display = 'none';
	},

	moveby: function(dx, dy) {
		this.setX(this.getX()+dx);
		this.setY(this.getY()+dy);
	}
};


// keyboard helpers ////////////////////////////////////////////////

function keyCode(e) {
	// Firefox passes events directly to the handler,
	// whereas IE uses a global event object.
	// So, we check wether e is null to distinguish:
	return e ? e.keyCode : window.event.keyCode;
}

function charCode(e) {
	return e ? e.charCode : window.event.charCode;
}

// stolen from: http://wcpierce.spaces.live.com/blog/cns!676A55AA5C4FB1BE!198.entry
function cancelEvent(evt) {
	evt.cancelBubble = true;
	evt.returnValue = false;
	if (evt.preventDefault) evt.preventDefault();
	if (evt.stopPropagation) evt.stopPropagation();
	return false;
}