
var lCategory;
var mainIndex;
var betAmountDiffX;
var soundBtn = 1;
var gamelostword;
var loopFlag = true;
var selectedHorse;
var accountBalance = 100;
localStorage.setItem("accountBalance",accountBalance);
var amountEnteredOrNotFlag = false;
var digitFlag = "one";
var val1,val2,val3,val4, val5;
var fromPause = false;
var totalVal, amoutError, amoutError1;
var digit1, digit2, digit3, digit4;

Crafty.scene('TTSplash', function() {
    console.log("TT Splash 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/logo.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/menuBg.png");
    
    var newGameHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/newgame1.png");
    newGameHover.x = bg._x+170; newGameHover.y = bg._y+50;
    
    var newGameBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/newgame.png");
    newGameBtn.x = bg._x+170; newGameBtn.y = bg._y+50;
    
    var helpHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/help1.png");
    helpHover.x = bg._x+170; helpHover.y = bg._y+150;
    
    var helpBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/help.png");
    helpBtn.x = bg._x+170; helpBtn.y = bg._y+150;
    
    var onBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/on.png");
    onBtn.x = bg._x+170; onBtn.y = bg._y+100;
    onBtn.originX = onBtn._x;
    
    var offBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/off.png");
    offBtn.x = bg._x+170; offBtn.y = bg._y+100;
    offBtn.originX = offBtn._x;    
    
    
    var aboutUsHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/about_us_button1.png");
    aboutUsHover.x = bg._x+170; aboutUsHover.y = bg._y+200;
    
    var aboutUsBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/about_us_button.png");
    aboutUsBtn.x = bg._x+170; aboutUsBtn.y = bg._y+200;


    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) {
        setAudioIsMuted(false)
        onBtn.visible = true;
        offBtn.visible = false;
        offBtn.x = -1000;
        onBtn.x = onBtn.originX
        playBgMusic("BgmMenu")
        soundBtn = 1;
	});
    
    onBtn.bind("MouseDown", function(e) {
        setAudioIsMuted(true)
        offBtn.visible = true;
        onBtn.visible = false;
        offBtn.x = offBtn.originX
        onBtn.x = -1000;
        Crafty.audio.stop();
        playSfxAudio("BtnClick");
        setBgMusicPlaying();
        soundBtn = 0;
    });
    
    onBtn.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        newGameBtn.alpha = 1;
        aboutUsBtn.alpha = 1;
    });
    
    offBtn.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        newGameBtn.alpha = 1;
        aboutUsBtn.alpha = 1;
    });
    
    bg.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        newGameBtn.alpha = 1;
        aboutUsBtn.alpha = 1;
    });
    
    helpBtn.bind("MouseDown", function(e) { 
        helpBtn.alpha = 0.01;
    });
    
    helpBtn.bind("MouseUp", function(e) { 
        helpBtn.alpha = 1;
        playSfxAudio("BtnClick");
        createHelpScreen();
    });
    
        
    newGameBtn.bind("MouseDown", function(e) { 
        newGameBtn.alpha = 0.01;
    });
    
    newGameBtn.bind("MouseUp", function(e) { 
        newGameBtn.alpha = 1;
        accountBalance = 100;
        localStorage.setItem("accountBalance",accountBalance);
        playSfxAudio("BtnClick");
        Crafty.scene('SelectCategory');
    });

    
    aboutUsBtn.bind("MouseDown", function(e) { 
        aboutUsBtn.alpha = 0.01;
    });

    aboutUsBtn.bind("MouseUp", function(e) { 
        aboutUsBtn.alpha = 1;
        playSfxAudio("BtnClick");
        createAboutScreen();
    });

    playBgMusic("BgmMenu");

});
 



