function Timer() {
	this.initVariables();

	this.dbConnectManager = new DBConnectManager();

	// bar
	var bar = game.add.graphics();
	bar.beginFill(0x202020);
	bar.drawRect(0, 60, GAME_SCREEN_SIZE.x, 240);


	this.makeTimer();
	this.makeTimerButtons();

	this.resetButton = this.makeResetButton();
	this.pauseButton = this.makePauseButton();
	this.showButton(this.pauseButton, false);
	this.playButton = this.makePlayButton();
}



Timer.prototype.initVariables = function() {
	this.isTimerGoingOn = false;
	this.isTimeOver = false;

	this.subjectName = "";

	this.startTime = 0
	this.leftTime = 0;
	this.timerEvent = null;

	this.prevTime = null;

	this.timerCenterX = game.world.centerX + Timer.Timer_CENTER_OFFSET_X;
	this.timerCenterY = 183;

	this.hour10ButtonPosX = this.timerCenterX - 285 - 90;
	this.hour1ButtonPosX = this.timerCenterX - 195 - 90;

	this.minute10ButtonPosX = this.timerCenterX - 45 - 90;
	this.minute1ButtonPosX = this.timerCenterX + 45 - 90;

	this.timePlusButtonPosY = this.timerCenterY - 82;
	this.timeMinusButtonPosY = this.timerCenterY + 75;
}

Timer.prototype.loadDataFromServer = function(leftTime, startTime, subjectName) {
	this.setSubjectName(subjectName);

	if(leftTime == null || leftTime == 0) {
		this.startTime = startTime;
		this.leftTime = startTime;
		this.resetTimer();
		return;
	}

	this.startTime = startTime;
	this.leftTime = leftTime;
	this.pauseTimer();
}

Timer.prototype.setSubjectName = function(subjectName) {
	this.subjectName = subjectName;
}



Timer.prototype.makeTimer = function() {
	this.timeHourText = this.makeTimerTextContent(this.timerCenterX - 240, this.timerCenterY, 160, "00");
	// this.timeHourMinuteSeperator = this.makeTimerTextContent(this.timerCenterX - 185, this.timerCenterY, 160, ":");
	this.timeHourSeperator = this.makeTimerTextContent(this.timerCenterX - 190, this.timerCenterY + 45, 30, "시간");
	this.timeMinuteText = this.makeTimerTextContent(this.timerCenterX, this.timerCenterY, 160, "00");
	// this.timeMinuteSecondSeperator = this.makeTimerTextContent(this.timerCenterX + 40, this.timerCenterY + 20, 100, ":");
	this.timeMinuteSeperator = this.makeTimerTextContent(this.timerCenterX + 30, this.timerCenterY + 45, 30, "분");
	this.timeSecondText = this.makeTimerTextContent(this.timerCenterX + 160, this.timerCenterY + 24, 100, "00");
	this.timeSecondSeperator = this.makeTimerTextContent(this.timerCenterX + 180, this.timerCenterY + 50, 20, "초");
}

Timer.prototype.makeTimerButtons = function() {
	// hour button
	this.hourPlus10Button = this.makeTimePlusButton(this.hour10ButtonPosX, this.timePlusButtonPosY, "▲",
		(function() { this.timePlus(10, Timer.TYPE_HOUR); }).bind(this)
	);
	// this.hourPlus10Button.setInputEnabled(false);
	this.hourMinus10Button = this.makeTimeMinusButton(this.hour10ButtonPosX, this.timeMinusButtonPosY, "▼",
		(function() { this.timeMinus(10, Timer.TYPE_HOUR); }).bind(this)
	);

	this.hourPlus1Button = this.makeTimePlusButton(this.hour1ButtonPosX, this.timePlusButtonPosY, "▲",
		(function() { this.timePlus(1, Timer.TYPE_HOUR); }).bind(this)
	);
	this.hourMinus1Button = this.makeTimeMinusButton(this.hour1ButtonPosX, this.timeMinusButtonPosY, "▼",
		(function() { this.timeMinus(1, Timer.TYPE_HOUR); }).bind(this)
	);

	// minute button
	this.minutePlus10Button = this.makeTimePlusButton(this.minute10ButtonPosX, this.timePlusButtonPosY, "▲",
		(function() { this.timePlus(10, Timer.TYPE_MINUTE); }).bind(this)
	);
	this.minuteMinus10Button = this.makeTimeMinusButton(this.minute10ButtonPosX, this.timeMinusButtonPosY, "▼",
		(function() { this.timeMinus(10, Timer.TYPE_MINUTE); }).bind(this)
	);

	this.minutePlus1Button = this.makeTimePlusButton(this.minute1ButtonPosX, this.timePlusButtonPosY, "▲",
		(function() { this.timePlus(1, Timer.TYPE_MINUTE); }).bind(this)
	);
	this.minuteMinus1Button = this.makeTimeMinusButton(this.minute1ButtonPosX, this.timeMinusButtonPosY, "▼",
		(function() { this.timeMinus(1, Timer.TYPE_MINUTE); }).bind(this)
	);
}

