
var SelectLevelMenu = cc.Layer.extend({
	_winSize:null,
	_gameModeToggle:null,
	_batch:null,
	_menuToggle:null,
	_selectedLevel:1,
	_playButtonItem:null,
	_playButtonMenu:null,
	_backgroundSprite:null,
	_quitMenuItem:null,
	_quitButton:null,	
	_leftButton:null,
	_rightButton:null,
	_pageLabel:null,
	_levelsCount:0,
	_currentPage:1,
	_levelsPreviewLayer:null,

	ctor:function () {
		this._super();
		this.init();
	},
	
	init:function () {
		var winSize = cc.director.getWinSize();
		GameManager.getInstance().setGameState(SS.GAME_STATE.MENU);
		this._winSize = cc.director.getWinSize()
		this._gameMode = SS.GAME_MODE.RELAX;
		//=======BACKGROUND=====
		this._backgroundSprite = new cc.Sprite(res.selectlevel_menu_BG_png);
		this.addChild(this._backgroundSprite,BACKGROUND);
		this._backgroundSprite.attr({
			anchorX:0,
			anchorY:0,
		})


		//==========Level's Previews===========	
		this._levelsPreviewLayer = new cc.Layer();
		this.addChild(this._levelsPreviewLayer, FOREGROUND);
		this._levelsPreviewLayer.setVisible(true);
		this._levelsPreviewLayer.setPositionX(this._levelsPreviewLayer.getPositionX());

		var i = 0;
		var j = 0;
		var tag = 100;
		var isBottomRow = false;
		this._levelsCount = 0;
		for(var level in SS.LEVELS){			
			var prev = new LevelPreview(SS.LEVELS[level]);	
			prev.setTag(tag);
			this._levelsPreviewLayer.addChild(prev,FOREGROUND,tag);

			prev.attr({
				anchorX: 0,
				anchorY: 0,
				y:this._winSize.height*0.48
			})			
			if(i%3 == 0 && (i/3)%2 != 0){
				isBottomRow = true;
			}
			if(i%6 == 0 && i){
				j++;	
				isBottomRow = false;
				i = 0;
			}
			if(isBottomRow){
				prev.setPositionX(this._winSize.width*j + 128 + (280 + 10)*(i-3));
				prev.setPositionY(this._winSize.height*0.48 - 250);
			}
			else{
				prev.setPositionX(this._winSize.width*j + 128 + (280 + 10)*i);
			}		
			i++;
			tag++;
			this._levelsCount++;
		}

		this._foregroundSprite = new cc.Sprite(res.selectlevel_menu_BG_sides_png);
		this.addChild(this._foregroundSprite, FOREGROUND);
		this._foregroundSprite.attr({
			anchorX:0,
			anchorY:0,
		})

		//=========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.onMenuCallback,this );
		this._quitButton = new cc.Menu(this._quitMenuItem);
		this._quitButton.attr({
			x:980.5,
			y:714.5,
			anchorX:0.5,
			anchorY:0.5,
		});
		this.addChild(this._quitButton, FOREGROUND);	
		//=========GAME MODE TOGGLE===
		this._gameModeToggle = new GameModeToggle();
		this.addChild(this._gameModeToggle,FOREGROUND);

		//==========PLAY BUTTON==================
		var playSprite         = new cc.Sprite(res.play_selectmenu_png);
		var playSpriteSelected = new cc.Sprite(res.play_selectmenu_selected_png);
		var playSpriteDisabled = new cc.Sprite(res.play_selectmenu_png);
		this._playButtonItem = new cc.MenuItemSprite(playSprite,
				playSpriteSelected,
				playSpriteDisabled,
				this.StartGame, 
				this );
		this._playButtonItem.attr({
			x:515,
			y:74.25,		
		})
		this._playButtonMenu = new cc.Menu(this._playButtonItem);
		this._playButtonMenu.attr({
			x:0,
			y:0,		
		})
		this.addChild(this._playButtonMenu, FOREGROUND);

		//==========LEFT BUTTON=========
		var leftButtonSpriteNormal = new cc.Sprite(res.left_png);
		var leftButtonSpriteSelected = new cc.Sprite(res.left_selected_png);
		var leftButtonSpriteDisabled = new cc.Sprite(res.left_png);

		this._leftButton = new cc.MenuItemSprite(leftButtonSpriteNormal,
				leftButtonSpriteSelected,
				leftButtonSpriteDisabled,
				function () {
			this.LeftButtonCallback();
		}.bind(this));
		this._leftButton.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2 - 100,
			y: winSize.height*0.83
		})

		//==========RIGHT BUTTON=========
		var rightButtonSpriteNormal = new cc.Sprite(res.right_png);
		var rightButtonSpriteSelected = new cc.Sprite(res.right_selected_png);
		var rightButtonSpriteDisabled = new cc.Sprite(res.right_png);
			this._rightButton = new cc.MenuItemSprite(rightButtonSpriteNormal,
				rightButtonSpriteSelected,
				rightButtonSpriteDisabled,
				function () {
			this.RightButtonCallback();
		}.bind(this));

		this._rightButton.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2 + 100,
			y: winSize.height*0.83
		})
		this._playButtonMenu.addChild(this._leftButton);
		this._playButtonMenu.addChild(this._rightButton);
		//======PAGE LABEL====
		var str = sprintf("Page %d", this._currentPage);
		this._pageLabel = new cc.LabelTTF(str, "Old English Text MT", 45);
		this._pageLabel.color = cc.color(253, 211,129);
		this._pageLabel.attr({
			anchorX: 0.5,
			anchorY: 0.5,
			x: winSize.width/2,
			y: winSize.height*0.83
		})
		this.addChild(this._pageLabel, FOREGROUND);

		var flag = 0;
		//=====Mouse Event============
			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._playButtonItem.getBoundingBox();

						for(var i = 0; i < this._levelsCount;i++){
							prev = target.getChildByTag(i);

							var rect2  = prev.getPreview().getBoundingBox();
							rect2.x = prev.getPositionX();
							rect2.y = prev.getPositionY();
							if ( cc.rectContainsPoint(rect2, locationInNode)  ) {  								
								prev.mouseOverCallback(1.01);				
							}
							else {
								prev.mouseOverCallback(1);
							}
						}

						if ( cc.rectContainsPoint(rect, locationInNode)  ) {  								
							target._playButtonItem.setNormalImage(new cc.Sprite(res.play_selectmenu_over_png));
							if(!flag){									
								cc.audioEngine.playEffect(res.play_over_mp3);					
	
								flag = 1;
							}
						}
						else {
							target._playButtonItem.setNormalImage(new cc.Sprite(res.play_selectmenu_png));
							flag = 0;
						}

						var s = target._quitMenuItem.getContentSize();
						var	rect3 = cc.rect(980.5-s.width/2,
								714.5-s.height/2,
								s.width, 
								s.height);	

						if ( cc.rectContainsPoint(rect3, locationInNode)  ) {  								
							target._quitMenuItem.setNormalImage(new cc.Sprite(res.quit_over_png));			
						}
						else {
							target._quitMenuItem.setNormalImage(new cc.Sprite(res.quit_normal_png));
						}

					}						
				}, this);
			}
			console.log("Select level menu");
			return true;
	},

	SelectLevel:function(target){
		var menu = target.parent;
		var ar = menu.getChildren();
		for(var i = 0; i <5;i++){
			ar[i].setScale(1);
		}
		target.setScale(1.02);		
		GameManager.getInstance().setSelectedLevel( target.getTag() );
	},

	StartGame:function () {
		var resourse = GameManager.getInstance().getSelectedLevel().Resourses;
		for (var i = 0; i < resourse.length ; i++){
			g_gamelayer.push(resourse[i]);
		}	
			
		cc.audioEngine.playEffect(res.play_on_mp3);						
		
		console.log("before loading resources in game layer");
		cc.LoaderScene.preload(g_gamelayer, function () {
			var scene = GameLayer.scene();
			console.log("Will start game");
			cc.director.runScene(new cc.TransitionFade(1, scene));
		}, this);
	},	

	onMenuCallback:function (pSender) {
		//load resources
		cc.audioEngine.playEffect(res.click_mp3);								
		
		cc.LoaderScene.preload(g_selectlevelmenu, function () {
			var scene = MainMenu.scene();
			cc.director.runScene(new cc.TransitionFade(1, scene));
		}, this);
	},

	LeftButtonCallback:function(){
		if(this._currentPage == 1)return;
		this._currentPage--;
		var str = sprintf("Page %d", this._currentPage);
		this._pageLabel.setString(str);
		var action = cc.MoveBy.create(0.4, cc.p(this._winSize.width, 0 ));
		action.easing(cc.easeOut(5.0));
		this._levelsPreviewLayer.runAction(action);
	},

	RightButtonCallback:function(){
		if(this._currentPage == this._levelsCount/6) return;
		this._currentPage++;
		var str = sprintf("Page %d", this._currentPage);
		this._pageLabel.setString(str);
		var action = cc.MoveBy.create(0.4, cc.p(-this._winSize.width, 0 ));
		action.easing(cc.easeOut(5.0));
		this._levelsPreviewLayer.runAction(action);
	},
});

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


