var noOfPlayer;
var soundBtn = 1;


Crafty.scene('TTSplash', function() {
    console.log("TTSplash SCREEN");
    Crafty.background('rgb(255,255,255)');
    var bg = Crafty.e("2D, Canvas, Image").image("images/tt_Logo.png");
    bg.x = Crafty.viewport.width/2-110; bg.y = Crafty.viewport.height/2-65;
    
	setTimeout(function(){
       	Crafty.scene('GameSplash');
    },3000);

});


Crafty.scene('GameSplash', function() {
	console.log("GAME SPLASH SCREEN");
    var game_Logo = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image("images/title.png");
        
	var bTime1 = setTimeout(function(){
       	Crafty.scene('GameMenu'); game_Logo.destroy();
    },3000);

    game_Logo.bind("MouseUp", function(e) {
        clearTimeout(bTime1)
        Crafty.scene('GameMenu');
    })
});


Crafty.scene('GameMenu', function() {
	console.log("GAME MENU SCREEN");

    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var newGameHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/newgame2.png");
    newGameHover.x = bg._x+103; newGameHover.y = bg._y+17;
    newGameHover.alpha = 0.01;
    
    var newGameBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/newgame1.png");
    newGameBtn.x = bg._x+103; newGameBtn.y = bg._y+17;

	var onBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/soundon.png");
    onBtn.x = bg._x+103; onBtn.y = bg._y+80;
    onBtn.originX = onBtn._x;
    
    var offBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/soundoff.png");
    offBtn.x = bg._x+103; offBtn.y = bg._y+80;
    offBtn.originX = offBtn._x;
    
    var helpBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/help2.png");
    helpBtnHover.x = bg._x+103; helpBtnHover.y = bg._y+143;
    helpBtnHover.alpha = 0.01;
    
    var helpBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/help1.png");
    helpBtn.x = bg._x+103; helpBtn.y = bg._y+143;
    
    var aboutUsHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/aboutus2.png");
    aboutUsHover.x = bg._x+103; aboutUsHover.y = bg._y+207;
    aboutUsHover.alpha = 0.01;
    
    var aboutUsBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/aboutus1.png");
    aboutUsBtn.x = bg._x+103; aboutUsBtn.y = bg._y+207;


    bg.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        helpBtnHover.alpha = 0.01;
        newGameBtn.alpha = 1;
        newGameHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        aboutUsHover.alpha = 0.01;
    });
    
    
    
	helpBtn.bind("MouseDown", function(e) { 
        helpBtn.alpha = 0.01;
        helpBtnHover.alpha = 1;
    });
    
    helpBtn.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        helpBtnHover.alpha = 0.01;
        createHelpScreen();
    });

    newGameBtn.bind("MouseDown", function(e) { 
        newGameBtn.alpha = 0.01;
        newGameHover.alpha = 1;
    });
    newGameBtn.bind("MouseUp", function(e) { 
        newGameBtn.alpha = 1;
        newGameHover.alpha = 0.01;
        Crafty.scene('SelectPlayer');
    });


    aboutUsBtn.bind("MouseDown", function(e) { 
        aboutUsBtn.alpha = 0.01;
        aboutUsHover.alpha = 1;
    });
    aboutUsBtn.bind("MouseUp", function(e) { 
        aboutUsBtn.alpha = 1;
        aboutUsHover.alpha = 0.01;
        Crafty.scene('AboutUs');
    });

	if(getAudioIsMuted()) {
        offBtn.visible = true;
        onBtn.visible = false;
        onBtn.x = -1000;
    }
	else {
        offBtn.visible = false;
        onBtn.visible = true;
        offBtn.x = -1000;
    }
            
    offBtn.bind("MouseDown", function(e) {
        console.log("menu off btn clicked");
        soundBtn = 1;
        setAudioIsMuted(false)
        onBtn.visible = true;
        offBtn.visible = false;
        offBtn.x = -1000;
        onBtn.x = onBtn.originX
        playBgMusic("BgmMenu")
        console.log('soundBtn = '+soundBtn);
            
	});
    offBtn.bind("MouseUp", function(e) {
        helpBtn.alpha = 1;
        helpBtnHover.alpha = 0.01;
        newGameBtn.alpha = 1;
        newGameHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        aboutUsHover.alpha = 0.01;
    });

	onBtn.bind("MouseDown", function(e) {
        console.log("menu on btn clicked");
        soundBtn = 0;
        setAudioIsMuted(true)
        offBtn.visible = true;
        onBtn.visible = false;
        offBtn.x = offBtn.originX
        onBtn.x = -1000;
        Crafty.audio.stop();
        setBgMusicPlaying();
        console.log('soundBtn = '+soundBtn);
    });
    onBtn.bind("MouseUp", function(e) {
        helpBtn.alpha = 1;
        helpBtnHover.alpha = 0.01;
        newGameBtn.alpha = 1;
        newGameHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        aboutUsHover.alpha = 0.01;
    });
    
    
    playBgMusic("BgmMenu")

});
 