Crafty.scene('SelectCategory', function() {
	console.log("SelectCategory SCREEN");
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menuBg.png");
    
    var categoryTag = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_horse.png");
    categoryTag.x = bg._x+170; categoryTag.y = bg._y+15;
    
    var selectionBox = Crafty.e("2D, Canvas, Image, Mouse").image("images/FRAME.png");
    selectionBox.x = bg._x-170; selectionBox.y = bg._y+70;
    
    var whiteHorseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/white_horse.png");
    whiteHorseSelect.x = bg._x+30; whiteHorseSelect.y = bg._y+70;
   
    var blackHorseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/black_horse.png");
    blackHorseSelect.x = bg._x+172; blackHorseSelect.y = bg._y+70;

    var brownHorseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/brown_horse.png");
    brownHorseSelect.x = bg._x+315; brownHorseSelect.y = bg._y+70;
  
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image1.png");
    backBtnHover.x = bg._x+428; backBtnHover.y = bg._y+232;    
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image.png");
    backBtn.x = bg._x+428; backBtn.y = bg._y+232;    


    backBtn.bind("MouseDown", function(e) { 
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        playSfxAudio("BtnClick");
        Crafty.scene('GameMenu');
    });
    
 
    bg.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        selectionBox.x = bg._x-170;
    }); 
    
    categoryTag.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        selectionBox.x = bg._x-170;
    });

    
    whiteHorseSelect.bind("MouseDown", function(e) { 
        selectionBox.x = whiteHorseSelect.x;
    });

    blackHorseSelect.bind("MouseDown", function(e) { 
        selectionBox.x = blackHorseSelect.x;
    });
    
    brownHorseSelect.bind("MouseDown", function(e) { 
        selectionBox.x = brownHorseSelect.x;
    });
    
    
    whiteHorseSelect.bind("MouseUp", function(e) { 
        selectedHorse = "whiteHorse";
        playSfxAudio("BtnClick");
        Crafty.scene('SelectedHorseScene');
    });

    blackHorseSelect.bind("MouseUp", function(e) { 
        selectedHorse = "blackHorse";
        playSfxAudio("BtnClick");
        Crafty.scene('SelectedHorseScene');
    });
    
    brownHorseSelect.bind("MouseUp", function(e) { 
        selectedHorse = "brownHorse";
        playSfxAudio("BtnClick");
        Crafty.scene('SelectedHorseScene');
    });
    
    playBgMusic("BgmMenu");
    
});



Crafty.scene('SelectedHorseScene', function() {
    console.log("SelectedHorseScene SCREEN");
   
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/bettingBg.png');
    
    if(selectedHorse == "whiteHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/white_horse.png");
        horseSelect.x = bg._x+172; horseSelect.y = bg._y+70;
    }
    else if(selectedHorse == "blackHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/black_horse.png");
        horseSelect.x = bg._x+172; horseSelect.y = bg._y+70;
    }
    else if(selectedHorse == "brownHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/brown_horse.png");
        horseSelect.x = bg._x+172; horseSelect.y = bg._y+70;
    }

    var selectionBox = Crafty.e("2D, Canvas, Image, Mouse").image("images/FRAME.png");
    selectionBox.x = horseSelect._x; selectionBox.y = horseSelect._y;
    
    var proceedToHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_bar1.png");
    proceedToHover.x = horseSelect._x-13; proceedToHover.y = horseSelect._y+130;
    
    var proceedToBetBar = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_bar.png");
    proceedToBetBar.x = horseSelect._x-13; proceedToBetBar.y = horseSelect._y+130;

    //proceed
    var proceedToBet = Crafty.e("2D, Canvas, Image, Mouse").image("images/procced.png");
    proceedToBet.x = proceedToBetBar._x+9; proceedToBet.y = proceedToBetBar._y+4; 

    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image1.png");
    backBtnHover.x = bg._x+428; backBtnHover.y = bg._y+232;    
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image.png");
    backBtn.x = bg._x+428; backBtn.y = bg._y+232;    


    backBtn.bind("MouseDown", function(e) { 
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        playSfxAudio("BtnClick");
        Crafty.scene('SelectCategory');
    });
    
 
    bg.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        proceedToBetBar.alpha = 1;
    });   
    
    selectionBox.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        proceedToBetBar.alpha = 1;
    });
    
    
    proceedToBetBar.bind("MouseDown", function(e) {        
        proceedToBetBar.alpha = 0.01;
    });
    
    proceedToBetBar.bind("MouseUp", function(e) {  
         proceedToBetBar.alpha = 1;
        playSfxAudio("BtnClick");
        val1 = val2 = val3 = val4 = null;
        digitFlag = "one";
        totalVal = 0;
        Crafty.scene('BetScene');
    });
    
    proceedToBet.bind("MouseDown", function(e) {        
         proceedToBetBar.alpha = 0.01;
    });

    proceedToBet.bind("MouseUp", function(e) {   
         proceedToBetBar.alpha = 1;
        playSfxAudio("BtnClick");
        val1 = val2 = val3 = val4 = null;
        digitFlag = "one";
        totalVal = 0;
        Crafty.scene('BetScene');
    });
    
    
    playBgMusic("BgmMenu");
  
});