Timer.prototype.makeResetButton = function() {
	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	setting.x = 100;
	setting.y = this.timerCenterY;
	setting.width = 80;
	setting.height = 80;
	setting.strokeWidthPx = 5;
	setting.roundAmount = 60;
	setting.fontStyle.fontSize = 40;

	// red
	setting.setStrokeColor(
		0x900020,
		0x800010,
		0x600000,
		0x666666
	);
	setting.setButtonColor(
		0xc00040,
		0xa00020,
		0x800010,
		0x666666
	);
	setting.setIconColor(
		0xffffff,
		0xdddddd,
		0xbbbbbb,
		0x333333
	);

	var button = new RoundRectButton(
		setting, null, "",
		(function() {
			this.resetTimer();
			this.resetLeftTimeToServer();
		}).bind(this)
	);
	button.setIcon(new SpriteIconTimer(50, 50, SpriteIconTimer.FRAME_RESET));
}

Timer.prototype.makePlayButton = function() {
	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	setting.x = game.world.width - 115;
	setting.y = this.timerCenterY;
	setting.width = 120;
	setting.height = 120;
	setting.strokeWidthPx = 5;
	setting.roundAmount = 90;
	setting.fontStyle.fontSize = 60;

	// yellow
	setting.setStrokeColor(
		0xd0d020,
		0xb0b010,
		0x909000,
		0x333333
	);
	setting.setButtonColor(
		0xf0f040,
		0xd0d030,
		0xb0b020,
		0x666666
	);
	setting.setIconColor(
		0x906020,
		0x704010,
		0x502000,
		0x333333
	);

	var button = new RoundRectButton(
		setting, null, "",
		(function() { this.startTimer(); }).bind(this)
	);
	button.setIcon(new SpriteIconTimer(100, 100, SpriteIconTimer.FRAME_PLAY));

	return button;
}

Timer.prototype.makePauseButton = function() {
	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	setting.x = game.world.width - 115;
	setting.y = this.timerCenterY;
	setting.width = 120;
	setting.height = 120;
	setting.strokeWidthPx = 5;
	setting.roundAmount = 90;
	setting.fontStyle.fontSize = 40;
	setting.fontStyle.fontWeight = "bold";

	// gray
	setting.setStrokeColor(
		0xa0a0a0,
		0x808080,
		0x606060,
		0x333333
	);
	setting.setButtonColor(
		0xf0f0f0,
		0xd0d0d0,
		0xb0b0b0,
		0x666666
	);
	setting.setIconColor(
		0x444444,
		0x333333,
		0x222222,
		0x333333
	);


	var button = new RoundRectButton(
		setting, null, "",
		(function() {
			this.pauseTimer();

			if(this.leftTime > 0)
				this.updateLeftTimeToServer();
			else
				this.resetLeftTimeToServer();
		}).bind(this)
	);
	button.setIcon(new SpriteIconTimer(100, 100, SpriteIconTimer.FRAME_PAUSE));

	return button;
}

Timer.prototype.showButton = function(button, flag) {
	var backupPosX = button.getPosX();

	if(flag)
		button.move(backupPosX, this.timerCenterY);
	else
		button.move(backupPosX, -200);
}