Crafty.scene('SelectPlayer', function() {
	console.log("SelectPlayer SCREEN");
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var singleBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/sngleplayer2.png");
    singleBtnHover.x = bg._x+60; singleBtnHover.y = bg._y+55;
    singleBtnHover.alpha = 0.01;
    
    var singleBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/sngleplayer1.png");
    singleBtn.x = bg._x+60; singleBtn.y = bg._y+55;

    var multiBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/twoplayer2.png");
    multiBtnHover.x = bg._x+60; multiBtnHover.y = bg._y+135;
    multiBtnHover.alpha = 0.01;
    
    var multiBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/twoplayer1.png");
    multiBtn.x = bg._x+60; multiBtn.y = bg._y+135;
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back2.png");
    backBtnHover.x = bg._x+418; backBtnHover.y = bg._y+225;    
    backBtnHover.alpha = 0.01;
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back1.png");
    backBtn.x = bg._x+418; backBtn.y = bg._y+225;    


    backBtn.bind("MouseDown", function(e) { 
        backBtnHover.alpha = 1;
        backBtn.alpha = 0.01;
        
    });
    backBtn.bind("MouseUp", function(e) { 
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
        Crafty.scene('GameMenu');
    });
    
    
    bg.bind("MouseUp", function(e) { 
        singleBtn.alpha = 1;
        singleBtnHover.alpha = 0.01;
        multiBtn.alpha = 1;
        multiBtnHover.alpha = 0.01;
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
    });
    
    
   singleBtn.bind("MouseDown", function(e) { 
        singleBtn.alpha = 0.01;
        singleBtnHover.alpha = 1;
    });
    singleBtn.bind("MouseUp", function(e) { 
        singleBtn.alpha = 1;
        singleBtnHover.alpha = 0.01;
        noOfPlayer = 1;
        Crafty.scene('GamePlay');
    });

       
    multiBtn.bind("MouseDown", function(e) {
         multiBtn.alpha = 0.01;
         multiBtnHover.alpha = 1;
    });
    multiBtn.bind("MouseUp", function(e) {
        multiBtn.alpha = 0.01;
        multiBtnHover.alpha = 1;
        noOfPlayer = 2;
        Crafty.scene('GamePlay');
    });
    
});

 
Crafty.scene('AboutUs', function() {
    console.log("ABOUT US SCREEN");

    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');

    var blackBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/pop_up.png');
    blackBg.x = bg._x+62; blackBg.y = bg._y+35;
    
    var aboutUsTag = Crafty.e("2D, Canvas, Image").image("images/aboutus3.png");
    aboutUsTag.x = bg._x+175; aboutUsTag.y = bg._y+7;

    var aboutusText = Crafty.e("2D, Canvas, Image, Mouse").image("images/aboutus4.png");
    aboutusText.x = bg._x+115; aboutusText.y = bg._y+57;

    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back2.png");
    backBtnHover.x = bg._x+418; backBtnHover.y = bg._y+225;    
    backBtnHover.alpha = 0.01;
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back1.png");
    backBtn.x = bg._x+418; backBtn.y = bg._y+225;    


    backBtn.bind("MouseDown", function(e) { 
        backBtnHover.alpha = 1;
        backBtn.alpha = 0.01;
        
    });
    backBtn.bind("MouseUp", function(e) { 
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
        Crafty.scene('GameMenu');
    });
    
    bg.bind("MouseUp", function(e) { 
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
    });
    
    blackBg.bind("MouseUp", function(e) { 
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
    });
    
    aboutusText.bind("MouseUp", function(e) { 
        backBtnHover.alpha = 0.01;
        backBtn.alpha = 1;
    });
    
});