Crafty.scene('BetScene', function() {
    console.log("bet SCREEN");
    var noDiffX = 28;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/bettingBg.png');
    
    var backbar = Crafty.e("2D, Canvas, Image, Mouse, LongBackBar");
    backbar.x = bg._x+50; backbar.y = bg._y+115;
 
    var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/ac_balance.png");
    horseSelect.x = bg._x+140; horseSelect.y = bg._y+30;

    accountBalance = localStorage.getItem("accountBalance");
     
    var mainBalance = Crafty.e("2D, DOM, Text").text(accountBalance).textFont({ size: '21px', weight: 'bold' })
    .textColor('black');

    if(accountBalance<10){
        mainBalance.x = 300;
        mainBalance.y = 36;
        betAmountDiffX = 389;
    }
    else if(accountBalance<100){
        mainBalance.x = 293;
        mainBalance.y = 36;
        betAmountDiffX = 381;
    }
    else if(accountBalance<1000){
        mainBalance.x = 286;
        mainBalance.y = 36;
        betAmountDiffX = 375;
    }
    else if(accountBalance<10000){
        mainBalance.x = 280;
        mainBalance.y = 36;
        betAmountDiffX = 366;
    }  
    else if(accountBalance<100000){
        mainBalance.x = 275;
        mainBalance.y = 36;
        betAmountDiffX = 359;
    }  

    
    
    var betTag = Crafty.e("2D, Canvas, Image, Mouse").image("images/bet_amount.png");
    betTag.x = bg._x+100; betTag.y = bg._y+85;

       
    var noOne = Crafty.e("2D, Canvas, Image, Mouse, nos1");
    noOne.x= backbar._x; noOne.y=backbar._y;  

    var noTwo = Crafty.e("2D, Canvas, Image, Mouse, nos2");
    noTwo.x= noOne._x+noDiffX; noTwo.y=backbar._y;    
   
    var noThree = Crafty.e("2D, Canvas, Image, Mouse, nos3");
    noThree.x= noTwo._x+noDiffX; noThree.y=backbar._y;

    var noFour = Crafty.e("2D, Canvas, Image, Mouse, nos4");
    noFour.x= noThree._x+noDiffX; noFour.y=backbar._y;

    var noFive = Crafty.e("2D, Canvas, Image, Mouse, nos5");
    noFive.x= noFour._x+noDiffX; noFive.y=backbar._y;

    var noSix = Crafty.e("2D, Canvas, Image, Mouse, nos6");
    noSix.x= noFive._x+noDiffX; noSix.y=backbar._y;
    
    var noSeven = Crafty.e("2D, Canvas, Image, Mouse, nos7");
    noSeven.x= noSix._x+noDiffX; noSeven.y=backbar._y;

    var noEight = Crafty.e("2D, Canvas, Image, Mouse, nos8");
    noEight.x= noSeven._x+noDiffX; noEight.y=backbar._y;

    var noNine = Crafty.e("2D, Canvas, Image, Mouse, nos9");
    noNine.x= noEight._x+noDiffX; noNine.y=backbar._y;

    var noZero = Crafty.e("2D, Canvas, Image, Mouse, nos0");
    noZero.x= noNine._x+noDiffX; noZero.y=backbar._y;
    
    // bet box 
    var betBox = Crafty.e("2D, Canvas, Image, Mouse").image("images/bet_amount2.png");
    betBox.x = bg._x+348; betBox.y = backbar._y-1;

    
    
    noOne.bind("MouseDown", function(e) {
        totalAmountBetted(1); 
    });

    noTwo.bind("MouseDown", function(e) {        
        totalAmountBetted(2); 
    });

    noThree.bind("MouseDown", function(e) {        
        totalAmountBetted(3); 
    });

    noFour.bind("MouseDown", function(e) {        
        console.log("mouse on 2");        
        totalAmountBetted(4); 
    });

    noFive.bind("MouseDown", function(e) {        
        totalAmountBetted(5); 
    });

    noSix.bind("MouseDown", function(e) {        
        totalAmountBetted(6); 
    });

    noSeven.bind("MouseDown", function(e) {        
        totalAmountBetted(7); 
    });

    noEight.bind("MouseDown", function(e) {        
        totalAmountBetted(8); 
    });

    noNine.bind("MouseDown", function(e) {        
        totalAmountBetted(9); 
    });

    noZero.bind("MouseDown", function(e) {        
        totalAmountBetted(0); 
    });
     
    
    
    var proceedToBetHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/start_race2.png");
    proceedToBetHover.x = bg._x+140; proceedToBetHover.y = bg._y+210;
    
    var proceedToBet = Crafty.e("2D, Canvas, Image, Mouse").image("images/start_race.png");
    proceedToBet.x = bg._x+140; proceedToBet.y = bg._y+210; 

    amoutError = Crafty.e("2D, Canvas, Image, Mouse").image("images/error_msg1.png");
    amoutError.x = bg._x+130; amoutError.y = bg._y+165;    
    amoutError.alpha = 0.01;
    
    amoutError1 = Crafty.e("2D, Canvas, Image, Mouse").image("images/error_msg2.png");
    amoutError1.x = bg._x+30; amoutError1.y = bg._y+165;   
    amoutError1.alpha = 0.01;
    
    
    proceedToBet.bind("MouseDown", function(e) {      
        proceedToBet.alpha = 0.01;
    });
    
    
    
    proceedToBet.bind("MouseUp", function(e) {      
        var oneAccountBalance = localStorage.getItem("accountBalance");
        proceedToBet.alpha = 1;
        if((totalVal == undefined) ||(totalVal == 0)){
            if(accountBalance > 0){
                amoutError.alpha = 1;
            }
            else{
                amoutError1.alpha = 1;
            }
        }else{         
            Crafty.scene('GamePlay');
        }
    });
    
   
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image1.png");
    backBtnHover.x = bg._x+428; backBtnHover.y = bg._y+232;    
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/back_image.png");
    backBtn.x = bg._x+428; backBtn.y = bg._y+232;    


    backBtn.bind("MouseDown", function(e) { 
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        val1 = val2 = val3 = val4 = null;
        totalVal = 0;
        playSfxAudio("BtnClick");
        Crafty.scene('SelectedHorseScene');
    });
    
 
    bg.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        proceedToBet.alpha = 1;
    }); 
    
    amoutError.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        proceedToBet.alpha = 1;
    });
    
    amoutError1.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
        proceedToBet.alpha = 1;
    });
    
    backbar.bind("MouseUp", function(e) { 
        backBtn.alpha = 1;
    });

      
    playBgMusic("BgmMenu");
  
});



