
var MainMenu = cc.Layer.extend({
	_backgroundSprite:null,
	_playButton:null,
	_volumeToggle:null,
	_batch:null,
	_menu:null,
	_playLabel:null,
	_chapterLabel:null,
	_emitter: null,
	_brandLogo:null,
	_moreGamesButton:null,
	_brandMenu:null,
	_scaleButton:null,
	_policy:false,
	_hintButton:null, 
	_deltaFrame:0,
	_clickCounter:0,
	
	ctor:function () {
		this._super();
		this.init();
	},
	
	init:function () {		
		GameManager.getInstance().setGameState(SS.GAME_STATE.MENU);
		var winSize = cc.director.getWinSize();
		//=========QUIT BUTTON=============
		var menuSpriteNormal =  new cc.Sprite(res.quit_normal_png);

		var menuSpriteSelected =  new cc.Sprite(res.quit_selected_png);

		this._quitMenuItem = new cc.MenuItemSprite(menuSpriteNormal, menuSpriteSelected,  this.onQuitButton,this );
		this._quitButton = new cc.Menu(this._quitMenuItem);
		this._quitButton.attr({
			x:975.5,
			y:698.5,
			anchorX:0.5,
			anchorY:0.5,
		});
		this.addChild(this._quitButton, FOREGROUND);	
		//==========BACKGROUND=========
		this._backgroundSprite = new cc.Sprite(res.main_menu_BG_png);
		this._backgroundSprite.attr({
			anchorX: 0,
			anchorY: 0,
			x: 0,
			y: 0
		});
		this.addChild(this._backgroundSprite,0);
		  
		//==========PLAY LABEL==========
		this._playLabel = new cc.Sprite(res.play_label_png);
		this._playLabel.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2,
			y: winSize.height*0.3

		})
		this._playLabel.setVisible(false);
		this.addChild(this._playLabel, FOREGROUND);
		//==========PLAY BUTTON=========
		var playButtonSpriteNormal = new cc.Sprite(res.play_not_png);
		var playButtonSpriteSelected = new cc.Sprite(res.play_selected_png);
		var playButtonSpriteDisabled = new cc.Sprite(res.play_not_png);

		this._playButton = new cc.MenuItemSprite(playButtonSpriteNormal,
												 playButtonSpriteSelected,
												 playButtonSpriteDisabled, 
												 function () {	
															 this.onSelectLevel();
														    	}.bind(this));	

		
		this._playButton.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2,
			y: winSize.height/2

		})
		var sprite = new cc.Sprite(res.play_not_png);
		sprite.setOpacity(1);
		this._hintButton = new cc.MenuItemSprite(sprite, 
				sprite,
				sprite, 
				function () {	
			var df = cc.director.getTotalFrames();
			if(df - this._deltaFrame >= ANTICLICKER_TIME_BETWEEN_CLICKS && this._deltaFrame)
				this._clickCounter = 0;
			else{
				this._clickCounter++;
			}
			if(this._clickCounter>= ANTICLICKER_CLICKS_COUNT){	
				HINT_COOLDOWN_TIME  = 60;
			}
			this._deltaFrame = df;			
		
		}.bind(this));	


		this._hintButton.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2-60,
			y: 580
		})
		this._hintButton.setOpacity(1);
		
		this._menu = new cc.Menu(this._playButton, this._hintButton);
		this._menu.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0
			
		});
		this.addChild(this._menu, 1, 2);
		
		//==========CHAPTER LABEL==========
		this._chapterLabel = new cc.Sprite(res.chapter_11_png);
		this._chapterLabel.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2,
			y: winSize.height*0.75

		})

		this.addChild(this._chapterLabel, FOREGROUND);
		
		//==========START MUSIC=========

		//if (!cc.audioEngine.isMusicPlaying()) {
				//cc.audioEngine.setMusicVolume(0.7);
				//cc.audioEngine.setEffectsVolume(0.7)
				cc.audioEngine.playMusic(res.Chapter_11_music_wav, true);
		//}

		//==========VOLUME TOGGLE=========
		this._volumeToggle = new VolumeToggle();
		this.addChild(this._volumeToggle,100,1);		
		
			//=====Mouse Event============
		var flag = 0;
		if ('mouse' in cc.sys.capabilities){
				cc.eventManager.addListener({
					event: cc.EventListener.MOUSE,
					onMouseMove: function(event){
						var target = event.getCurrentTarget();
						var locationInNode = target.convertToNodeSpace(event.getLocation());				
							
							var	rect = target._playButton.getBoundingBox();		
							var s =cc.rectContainsPoint(rect, locationInNode);
							if ( cc.rectContainsPoint(rect, locationInNode)  ) {  								
								target._playButton.setNormalImage(new cc.Sprite(res.play_png));
								target._playLabel.setVisible(true);
								if(!flag){				
									cc.audioEngine.playEffect(res.play_over_mp3);										
									flag = 1;
								}
							}
							else {
								target._playButton.setNormalImage(new cc.Sprite(res.play_not_png));
								target._playLabel.setVisible(false);
								flag = 0;
							}			
					}						
				}, this);
			}
	console.log("Main menu");
	return true;
},	
	onSelectLevel:function (pSender) {			
		cc.audioEngine.playEffect(res.play_on_mp3);
		console.log("Will go to select level menu");
		var scene = SelectLevelMenu.scene();
		cc.director.runScene(new cc.TransitionFade(1, scene));
	},
	checkHint:function (pSender) {
		console.log("Hint");
	},
	onQuitButton:function(sender){
		cc.director.end();
	}
});

MainMenu.scene = function () {
	var scene = new cc.Scene();
	var layer = new MainMenu();
	scene.addChild(layer);
	return scene;
};
