SS.HINT_STATE = {
		OPEN:0,
		CLOSED:1,

};

var Hint = cc.Layer.extend({
	_emitter:null,
	_menu:null,
	_medallionBatch:null,
	_menuItemMedallion:null,
	_cooldownCounter:0,
	_deltaFrame:0,
	_deltaFrameUpdate:0,
	_state:SS.HINT_STATE.OPEN,
	_medallionSpriteNormal:null,
	_medallionSpriteSelected:null,
	_flagInUpdate:0,

	ctor:function () {
		this._super();

		var winSize = cc.director.getWinSize();
		//=======MedallionBatch========
		cc.spriteFrameCache.addSpriteFrames("res/medallion.plist");
		var batch = cc.textureCache.addImage(res.medallion_png);
		this._medallionBatch = new cc.SpriteBatchNode(batch);
		this._medallionBatch.attr({
			x:0,
			y:0,
			anchorX:0,
			anchorY:0
		})		
		this.addChild(this._medallionBatch, FOREGROUND);
		
		
		var fr = cc.spriteFrameCache.getSpriteFrame("medallion_4.png");
		
		this._medallionSpriteNormal  = new cc.Sprite( fr );		
		this._medallionSpriteSelected= new cc.Sprite( fr );

		this._menuItemMedallion = new cc.MenuItemSprite(this._medallionSpriteNormal,
														this._medallionSpriteSelected,
													    this.onMedallionCallback,
													    this );

		this._menuItemMedallion.attr({
			x:winSize.width - this._menuItemMedallion.getBoundingBox().width - 5,
			y:0,
			anchorX:0,
			anchorY:0,
		})
		
		this._menu = new cc.Menu(this._menuItemMedallion);
		this._menu.attr({
			x:0,
			y:0,
			anchorX:0,
			anchorY:0,
		});		
		this.addChild(this._menu, FOREGROUND);
		
		//=====Mouse Event============
		if ('mouse' in cc.sys.capabilities){
			cc.eventManager.addListener({
				event: cc.EventListener.MOUSE,
				onMouseMove: function(event){
					if(GameManager.getInstance().getGameState() == SS.GAME_STATE.FROZEN) return;
					var target = event.getCurrentTarget();
					var parent = target.getParent()
					if( target.getState() == SS.HINT_STATE.OPEN ){						
						var locationInNode = target.convertToNodeSpace(event.getLocation());	
						var medallion =  target._menuItemMedallion;
						var s2 = medallion.getContentSize();
						var rect2 = cc.rect(winSize.width - s2.width - 5,
								medallion.getPositionY(), 
								s2.width, 
								s2.height);					
						if (cc.rectContainsPoint(rect2, locationInNode)) {  
							target._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_active.png")));			
						}
						else{
							target._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_4.png")));
						}
					}
				}
			}, this);
		}
		
		this.scheduleUpdate();
	},
	
	update:function(){
		var df = cc.director.getTotalFrames();
		
		if(this._state == SS.HINT_STATE.CLOSED){		
			this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_0.png")));
			this._menuItemMedallion.setSelectedImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_0.png")));
		}
		
		if(!this._deltaFrameUpdate || this._state == SS.HINT_STATE.OPEN) return;
		
		var delta = df - this._deltaFrameUpdate;
		
	
		if(delta >= HINT_COOLDOWN_TIME){
			this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_4.png")));
			this._menuItemMedallion.setSelectedImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_4.png")));
			this._state = SS.HINT_STATE.OPEN;
			if(!this._flagInUpdate){		
				cc.audioEngine.playEffect(res.hint_ready_mp3);						
			
			this._flagInUpdate = 1;
			}
		}		
		else if(delta >= (HINT_COOLDOWN_TIME/4)*3 ){
			this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_3.png")));
			this._menuItemMedallion.setSelectedImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_3.png")));
		}
		else if(delta >= (HINT_COOLDOWN_TIME/4)*2 ){
			this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_2.png")));
			this._menuItemMedallion.setSelectedImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_2.png")));
		}
		else if(delta >= (HINT_COOLDOWN_TIME/4) ){
			this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_1.png")));
			this._menuItemMedallion.setSelectedImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_1.png")));
		}		
	},
	
	initEmitter:function(){
		var df = cc.director.getTotalFrames();
		this._deltaFrameUpdate = df;
		this._state = SS.HINT_STATE.CLOSED;
	},
	
	isReady:function(){
		var ready = false;
		var df = cc.director.getTotalFrames();
		if(df - this._deltaFrame >= HINT_COOLDOWN_TIME || !this._deltaFrame){
			ready = true;
			this._deltaFrame = df;
		}	
		return ready;
	},
	
	onMedallionCallback:function(){
		if(GameManager.getInstance().getGameState() == SS.GAME_STATE.FROZEN) return;
		var parent  = this.getParent();	
		this._flagInUpdate = 0;
		parent.performHint();
	},
	
	MouseOver:function(what){
		var is = what;
		if(  this.state == SS.HINT_STATE.OPEN ){			
			if(is){
				this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_active.png")));
			}				
			if(!is){
				this._menuItemMedallion.setNormalImage(new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("medallion_4.png")));
			}
		}
	},
	
	getState:function(){
		return this._state;
	},
	
	getMedallion:function(){
		return this._menuItemMedallion;
	},
	getMenu:function(){
		return this._menu;
	}
	
});


