/*
 ===================================================
	Draws levels previews and medals
 ===================================================
 */

var LevelPreview = cc.Layer.extend({
	_level:null,
	_score:null,
	_previewSprite:null,
	_BronzeSprite:null,
	_SilverSprite:null,
	_GoldSprite:null,
	_PlatinumSprite:null,
	_CrystalSprite:null,
	_menu:null,
	_glow:null,
	_batch:null,

	ctor:function (level) {
		this._super();
		this._level = level;
		if(this._level.ScoreStorage.getItem('score'+this._level.ID)){
			this._score = this._level.ScoreStorage.getItem('score'+this._level.ID);
		}else 
			this._score = 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:0,
			y:0,
			anchorX:0,
			anchorY:0,
		})
		this._BronzeSprite.setScale(0.8);
		this._batch.addChild(this._BronzeSprite);
		//----------Silver--------------
		this._SilverSprite = new cc.Sprite(blank);
		this._SilverSprite.attr({
			x:41.6 ,
			y:0,
			anchorX:0,
			anchorY:0,
		})
		this._SilverSprite.setScale(0.8);
		this._batch.addChild(this._SilverSprite);
		//----------Gold--------------
		this._GoldSprite = new cc.Sprite(blank);
		this._GoldSprite.attr({
			x:83.2 ,
			y:0,
			anchorX:0,
			anchorY:0,
		})
		this._GoldSprite.setScale(0.8);
		this._batch.addChild(this._GoldSprite);
		//----------Platimun--------------
		this._PlatinumSprite = new cc.Sprite(blank);
		this._PlatinumSprite.attr({
			x:124.8 ,
			y:16.8,
			anchorX:0,
			anchorY:0.5,
		})
		this._PlatinumSprite.setScale(0.8);
		this._batch.addChild(this._PlatinumSprite);
		//----------Crystal--------------
		this._CrystalSprite = new cc.Sprite(blank);
		this._CrystalSprite.attr({
			x:166.4 ,
			y:16.8,
			anchorX:0,
			anchorY:0.5,
		})
		this._CrystalSprite.setScale(0.8);
		this._batch.addChild(this._CrystalSprite);
		
		if(this._score >= CRYSTAL_SCORE){  
			this._CrystalSprite.setSpriteFrame(crystal);
			this._CrystalSprite.attr({
				x:147.6 ,
				y:16.8,
				anchorX:0,
				anchorY:0.5,
			})
		}
		if(this._score >= PLATINUM_SCORE){  
			this._PlatinumSprite.setSpriteFrame(platinum);
			this._PlatinumSprite.attr({
				x:108 ,
				y:16.8,
				anchorX:0,
				anchorY:0.5,
			})
		}
		if(this._score >= GOLD_SCORE){
			this._GoldSprite.setSpriteFrame(gold);
		}
		if(this._score >= SILVER_SCORE){
			this._SilverSprite.setSpriteFrame(silver);
		}
		if(this._score >= BRONZE_SCORE){	
			this._BronzeSprite.setSpriteFrame(bronze);
		}

		//==============PREVIEW SPRITE==========
		this._previewSprite = new cc.Sprite(this._level.PreviewPath);	

		this.addChild(this._previewSprite);
		this.initEvents();
		this._previewSprite.attr({
			x:-42,
			y:50,
			anchorX:0,
			anchorY:0,
		})
		//==========PREVIEW GLOW===============
		this.glow = new cc.Sprite(res.preview_glow_png);
		this.glow.attr({	
			x:-18.5,
			y:-3,
			anchorX:0,
			anchorY:0,
		})
		this._previewSprite.addChild(this.glow,-1);
		if(this._level != GameManager.getInstance().getSelectedLevel())
			this.glow.setVisible(false);

		//==========Title=====================
		var title = new cc.LabelTTF(this._level.Title,"Old English Text MT", 25 );
		title.color = cc.color(253, 211, 129);
		title.attr({
			x:this._previewSprite.getContentSize().width/2 - title.getContentSize().width/2,
			y:this._previewSprite.getContentSize().height/2 - title.getContentSize().height/2 ,
			anchorX:0,
			anchorY:0,
		})
		this._previewSprite.addChild(title);	

	},	

	initEvents:function(){
		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;

				target._previewSprite;
				s = target._previewSprite.getContentSize();
				rect = cc.rect(target._previewSprite.getPositionX(), 
						target._previewSprite.getPositionY(), 
						s.width, 
						s.height);					

				if (cc.rectContainsPoint(rect, locationInNode)) {  
					target.setLevel();
				}
				return true;
			}
		}, this);
	},

	setLevel:function(){
		cc.audioEngine.playEffect(res.click_mp3);

		GameManager.getInstance().setSelectedLevel( this._level );		
		var menu = this.parent;
		var ar = menu.getChildren();
		for(var i = 0; i < ar.length;i++){
			if(ar[i].getTag() >= 0)
				LevelPreview.setGlow(ar[i],false);
		}
		LevelPreview.setGlow(this,true);	
	},
	getPreview:function(){
		return this._previewSprite;
	},
	mouseOverCallback:function(scale){
		this._previewSprite.setScale(scale);
	},

});

LevelPreview.setGlow = function(target, bool){
	var ch = target._previewSprite.getChildren();
	ch[0].setVisible(bool);
}
