/*
 ===================================================
	Window, showing final score and medals
 ===================================================
 */
var ResultsLayer = cc.Layer.extend({
	_window:null,
	_winSize:null,
	_scoreTTF:null,
	_BronzeSprite:null,
	_SilverSprite:null,
	_GoldSprite:null,
	_PlatinumSprite:null,
	_CrystalSprite:null,
	_batch:null,
	
	ctor:function (score) {

		this._super();
		this._winSize = cc.director.getWinSize();
		this._window = new cc.Sprite(res.results_png);
		this.addChild(this._window);
		this._window.attr({
			x:this._winSize.width*0.5,
			y:this._winSize.height*0.5
		})
		
		//=========QUIT BUTTON=============
		
		this._quitButtonNormal = new cc.Sprite(res.quit_normal_png);
		this.addChild(this._quitButtonNormal, FOREGROUND);

		this._quitButtonNormal.attr({
			x:809,
			y:611,
			anchorX:0.5,
			anchorY:0.5,
		});	
		var s = GameManager.getInstance().getGameState();

		//=========MESSAGE LABEL=============
		var resultLabel = new cc.LabelTTF("You lost!", "Old English Text MT", 45);
		if(GameManager.getInstance().getGameState()==SS.GAME_STATE.RESULTS)
			resultLabel.setString("You win!");
		resultLabel.color = cc.color(253, 211, 129);
		this._window.addChild(resultLabel,FOREGROUND);
		resultLabel.attr({
			x:this._winSize.width*0.5- resultLabel.getContentSize().width/2,
			y:this._winSize.height*0.5,
			anchorX:0,
			anchorY:0,
		})

		//=========RESULT LABEL=============
		this._scoreTTF = new cc.LabelTTF("Your score is: " + score.toString(), "Old English Text MT", 45);
		this._window.addChild(this._scoreTTF,FOREGROUND);
		this._scoreTTF.color = cc.color(253, 211, 129);
		this._scoreTTF.attr({
			x:this._winSize.width*0.5 - this._scoreTTF.getContentSize().width/2,
			y:this._winSize.height*0.6,
			anchorX:0,
			anchorY:0,
		})

		//==========MEDALS================
		cc.spriteFrameCache.addSpriteFrames(res.medals_plist);
		var batch = cc.textureCache.addImage(res.medals_png);
		this._batch = new cc.SpriteBatchNode(batch);
		this.addChild(this._batch);

		var blank 	 = cc.spriteFrameCache.getSpriteFrame("blank.png");
		var bronze	 = cc.spriteFrameCache.getSpriteFrame("bronze.png");
		var silver	 = cc.spriteFrameCache.getSpriteFrame("silver.png");
		var gold 	 = cc.spriteFrameCache.getSpriteFrame("gold.png");
		var platinum = cc.spriteFrameCache.getSpriteFrame("platinum.png");
		var crystal  = cc.spriteFrameCache.getSpriteFrame("crystal.png");
		
		//----------Bronze--------------
		this._BronzeSprite = new cc.Sprite(blank);
		this._BronzeSprite.attr({
			x:-121.4 + this._winSize.width*0.5 ,
			y:this._winSize.height*0.4,
			anchorX:0.5,
			anchorY:0,
		})
	
		this._batch.addChild(this._BronzeSprite);
		//----------Silver--------------
		this._SilverSprite = new cc.Sprite(blank);
		this._SilverSprite.attr({
			x:-59.4 + this._winSize.width*0.5 ,
			y:this._winSize.height*0.4,
			anchorX:0.5,
			anchorY:0,
		})
	
		this._batch.addChild(this._SilverSprite);
		//----------Gold--------------
		this._GoldSprite = new cc.Sprite(blank);
		this._GoldSprite.attr({
			x:3 + this._winSize.width*0.5 ,
			y:this._winSize.height*0.4,
			anchorX:0.5,
			anchorY:0,
		})
	
		this._batch.addChild(this._GoldSprite);
		//----------Platimun--------------
		this._PlatinumSprite = new cc.Sprite(blank);
		this._PlatinumSprite.attr({
			x:65.4 + this._winSize.width*0.5 ,
			y:21+this._winSize.height*0.4,
			anchorX:0.5,
			anchorY:0.5,
		})

		this._batch.addChild(this._PlatinumSprite);
		//----------Crystal--------------
		this._CrystalSprite = new cc.Sprite(blank);
		this._CrystalSprite.attr({
			x:127.4 + this._winSize.width*0.5,
			y:21 + this._winSize.height*0.4,
			anchorX:0.5,
			anchorY:0.5,
		})

		this._batch.addChild(this._CrystalSprite);
		
		var effect = null;
		if(score >= CRYSTAL_SCORE){  
				
			effect = cc.audioEngine.playEffect(res.gold_award_mp3);						
			
			this._CrystalSprite.setSpriteFrame(crystal);
			this._CrystalSprite.attr({
				x:127.4 + this._winSize.width*0.5 ,
				y:21 + this._winSize.height*0.4,
				anchorX:0.5,
				anchorY:0.5,
			})
		}
		if(score >= PLATINUM_SCORE){  			
			this._PlatinumSprite.setSpriteFrame(platinum);
			this._PlatinumSprite.attr({
				x:65.4 + this._winSize.width*0.5,
				y:21+this._winSize.height*0.4,
				anchorX:0.5,
				anchorY:0.5,
			})
		}
		if(score >= GOLD_SCORE){
			this._GoldSprite.setSpriteFrame(gold);
		}
		if(score >= SILVER_SCORE){
			this._SilverSprite.setSpriteFrame(silver);
		}
		if(score >= BRONZE_SCORE){	
			this._BronzeSprite.setSpriteFrame(bronze);
		}
		
		
		
		//======SAVING SCORE=========
		var level = GameManager.getInstance().getSelectedLevel();
		var num = level.ScoreStorage.getItem(( "score" + level.ID) );		
		num = parseInt(num, "10");
		if(!num) num = score;		
		if(num > score){
			score = num;
		}		
		level.ScoreStorage.setItem(  ("score"+level.ID) , score);
		//=============AWARDS====================
		if(score >= CRYSTAL_SCORE){  
			var award = {award:level.award};            
			//submit the player's score
			GameAPI.Award.submit(award);
		}
		//=============TOUCH EVENT===============
		cc.eventManager.addListener({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			onTouchBegan: function (touch, event) {	
				var target = event.getCurrentTarget();
				var locationInNode = target._clickLocation = target.convertToNodeSpace(touch.getLocation());
				var s = null;
				var rect = null;
				s = target._quitButtonNormal.getContentSize();
				rect = cc.rect(target._quitButtonNormal.getPositionX()-s.width/2, 
							   target._quitButtonNormal.getPositionY()-s.height/2, 
							   s.width, 
							   s.height);		
				if (cc.rectContainsPoint(rect, locationInNode)) {  
					var texture = cc.textureCache.addImage(res.quit_selected_png);
					var frame = new cc.SpriteFrame(texture, cc.rect(0,0,s.width,s.width));
					target._quitButtonNormal.setSpriteFrame(frame);
					target.onMenuCallback();
				}
				return true;
			}
		}, this);
		return true;
	},


	onMenuCallback:function (pSender) {
		//load resources
		GameManager.getInstance().setGameMode(SS.GAME_MODE.HARDCORE);
		cc.audioEngine.playEffect(res.click_mp3);						
		var scene = SelectLevelMenu.scene();
		cc.director.runScene(new cc.TransitionFade(1, scene));

	},

});
