/*
 ===================================================
	Simple timer
 ===================================================
 */

var Timer = cc.Layer.extend({
	_seconds:null,
	_timerTTF:null,
	_timerHUD:null,
	
	ctor:function () {
		this._super();
		var winSize = cc.director.getWinSize();
		this._seconds = GameManager.getInstance().getCurrentRound().ItemSearchTime;
		//===========TIMER HUD================
		this._timerHUD = new cc.Sprite(res.timer_hud_png);
		this._timerHUD.attr({
			x:1024,
			y:700,
			anchorX:1,
			anchorY:0
		})
		this._timerHUD.setScale(0.7);
		this._timerHUD.setOpacity(90);
		this.addChild(this._timerHUD);
		//============TIMER TTF==============
		var string = moment().startOf('day').seconds(this._seconds).format('mm:ss');
		this._timerTTF = new cc.LabelTTF(string, "Old English Text MT", 50);
		this._timerTTF.color = cc.color(253, 211,129);
		this._timerTTF.setOpacity(180);
		this.addChild(this._timerTTF);
		this._timerTTF.attr({
			x:1015,
			y:707,
			anchorX:1,
			anchorY:0
		})
		this._timerTTF.setScale(0.7);
		this._timerHUD.setOpacity(95);
		this.schedule(this.update, 1);
	},
	
	update:function () {
		if(GameManager.getInstance().getGameState() == SS.GAME_STATE.GAME_OVER
				|| GameManager.getInstance().getGameState() == SS.GAME_STATE.RESULTS){
			this.unscheduleUpdate();
			return;
		}
		else if(this._seconds){
			this._seconds--;		
			var string = moment().startOf('day').seconds(this._seconds).format('mm:ss');
			this._timerTTF.setString(string);
			return;
		}
		GameManager.getInstance().setGameState(SS.GAME_STATE.GAME_OVER);
	}
});