function totalAmountBetted(val){
    var amount =  val; //parseInt(val);
    amoutError.alpha = 0.01;
    var betDigitDiffX = 13;
    if( digitFlag == "one"){
        val1 = amount;
        digit1 = Crafty.e("2D, DOM, Text").attr({ x: betAmountDiffX, y: 119 }).text(amount)
        .textFont({ size: '21px', weight: 'bold'}).textColor('black');
        
        digitFlag = "two";
    }
    else if(digitFlag == "two"){
        val2 = amount;
        digit2 = Crafty.e("2D, DOM, Text").attr({ x: betAmountDiffX+betDigitDiffX, y: 119 }).text(amount)
        .textFont({ size: '21px', weight: 'bold' }).textColor('black');
            
        digitFlag = "three";
    }
    else if(digitFlag == "three"){
        val3 = amount;
        digit3 = Crafty.e("2D, DOM, Text").attr({ x: betAmountDiffX+(2*betDigitDiffX), y: 119 }).text(amount)
        .textFont({ size: '21px', weight: 'bold' }).textColor('black');
        
        digitFlag = "four";
    }
    
    else if(digitFlag == "four"){
        val4 = amount;
        digit4 = Crafty.e("2D, DOM, Text").attr({ x: betAmountDiffX+(3*betDigitDiffX), y: 119 }).text(amount)
        .textFont({ size: '21px', weight: 'bold' }).textColor('black');
        digitFlag = "five";
    }
    
    else if(digitFlag == "five"){
        val5 = amount;
        digitFlag = "one";
    }
    
    

    if(val3 == null && val2 == null){
        totalVal = val1.toString();
        console.log("totalVal1 = "+totalVal);
    }
    else if(val3 == null){
        totalVal = val1.toString()+val2.toString();
        console.log("totalVal2 = "+totalVal);
    }
    else if((val1 != null) && (val2 != null) && (val3 != null) && (val4 == null)){
        totalVal = val1.toString()+val2.toString()+val3.toString();
        console.log("totalVal3 = "+totalVal);
    }
    else if((val1 != null) && (val2 != null) && (val3 != null) && (val4 != null) && (val5 == null)){
        totalVal = val1.toString()+val2.toString()+val3.toString()+val4.toString();
        console.log("totalVal4 = "+totalVal);
    }
    else if((val1 != null) && (val2 != null) && (val3 != null) && (val4 != null) && (val5 != null)){
        totalVal = val1.toString()+val2.toString()+val3.toString()+val4.toString()+val5.toString();
        console.log("totalVal5 = "+totalVal);
    }
  
    accountBalance = localStorage.getItem("accountBalance");
    totalVal = parseInt(totalVal);
    accountBalance = parseInt(accountBalance);
   
    if(accountBalance > 0){
        if((totalVal > accountBalance) || (totalVal == 0)){
            val1 = val2 = val3 = val4 = val5 = null;
            totalVal = 0;
            if(digitFlag == "one"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
                digit4.destroy();
            }
            else if(digitFlag == "five"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
                digit4.destroy();
            }
           else if(digitFlag == "four"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
            }
            else if(digitFlag == "two"){
                digit1.destroy();
            }
            else if(digitFlag == "three"){
                digit1.destroy();
                digit2.destroy();
            }
            
            digitFlag = "one";
            
            amoutError.alpha = 1;
        }
        else{
        }
    }
    else{
        val1 = val2 = val3 = val4 = null;
        totalVal = 0;
        if(digitFlag == "one"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
                digit4.destroy();
            }
            else if(digitFlag == "five"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
                digit4.destroy();
            }
           else if(digitFlag == "four"){
                digit1.destroy();
                digit2.destroy();
                digit3.destroy();
            }
            else if(digitFlag == "two"){
                digit1.destroy();
            }
            else if(digitFlag == "three"){
                digit1.destroy();
                digit2.destroy();
            }
        digitFlag = "one";
        amoutError1.alpha = 1;
    }

}