Timer.prototype.hideTimerButtons = function() {
	this.hourPlus10Button.move(-200, -200);
	this.hourPlus1Button.move(-200, -200);
	this.hourMinus10Button.move(-200, -200);
	this.hourMinus1Button.move(-200, -200);

	this.minutePlus10Button.move(-200, -200);
	this.minutePlus1Button.move(-200, -200);
	this.minuteMinus10Button.move(-200, -200);
	this.minuteMinus1Button.move(-200, -200);
}

Timer.prototype.showTimerButtons = function() {
	this.hourPlus10Button.move(this.hour10ButtonPosX, this.timePlusButtonPosY);
	this.hourMinus10Button.move(this.hour10ButtonPosX, this.timeMinusButtonPosY);
	this.hourPlus1Button.move(this.hour1ButtonPosX, this.timePlusButtonPosY);
	this.hourMinus1Button.move(this.hour1ButtonPosX, this.timeMinusButtonPosY);

	this.minutePlus10Button.move(this.minute10ButtonPosX, this.timePlusButtonPosY);
	this.minuteMinus10Button.move(this.minute10ButtonPosX, this.timeMinusButtonPosY);
	this.minutePlus1Button.move(this.minute1ButtonPosX, this.timePlusButtonPosY);
	this.minuteMinus1Button.move(this.minute1ButtonPosX, this.timeMinusButtonPosY);
}

Timer.prototype.setTimerTextColor = function(color) {
	this.timeHourText.tint = color;
	this.timeHourSeperator.tint = color;
	this.timeMinuteText.tint = color;
	this.timeMinuteSeperator.tint = color;
	this.timeSecondText.tint = color;
	this.timeSecondSeperator.tint = color;
}

Timer.prototype.startTimer = function() {
	this.isTimerGoingOn = true;
	if(this.leftTime < 0)
		this.setTimerTextColor(Timer.TEXT_COLOR_RED);
	else
		this.setTimerTextColor(Timer.TEXT_COLOR_YELLOW);
	this.updateTimeText();

	ShowNotifyTimeOver(this.leftTime * Timer.TIME_MILLISECONDS);

	this.prevTime = this.getNowTime();
	this.timerEvent = game.time.events.loop(1000, this.tickLeftTime, this);

	this.showButton(this.playButton, false);
	this.showButton(this.pauseButton, true);
	this.hideTimerButtons();
}

Timer.prototype.pauseTimer = function() {
	// this.isTimeOver = false;

	this.setTimerTextColor(Timer.TEXT_COLOR_GREY);
	this.updateTimeText();

	CancelNotifyTimeOver();

	game.time.events.remove(this.timerEvent);

	this.showButton(this.playButton, true);
	this.showButton(this.pauseButton, false);
	this.showTimerButtons();

	// this.updateLeftTimeToServer();
}

Timer.prototype.resetTimer = function() {
	this.isTimeOver = false;

	this.leftTime = this.startTime;
	this.pauseTimer();

	this.isTimerGoingOn = false;
	this.setTimerTextColor(Timer.TEXT_COLOR_WHITE);
}

Timer.prototype.tickLeftTime = function() {
	this.leftTime--;

	var outFocusedTime = this.getOutFocusedTime();
	if(outFocusedTime > 2) {
		this.leftTime -= outFocusedTime - 1;

		if(this.leftTime > 0) {
			CancelNotifyTimeOver();
			ShowNotifyTimeOver(this.leftTime * Timer.TIME_MILLISECONDS);
		}
	}

	this.updateTimeText();

	if(!this.isTimeOver && this.leftTime < 0)
		this.timeOver();
}

Timer.prototype.timeOver = function() {
	console.log("time over");

	this.isTimeOver = true;
	this.setTimerTextColor(Timer.TEXT_COLOR_RED);

	this.resetLeftTimeToServer();
}

Timer.prototype.getNowTime = function() {
	var now = new Date();
	return now.getTime();
}


Timer.prototype.getOutFocusedTime = function() {
	var nowTime = this.getNowTime();

	var diffTimeSec = 0;
	if(this.prevTime != null)
		var diffTimeSec = parseInt((nowTime - this.prevTime) / 1000);

	this.prevTime = nowTime;

	return diffTimeSec;
}