Crafty.scene('GameDraw', function() {
    
    Crafty.audio.stop();

    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image('images/menu_bg.png');
    
    var DrawText = Crafty.e("2D, Canvas, Image, Mouse").image('images/gamedraw.png');
    DrawText.x = bg._x+145;   DrawText.y = bg._y+50; 
    
    var tryAgainHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/tryagain2.png");
    tryAgainHover.x = bg._x+103; tryAgainHover.y = bg._y+95;
    tryAgainHover.alpha = 0.01;
    
    var tryAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/tryagain1.png");
    tryAgainBtn.x = bg._x+103; tryAgainBtn.y = bg._y+95;  

    var mainMenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu2.png");
    mainMenuHover.x = bg._x+103; mainMenuHover.y = bg._y+165;
    mainMenuHover.alpha = 0.01;
    
    var mainMenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu1.png");
    mainMenuBtn.x = bg._x+103; mainMenuBtn.y = bg._y+165;
    
    
    
    DrawText.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    bg.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    tryAgainBtn.bind("MouseDown", function(e) { 
        tryAgainBtn.alpha = 0.01;
        tryAgainHover.alpha = 1;
    });
    tryAgainBtn.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        Crafty.scene('GamePlay');
    });
    
    
    mainMenuBtn.bind("MouseDown", function(e) { 
        mainMenuBtn.alpha = 0.01;
        mainMenuHover.alpha = 1;
    });
    mainMenuBtn.bind("MouseUp", function(e) { 
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
        playerNo = 0;
//        Crafty.audio.stop();
//        setBgMusicPlaying();
        Crafty.scene('GameMenu');
    });
    
    playSfxAudio("Draw");
    
});
    


Crafty.scene('UserWin', function() {
    console.log("User Win SCREEN");
    Crafty.audio.stop();
        
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image('images/menu_bg.png');
    
    var winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/you_won.png');
    winText.x = bg._x+105;   winText.y = bg._y+50; 
    
    var playAgainHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain2.png");
    playAgainHover.x = bg._x+103; playAgainHover.y = bg._y+120;
    playAgainHover.alpha = 0.01;
    
    var playAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain1.png");
    playAgainBtn.x = bg._x+103; playAgainBtn.y = bg._y+120;  

    var mainMenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu2.png");
    mainMenuHover.x = bg._x+103; mainMenuHover.y = bg._y+190;
    mainMenuHover.alpha = 0.01;
    
    var mainMenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu1.png");
    mainMenuBtn.x = bg._x+103; mainMenuBtn.y = bg._y+190;
    
    
    winText.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    bg.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    playAgainBtn.bind("MouseDown", function(e) { 
        playAgainBtn.alpha = 0.01;
        playAgainHover.alpha = 1;
    });
    playAgainBtn.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        Crafty.scene('GamePlay');
    });
    
    
    mainMenuBtn.bind("MouseDown", function(e) { 
        mainMenuBtn.alpha = 0.01;
        mainMenuHover.alpha = 1;
    });
    mainMenuBtn.bind("MouseUp", function(e) { 
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
        playerNo = 0;
        Crafty.scene('GameMenu');
    });
    
    playSfxAudio("LevelComplete");
    
});


