function RealtimeStageTimer(timeLimitSec) {
	this.timeLimit = timeLimitSec;

	this.startTime = 0;
	this.elapsedTime = 0;
	this.isStarted = false;
	this.isPaused = true;

	this.onTimeUpdate = null;
	this.onTimeOver = null;

	this.timerEvent = null;
}

RealtimeStageTimer.prototype.setTimeUpdateEvent = function(eventMethod) {
	this.onTimeUpdate = eventMethod;
}

RealtimeStageTimer.prototype.setTimeOverEvent = function(eventMethod) {
	this.onTimeOver = eventMethod;
}

RealtimeStageTimer.prototype.start = function() {
	if(this.isStarted === true)
		return;

	this.isPaused = false;
	this.startTime = game.time.totalElapsedSeconds();

	this.elapsedTimer = game.time.create(false);
	this.timerEvent = this.elapsedTimer.loop(10, this.updateTimer, this);
	this.elapsedTimer.start();

	this.sendElapsedTime();
}

RealtimeStageTimer.prototype.pause = function() {
	this.isPaused = true;

	this.sendElapsedTime();
}

RealtimeStageTimer.prototype.resume = function() {
	this.isPaused = false;

	this.sendElapsedTime();
}

RealtimeStageTimer.prototype.stop = function() {
	this.isStarted = false;
	this.isPaused = true;

	this.sendElapsedTime();

	var recordTime = game.time.totalElapsedSeconds() - this.startTime;
	this.removeTimer();

	return recordTime;
}

RealtimeStageTimer.prototype.updateTimer = function() {
	if(this.isPaused == true)
		return;

	this.elapsedTime = game.time.totalElapsedSeconds() - this.startTime;

	var timeLeft = this.timeLimit - this.elapsedTime;
	if(timeLeft < 0) {
		this.stop();

		if(this.onTimeOver !== null)
			this.onTimeOver();

		return;
	}

	this.sendElapsedTime();
}

RealtimeStageTimer.prototype.sendElapsedTime = function() {
	if(this.onTimeUpdate === null)
		return;

	var timeLeft = this.timeLimit - this.elapsedTime;
	this.onTimeUpdate(timeLeft);
}

RealtimeStageTimer.prototype.getElapsedTime = function() {
	return this.elapsedTime;
}

RealtimeStageTimer.prototype.removeTimer = function() {
	game.time.events.remove(this.timerEvent);
	if(this.timerEvent)
		this.timerEvent = null;

	this.elapsedTimer.stop();
	this.elapsedTimer = null;
}



RealtimeStageTimer.FONT_HEIGHT_PX		= 70;

RealtimeStageTimer.DEFAULT_TEXT_FONT	= {
	font: "38px Arial",
	align: "center",
	boundsAlignH: "center",		// left, center. right
	boundsAlignV: "middle", 	// top, middle, bottom
	fill: "#fff"
};