
var winCheck = 0;
var cardLoadingIndex = 0;
var cardArray = [];
var cardNameArray = [];
var generalGraphicsArray = [];
var generalGraphicsLoadingIndex = 0;
var deskCanvas;
var deskCanvasID = "deskCanvas"+deskCanvas;
var soundDown = false;

Crafty.scene('TTSplash', function() {
    console.log("TTSplash SCREEN");
   
    var bg = Crafty.e("2D, Canvas, Image").attr({w: 500, h:500}).image("images/tt_Logo.png");
    bg.x = Crafty.viewport.width/2-100; bg.y = Crafty.viewport.height/2-65;
    
    localStorage.setItem("onceFlag","setTrue");
    
    setTimeout(function(){
        Crafty.scene('GameSplash')
        bg.destroy();
    },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: 320, h:440}).image("images/Splash screan.png");
        
    var timer = setTimeout(function(){
        Crafty.scene('GameMenu');
        game_Logo.destroy();
    },3000);
    
    game_Logo.bind("MouseUp", function(e) { 
        clearInterval(timer);
        game_Logo.destroy();
        Crafty.scene('GameMenu');
    });
    
    
});



Crafty.scene('GameMenu', function() {
    
    winCheck = 0;
	console.log("GAME MENU SCREEN");
    cardLoadingIndex=0;
    generalGraphicsLoadingIndex = 0;
    cardArray = [];
    cardNameArray = [];
    generalGraphicsArray = [];

    deskCanvasID = "deskCanvas"+(deskCanvas+1);
    
 
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w: 320, h:440}).image("images/menubg.png");

    var playHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/new_game_2.png");
    playHover.x = bg._x+70;     playHover.y = bg._y+40;
    playHover.alpha = 0.01;
    
    var playBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/new_game_1.png");
    playBtn.x = bg._x+70; playBtn.y = bg._y+40;
                
    var onBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/sound_on.png");
    onBtn.x = bg._x+70; onBtn.y = bg._y+110;
    onBtn.originX = onBtn._x;

    var offBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/sound_off.png");
    offBtn.x = bg._x+70; offBtn.y = bg._y+110;
    offBtn.originX = offBtn._x;

    var helpHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/help_2.png");
    helpHover.x = bg._x+70;     helpHover.y = bg._y+180;
    helpHover.alpha = 0.01;
    
    var helpBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/help_1.png");
    helpBtn.x = bg._x+70; helpBtn.y = bg._y+180;

    var aboutUsHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/about_2.png");
    aboutUsHover.x = bg._x+70;   aboutUsHover.y = bg._y+250;
    aboutUsHover.alpha = 0.01;
    
    var aboutUsBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/about_1.png");
    aboutUsBtn.x = bg._x+70; aboutUsBtn.y = bg._y+250;

    
    
    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("off btn clicked");
       setAudioIsMuted(false)
        onBtn.visible = true;
        offBtn.visible = false;
        offBtn.x = -1000;
        onBtn.x = onBtn.originX
        soundDown = false;
        playBgMusic("BgmMenu")
    });

    onBtn.bind("MouseDown", function(e) {
        console.log("on btn clicked");
       setAudioIsMuted(true)
        offBtn.visible = true;
        onBtn.visible = false;
        offBtn.x = offBtn.originX
        onBtn.x = -1000;
        soundDown = true;
        Crafty.audio.stop();
        setBgMusicPlaying();
    });

    
    playBtn.bind("MouseDown", function(e) { 
        playHover.alpha = 1;
        playBtn.alpha = 0.01;
                
    });
    
    playBtn.bind("MouseUp", function(e) { 
        console.log("play button clicked")
        //  $("#deskCanvas").show();
        $("#mainDiv").show();
        document.getElementById("mainDiv").innerHTML = "";
        cardArray.length = 0;
        generalGraphicsArray.length = 0;
        cardNameArray.length= 0;
        Crafty.scene('GamePlay');
      
    });

    
    helpBtn.bind("MouseDown", function(e) { 
        helpHover.alpha = 1;
        helpBtn.alpha = 0.01;
                
    });
           
    helpBtn.bind("MouseUp", function(e) { 
        helpHover.alpha = 0.01;
        helpBtn.alpha = 1;
        createHelpScreen();
                
    });
           
    aboutUsBtn.bind("MouseDown", function(e) { 
        aboutUsHover.alpha = 1;
        aboutUsBtn.alpha = 0.01;
    });
    
    aboutUsBtn.bind("MouseUp", function(e) { 
        Crafty.scene('AboutUs');
    });
    
    
    bg.bind("MouseUp", function(e) { 
        aboutUsHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        helpHover.alpha = 0.01;
        helpBtn.alpha = 1;
        playHover.alpha = 0.01;
        playBtn.alpha = 1;
                
    });
    
    onBtn.bind("MouseUp", function(e) { 
        aboutUsHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        helpHover.alpha = 0.01;
        helpBtn.alpha = 1;
        playHover.alpha = 0.01;
        playBtn.alpha = 1;
                
    });
    
    offBtn.bind("MouseUp", function(e) { 
        aboutUsHover.alpha = 0.01;
        aboutUsBtn.alpha = 1;
        helpHover.alpha = 0.01;
        helpBtn.alpha = 1;
        playHover.alpha = 0.01;
        playBtn.alpha = 1;
                
    });
    
    
    if(soundDown == false){
        Crafty.audio.stop();
        Crafty.audio.play("BgmMenu", -1)
    }
    
    
});



Crafty.scene('AboutUs', function() {

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

    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image.png");
    backBtnHover.x = bg._x+240; backBtnHover.y = bg._y+370;
    backBtnHover.alpha = 0.01;
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image1.png");
    backBtn.x = bg._x+240; backBtn.y = bg._y+370;
   
    
     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;
    });

    
});


Crafty.scene('YouWon', function() {
    Crafty.audio.stop();
    var bg = Crafty.e("2D, Canvas,Image, Mouse").attr({x:0, y:0, w: 320, h:440}).image("images/menubg.png");

    var winText = Crafty.e("2D, Canvas, Image, Mouse").image("images/congratilation.png");
    winText.x = bg._x+40;   winText.y = bg._y+80;

    var mainmenuHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/Main_menu_2.png");
    mainmenuHover.x = winText._x+32;     mainmenuHover.y = winText._y+132;
    mainmenuHover.alpha = 0.01;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/Main menu.png");
    mainmenuBtn.x = winText._x+32;     mainmenuBtn.y = winText._y+132;

  
    mainmenuBtn.bind("MouseDown", function(e) {
        mainmenuBtn.alpha = 0.01;
        mainmenuHover.alpha = 1;
    })

    mainmenuBtn.bind("MouseUp", function(e) {
        Crafty.scene('GameMenu');
    })
    
     
    winText.bind("MouseUp", function(e) {
        mainmenuBtn.alpha = 1;
        mainmenuHover.alpha = 0.01;
    })

    bg.bind("MouseUp", function(e) {
        mainmenuBtn.alpha = 1;
        mainmenuHover.alpha = 0.01;
    })
    
    playSfxAudio("GameWin")
    

});