Crafty.scene('LostScreen', function() {
    console.log("Lost SCREEN");
    Crafty.audio.stop();
    playSfxAudio("LevelFailed");
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image('images/menu_bg.png');
    
    var lostText = Crafty.e("2D, Canvas, Image, Mouse").image('images/you_lost.png');
    lostText.x = bg._x+110;   lostText.y = bg._y+50; 
    
    var tryAgainHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/tryagain2.png");
    tryAgainHover.x = bg._x+103; tryAgainHover.y = bg._y+95;
    tryAgainHover.alpha = 0.01;
    
    var tryAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/tryagain1.png");
    tryAgainBtn.x = bg._x+103; tryAgainBtn.y = bg._y+95;  

    var mainMenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu2.png");
    mainMenuHover.x = bg._x+103; mainMenuHover.y = bg._y+165;
    mainMenuHover.alpha = 0.01;
    
    var mainMenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu1.png");
    mainMenuBtn.x = bg._x+103; mainMenuBtn.y = bg._y+165;
    
    
    lostText.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    bg.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    tryAgainBtn.bind("MouseDown", function(e) { 
        tryAgainBtn.alpha = 0.01;
        tryAgainHover.alpha = 1;
    });
    tryAgainBtn.bind("MouseUp", function(e) { 
        tryAgainBtn.alpha = 1;
        tryAgainHover.alpha = 0.01;
        Crafty.scene('GamePlay');
    });
    
    
    mainMenuBtn.bind("MouseDown", function(e) { 
        mainMenuBtn.alpha = 0.01;
        mainMenuHover.alpha = 1;
    });
    mainMenuBtn.bind("MouseUp", function(e) { 
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
        playerNo = 0;
        Crafty.scene('GameMenu');
    });
    
});


Crafty.scene('Player1Won', function() {
    console.log("PLAYER 1 WIN SCREEN");
    Crafty.audio.stop();
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image('images/menu_bg.png');
    
    var winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/you_wonplayer1.png');
    winText.x = bg._x+105;   winText.y = bg._y+40; 
    
    var playAgainHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain2.png");
    playAgainHover.x = bg._x+103; playAgainHover.y = bg._y+120;
    playAgainHover.alpha = 0.01;
    
    var playAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain1.png");
    playAgainBtn.x = bg._x+103; playAgainBtn.y = bg._y+120;  

    var mainMenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu2.png");
    mainMenuHover.x = bg._x+103; mainMenuHover.y = bg._y+190;
    mainMenuHover.alpha = 0.01;
    
    var mainMenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu1.png");
    mainMenuBtn.x = bg._x+103; mainMenuBtn.y = bg._y+190;
    
    
    winText.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    bg.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    playAgainBtn.bind("MouseDown", function(e) { 
        playAgainBtn.alpha = 0.01;
        playAgainHover.alpha = 1;
    });
    playAgainBtn.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        Crafty.scene('GamePlay');
    });
    
    
    mainMenuBtn.bind("MouseDown", function(e) { 
        mainMenuBtn.alpha = 0.01;
        mainMenuHover.alpha = 1;
    });
    mainMenuBtn.bind("MouseUp", function(e) { 
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
        playerNo = 0;
        Crafty.scene('GameMenu');
    });
    
    playSfxAudio("LevelComplete");
    
});


Crafty.scene('Player2Won', function() {
    console.log("PLAYER 2 WIN SCREEN");
    Crafty.audio.stop();
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image('images/menu_bg.png');
    
    var winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/you_wonplayer2.png');
    winText.x = bg._x+105;   winText.y = bg._y+40; 
    
    var playAgainHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain2.png");
    playAgainHover.x = bg._x+103; playAgainHover.y = bg._y+120;
    playAgainHover.alpha = 0.01;
    
    var playAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/playagain1.png");
    playAgainBtn.x = bg._x+103; playAgainBtn.y = bg._y+120;  


    var mainMenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu2.png");
    mainMenuHover.x = bg._x+103; mainMenuHover.y = bg._y+190;
    mainMenuHover.alpha = 0.01;
    
    var mainMenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/mainmenu1.png");
    mainMenuBtn.x = bg._x+103; mainMenuBtn.y = bg._y+190;
    
    
    winText.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    bg.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
    });
    
    
    playAgainBtn.bind("MouseDown", function(e) { 
        playAgainBtn.alpha = 0.01;
        playAgainHover.alpha = 1;
    });
    playAgainBtn.bind("MouseUp", function(e) { 
        playAgainBtn.alpha = 1;
        playAgainHover.alpha = 0.01;
        Crafty.scene('GamePlay');
    });
    
    
    mainMenuBtn.bind("MouseDown", function(e) { 
        mainMenuBtn.alpha = 0.01;
        mainMenuHover.alpha = 1;
    });
    mainMenuBtn.bind("MouseUp", function(e) { 
        mainMenuBtn.alpha = 1;
        mainMenuHover.alpha = 0.01;
        playerNo = 0;
        Crafty.scene('GameMenu');
    });
    
    playSfxAudio("LevelComplete");

});
    