Crafty.scene('GameWin', function() {
    console.log("win SCREEN");
    
    Crafty.audio.stop();
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/bettingBg.png');
    
    var bgTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/status_tab.png");
    bgTab.x = bg._x+93; bgTab.y = bg._y+45;
    
    var winTag = Crafty.e("2D, Canvas, Image").image("images/won.png");
    winTag.x = bgTab._x+17; winTag.y = bgTab._y+10;

    accountBalance = accountBalance + (totalVal);
    localStorage.setItem("accountBalance",accountBalance);

    var dispAccountBalance = (totalVal*2);
    
//    Crafty.e("2D, DOM, Text").attr({ x: 225, y: 110 }).text(dispAccountBalance)
//    .textFont({ size: '20px', weight: 'bold' }).textColor('black');
    
    
    var selYesHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/select1.png");
    selYesHover.x = bgTab._x+6; selYesHover.y = bgTab._y+135;
    
    var selYes = Crafty.e("2D, Canvas, Image, Mouse").image("images/select.png");
    selYes.x = bgTab._x+6; selYes.y = bgTab._y+135;    
    
    var selNoHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/exit1.png");
    selNoHover.x = bgTab._x+243; selNoHover.y = bgTab._y+135;
    
    var selNo = Crafty.e("2D, Canvas, Image, Mouse").image("images/exit.png");
    selNo.x = bgTab._x+243; selNo.y = bgTab._y+135;    

 
    
    selNo.bind("MouseDown", function(e) { 
        selNo.alpha = 0.01;
    });
    
    selYes.bind("MouseDown", function(e) { 
       selYes.alpha = 0.01;
    });
    
    bg.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
    
    bgTab.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
    
    winTag.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
    
    
    
    selNo.bind("MouseUp", function(e) { 
        selNo.alpha = 1;
        setBgMusicPlaying();
        playSfxAudio("BtnClick");
        playBgMusic("BgmMenu");
        resetGame();
        Crafty.scene('GameMenu'); 
    });

    selYes.bind("MouseUp", function(e) {
        selYes.alpha = 1;
        setBgMusicPlaying();
        playSfxAudio("BtnClick");
        playBgMusic("BgmMenu");
        totalVal = 0;
        resetGame();
        Crafty.scene('SelectCategory');
    });
    
    playSfxAudio("LevelComplete");

    
});
    