Timer.prototype.makeTimerTextContent = function(x, y, sizePx, content) {
	// style setting
	var timerTextStyle = { font: "bold 160px Arial", fill: "#fff", align: "right"};
	timerTextStyle.fontSize = sizePx;

	// text
	var timerText = game.add.text(x, y, content, timerTextStyle);
	timerText.anchor.set(1, 0.5);
	timerText.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);

	return timerText;
}

Timer.prototype.makeTimePlusButton = function(x, y, buttonText, eventHandler) {
	var setting = this.makeTimerButtonSetting(x, y);
	setting.setStrokeColor(
		0xffffff,
		0xeeeeff,
		0xccccdd,
		0x333333
	);
	setting.setButtonColor(
		0xeeeeff,
		0xddddee,
		0xccccdd,
		0x666666
	);
	setting.setTextColor(
		"#66c",
		"#66c",
		"#66c",
		"#333"
	);

	return this.makeTimeButton(setting, buttonText, eventHandler);
}

Timer.prototype.makeTimeMinusButton = function(x, y, buttonText, eventHandler) {
	var setting = this.makeTimerButtonSetting(x, y);
	setting.setStrokeColor(
		0xffffff,
		0xffeeee,
		0xddcccc,
		0x333333
	);
	setting.setButtonColor(
		0xffeeee,
		0xeedddd,
		0xddcccc,
		0x666666
	);
	setting.setTextColor(
		"#a66",
		"#a66",
		"#a66",
		"#333"
	);

	return this.makeTimeButton(setting, buttonText, eventHandler);
}

Timer.prototype.makeTimerButtonSetting = function(x, y) {
	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	// setting.fontStyle.boundsAlignH = "center";	// left, center. right
	// setting.fontStyle.boundsAlignV = "middle";	// top, middle, bottom
	setting.strokeWidthPx = 3;
	setting.roundAmount = 1;
	setting.x = x;
	setting.y = y;
	setting.width = 70;
	setting.height = 25;

	return setting;
}

Timer.prototype.makeTimeButton = function(setting, buttonText, eventHandler) {
	var button = new RoundRectButton(setting, null, buttonText, eventHandler);
	button.text.fontSize = 18;

	return button;
}

Timer.prototype.timePlus = function(timePlusSecond, type) {
	var timeAmount = 0;
	switch(type) {
		case Timer.TYPE_HOUR:
		timeAmount = timePlusSecond * Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE;
		break;

		case Timer.TYPE_MINUTE:
		timeAmount = timePlusSecond * Timer.TIME_SECONDS_FOR_MINUTE;
		break;
	}

	// console.log(timeAmount);
	this.leftTime += timeAmount;
	var maxTime = 99 * Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE * Timer.TIME_SECONDS_FOR_MINUTE;
	if(this.leftTime > maxTime)
		this.leftTime = maxTime;
	this.updateTimeText();

	if(!this.isTimerGoingOn) {
		this.startTime = this.leftTime;
		this.updateStartTimeToServer();
	} else {
		this.updateLeftTimeToServer();
	}
}

Timer.prototype.timeMinus = function(timeMinusSecond, type) {
	var timeAmount = 0;
	switch(type) {
		case Timer.TYPE_HOUR:
		timeAmount = timeMinusSecond * Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE;
		break;

		case Timer.TYPE_MINUTE:
		timeAmount = timeMinusSecond * Timer.TIME_SECONDS_FOR_MINUTE;
		break;
	}

	// console.log(timeAmount);
	this.leftTime -= timeAmount;
	if(this.leftTime < 0)
		this.leftTime = 0;
	this.updateTimeText();

	if(!this.isTimerGoingOn) {
		this.startTime = this.leftTime;
		this.updateStartTimeToServer();
	} else {
		this.updateLeftTimeToServer();
	}
}


Timer.prototype.setTime =function(timeSecond) {
	this.leftTime = timeSecond;
	this.updateTimeText();

	if(!this.isTimerGoingOn) {
		this.startTime = this.leftTime;
		this.updateStartTimeToServer();
	} else {
		this.updateLeftTimeToServer();
	}
}


Timer.prototype.updateTimeText = function() {
	var secondsForHour = Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE;

	var leftTimeHour = parseInt(this.leftTime / secondsForHour);
	var leftTimeMinute = parseInt((this.leftTime % secondsForHour) / Timer.TIME_SECONDS_FOR_MINUTE);
	var leftTimeSecond = this.leftTime % Timer.TIME_SECONDS_FOR_MINUTE;

	this.timeHourText.text = this.getTimeWithTimeFormat(leftTimeHour, Timer.TYPE_HOUR);
	// console.log(this.leftTime);
	// console.log(this.timeHourText.text);
	this.timeMinuteText.text = this.getTimeWithTimeFormat(leftTimeMinute, Timer.TYPE_MINUTE);
	this.timeSecondText.text = this.getTimeWithTimeFormat(leftTimeSecond, Timer.TYPE_SECOND);
	// this.updateTimeVariables();
	// this.timerText.text = this.getTimeWithTimeFormat();
}

Timer.prototype.getTwoDigitNumber = function(value) {
	var absValue = value;
	if(absValue < 0)
		absValue *= -1;

	if(absValue < 10)
		return "0" + absValue;
	else
		return absValue;
}

Timer.prototype.getTimeWithTimeFormat = function(timeValue, type) {
	if(type != Timer.TYPE_HOUR) {
		return this.getTwoDigitNumber(timeValue);
	}

	if(this.leftTime < 0) {
		return "-" + this.getTwoDigitNumber(timeValue);
	} else {
		return this.getTwoDigitNumber(timeValue);
	}
}


Timer.prototype.setStartTime = function(time) {
	this.startTime = time;

	if(!this.isTimerGoingOn) {
		this.leftTime = this.startTime;
		this.updateTimeText();
	}

	this.updateStartTimeToServer();
}




Timer.prototype.updateStartTimeToServer = function() {
	// console.log("send start time to server : " + this.startTime);

	this.dbConnectManager.updateLicenseStartTime(
		sessionStorageManager.getMaestroID(),
		sessionStorageManager.getPlayerID(),
		this.startTime,
		this.subjectName,
		(function(replyJson) { /*console.log(replyJson);*/ }).bind(this),
		(function(replyJson) { /*console.log(replyJson);*/ })
	);
}

Timer.prototype.resetLeftTimeToServer = function() {
	// console.log("send left time to server : " + this.leftTime);

	this.dbConnectManager.updateLicenseLeftTime(
		sessionStorageManager.getMaestroID(),
		sessionStorageManager.getPlayerID(),
		0,
		this.subjectName,
		(function(replyJson) { /*console.log(replyJson);*/ }).bind(this),
		(function(replyJson) { /*console.log(replyJson);*/ })
	);
},

Timer.prototype.updateLeftTimeToServer = function() {
	// console.log("send left time to server : " + this.leftTime);

	this.dbConnectManager.updateLicenseLeftTime(
		sessionStorageManager.getMaestroID(),
		sessionStorageManager.getPlayerID(),
		this.leftTime,
		this.subjectName,
		(function(replyJson) { /*console.log(replyJson);*/ }).bind(this),
		(function(replyJson) { /*console.log(replyJson);*/ })
	);
},



Timer.Timer_CENTER_OFFSET_X		= 120;

Timer.TIME_MILLISECONDS			= 1000;

Timer.TIME_MINUTES_FOR_HOUR		= 60;
Timer.TIME_SECONDS_FOR_MINUTE	= 60;

Timer.TIME_DEFAULT_SEC
	= Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE;	// 60 min * 60 sec

Timer.TYPE_HOUR		= "hour";
Timer.TYPE_MINUTE	= "minute";
Timer.TYPE_SECOND	= "second";


Timer.TEXT_COLOR_WHITE		= 0xffffff;
Timer.TEXT_COLOR_CYAN		= 0x40c0c0;
Timer.TEXT_COLOR_YELLOW		= 0xe0e040;
Timer.TEXT_COLOR_GREY		= 0xa0a0a0;
Timer.TEXT_COLOR_RED		= 0xc04040;