Crafty.scene('GameLost', function() {
    console.log("Lost SCREEN");
    
    Crafty.audio.stop();
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/bettingBg.png');
    
    var bgTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/status_tab.png");
    bgTab.x = bg._x+93; bgTab.y = bg._y+45;
    
    var lostTag = Crafty.e("2D, Canvas, Image").image("images/lost.png");
    lostTag.x = bgTab._x+2; lostTag.y = bgTab._y+10;

    accountBalance = accountBalance - totalVal;
    localStorage.setItem("accountBalance",accountBalance);
    
    var dispAccountBalance = totalVal;
    
//    Crafty.e("2D, DOM, Text").attr({ x: 230, y: 82 }).text(dispAccountBalance)
//    .textFont({ size: '20px', weight: 'bold' }).textColor('black');

    
    var selYesHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/select1.png");
    selYesHover.x = bgTab._x+6; selYesHover.y = bgTab._y+135;
    
    var selYes = Crafty.e("2D, Canvas, Image, Mouse").image("images/select.png");
    selYes.x = bgTab._x+6; selYes.y = bgTab._y+135;    
    
    var selNoHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/exit1.png");
    selNoHover.x = bgTab._x+243; selNoHover.y = bgTab._y+135;
    
    var selNo = Crafty.e("2D, Canvas, Image, Mouse").image("images/exit.png");
    selNo.x = bgTab._x+243; selNo.y = bgTab._y+135;    

 
    
    selNo.bind("MouseDown", function(e) { 
        selNo.alpha = 0.01;
    });
    
    selYes.bind("MouseDown", function(e) { 
       selYes.alpha = 0.01;
    });
    
    bg.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
    
    bgTab.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
    
    lostTag.bind("MouseUp", function(e) { 
        selYes.alpha = 1;
        selNo.alpha = 1;
    });
 
    selNo.bind("MouseUp", function(e) { 
        selNo.alpha = 1;
        setBgMusicPlaying();
        playSfxAudio("BtnClick");
        playBgMusic("BgmMenu");
        resetGame();
        Crafty.scene('GameMenu');
    });

    selYes.bind("MouseUp", function(e) { 
        selNo.alpha = 1;
        setBgMusicPlaying();
        playSfxAudio("BtnClick");
        playBgMusic("BgmMenu");
        totalVal = 0;
        resetGame();
        Crafty.scene('SelectCategory');
    });
    
    playSfxAudio("LevelLost");

});

