
var arrCar = [
    [["cat1/0.png", "cat1/1.png", "cat1/2.png"], ["cat1/3.png","cat1/4.png","cat1/5.png"],
     ["cat1/6.png","cat1/7.png","cat1/8.png"]],
    [["cat2/0.png", "cat2/1.png", "cat2/2.png"], ["cat2/3.png","cat2/4.png","cat2/5.png"],
     ["cat2/6.png","cat2/7.png","cat2/8.png"]],
    [["cat3/0.png", "cat3/1.png", "cat3/2.png"], ["cat3/3.png","cat3/4.png","cat3/5.png"],
     ["cat3/6.png","cat3/7.png","cat3/8.png"]],
    [["cat4/0.png", "cat4/1.png", "cat4/2.png"], ["cat4/3.png","cat4/4.png","cat4/5.png"],
     ["cat4/6.png","cat4/7.png","cat4/8.png"]]
];
var unlockScreen = 0; 
var trackNo;
var gameMode;
var boardBack;
var categoryCount1, carCount1, colorCount1;

var noOfCar = 3;
var winCount = 1;
var noOfColor = 3;
var noOfCatagory = 4;

var carCount = 0;
var colorCount = 0;
var categoryCount = 0;

var soundDown = false;
var pathSelect = "images/select/";
var pathIngame = "images/ingame/";
var pathSmallHeader = "images/small/";

var arrCarType = [noOfCar];
var arrColor = [noOfColor];
var arrCarSmall = [noOfCar];
var arrColorSmall = [noOfColor];
var arrCatagory = [noOfCatagory];
var arrCatagorySmall = [noOfCatagory];

var pGameFinish = false;
var winRaceCount = 0;
var unlockCarCount = 0;
var unlockCategoryCount = 0;
var pBeginCarId, pEndCarId;
var unlockCategoryId, unlockCarId;



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-175; bg.y = Crafty.viewport.height/2-100;
    
	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");
    winRaceCount = 0;
    
    if(localStorage.getItem("GameUnlock", 1)) {
        pGameFinish = true;
    }
    console.log("pGameFinish = "+pGameFinish);
    
    winCount = 1;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var newGameHover2 = Crafty.e("2D, Canvas, Image, Mouse, Menuback2");
    newGameHover2.x = bg._x+135; newGameHover2.y = bg._y+25;
    
    var newGameHover1 = Crafty.e("2D, Canvas, Image, Mouse, Menuback1");
    newGameHover1.x = bg._x+135; newGameHover1.y = bg._y+25;
    
    var newGameBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/newgame1.png");
    newGameBtn.x = bg._x+172; newGameBtn.y = bg._y+33;
	    
    
    var optionHover2 = Crafty.e("2D, Canvas, Image, Mouse, Menuback2");
    optionHover2.x = bg._x+135; optionHover2.y = bg._y+88;
    
    var optionHover1 = Crafty.e("2D, Canvas, Image, Mouse, Menuback1");
    optionHover1.x = bg._x+135; optionHover1.y = bg._y+88;
   
    var optionBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/options_button.png");
    optionBtn.x = bg._x+190; optionBtn.y = bg._y+97;
   
    
    var helpBtnHover2 = Crafty.e("2D, Canvas, Image, Mouse, Menuback2");
    helpBtnHover2.x = bg._x+135; helpBtnHover2.y = bg._y+151;
    
    var helpBtnHover1 = Crafty.e("2D, Canvas, Image, Mouse, Menuback1");
    helpBtnHover1.x = bg._x+135; helpBtnHover1.y = bg._y+151;
    
    var helpBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/help1.png");
    helpBtn.x = bg._x+207; helpBtn.y = bg._y+160;
    
    
    var aboutUsHover2 = Crafty.e("2D, Canvas, Image, Mouse, Menuback2");
    aboutUsHover2.x = bg._x+135; aboutUsHover2.y = bg._y+215;
    
    var aboutUsHover1 = Crafty.e("2D, Canvas, Image, Mouse, Menuback1");
    aboutUsHover1.x = bg._x+135; aboutUsHover1.y = bg._y+215;
    
    var aboutUsBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/aboutus1.png");
    aboutUsBtn.x = bg._x+175; aboutUsBtn.y = bg._y+225;


	
    helpBtn.bind("MouseDown", function(e) { 
        helpBtnHover1.alpha = 0.01;
    });
    helpBtnHover1.bind("MouseDown", function(e) { 
        helpBtnHover1.alpha = 0.01;
    });
    
    helpBtn.bind("MouseUp", function(e) { 
        helpBtnHover1.alpha = 1;
        createHelpScreen();
    });
    helpBtnHover1.bind("MouseUp", function(e) {
        helpBtnHover1.alpha = 1;
        createHelpScreen();
    });
    
    
    
    optionBtn.bind("MouseDown", function(e) { 
        optionHover1.alpha = 0.01;
    });
    optionHover1.bind("MouseDown", function(e) { 
        optionHover1.alpha = 0.01;
    });
    
    optionBtn.bind("MouseUp", function(e) { 
        optionHover1.alpha = 1;
        createOptionScreen();
    });
    optionHover1.bind("MouseUp", function(e) {
        optionHover1.alpha = 1;
        createOptionScreen();
    });
    
    

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

    bg.bind("MouseUp", function(e) { 
        newGameHover1.alpha = 1;
        optionHover1.alpha = 1;
        helpBtnHover1.alpha = 1;
        aboutUsHover1.alpha = 1;
    });
    
   
    if(soundDown == false){
        if(boardBack == true){}
        else{
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
    }

});
 

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

    var tabBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/menu_back.png');
    tabBg.x = bg._x+75;    tabBg.y = bg._y+20;
    
    var modeTag = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_mode.png");
    modeTag.x = tabBg._x+63;      modeTag.y = tabBg._y+15;
    
    
    var mode1Hover2 = Crafty.e("2D, Canvas, Image, Mouse, Modeback2");
    mode1Hover2.x = tabBg._x+50;      mode1Hover2.y = tabBg._y+70;
    
    var mode1Hover1 = Crafty.e("2D, Canvas, Image, Mouse, Modeback1");
    mode1Hover1.x = tabBg._x+50;      mode1Hover1.y = tabBg._y+70;
    
    var mode1Btn = Crafty.e("2D, Canvas, Image, Mouse").image("images/one_on-_one.png");
    mode1Btn.x = tabBg._x+93;      mode1Btn.y = tabBg._y+83;
    
    
    var mode2Hover2 = Crafty.e("2D, Canvas, Image, Mouse, Modeback2");
    mode2Hover2.x = tabBg._x+50;      mode2Hover2.y = tabBg._y+130;
    
    var mode2Hover1 = Crafty.e("2D, Canvas, Image, Mouse, Modeback1");
    mode2Hover1.x = tabBg._x+50;      mode2Hover1.y = tabBg._y+130;
    
    var mode2Btn = Crafty.e("2D, Canvas, Image, Mouse").image("images/tournament.png");
    mode2Btn.x = tabBg._x+93;      mode2Btn.y = tabBg._y+143;
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse, BackBtn2");
    backBtnHover.x = bg._x+418;   backBtnHover.y = bg._y+225;   
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse, BackBtn1");
    backBtn.x = bg._x+418;   backBtn.y = bg._y+225;    

    backBtn.bind("MouseDown", function(e) {
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        boardBack = true;
        Crafty.scene('GameMenu');
    });
    
    tabBg.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        mode1Hover1.alpha = 1;
        mode2Hover1.alpha = 1;
    });
    
    bg.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        mode1Hover1.alpha = 1;
        mode2Hover1.alpha = 1;
    });
    

    mode1Hover1.bind("MouseDown", function(e) { 
        mode1Hover1.alpha = 0.01;
    });
    mode1Btn.bind("MouseDown", function(e) { 
        mode1Hover1.alpha = 0.01;
    });
    
    mode1Hover1.bind("MouseUp", function(e) { 
        mode1Hover1.alpha = 1;
        gameMode = 1;
        Crafty.scene('Loading');
    });
    mode1Btn.bind("MouseUp", function(e) {
        mode1Hover1.alpha = 1;
        gameMode = 1;
        Crafty.scene('Loading');
    });
    
    
    mode2Hover1.bind("MouseDown", function(e) { 
        mode2Hover1.alpha = 0.01;
    });
    mode2Btn.bind("MouseDown", function(e) { 
        mode2Hover1.alpha = 0.01;
    });
    
    mode2Hover1.bind("MouseUp", function(e) { 
        mode2Hover1.alpha = 1;
        gameMode = 2;
        Crafty.scene('Loading');
    });
    mode2Btn.bind("MouseUp", function(e) {
        mode2Hover1.alpha = 1;
        gameMode = 2;
        Crafty.scene('Loading');
    });
    
});


Crafty.scene('Loading', function() {
	console.log("GAME SPLASH SCREEN");
    
    trackNo = Crafty.math.randomInt(1,5);
    
    var load_Logo = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image("images/menu_bg.png");
    
    var loadText = Crafty.e("2D, Canvas, Image, Mouse").image("images/loading.png");
    loadText.x = load_Logo._x+170;       loadText.y = load_Logo._y+120;
        
	var bTime1 = setTimeout(function(){
       	Crafty.scene('SelectCategory'); 
        load_Logo.destroy();
        loadText.destroy();
    },1500);

});



Crafty.scene('SelectCategory', function() {
	console.log("SelectCategory SCREEN");
    categoryCount = 0;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var startX = bg._x + 100;
    var startX1 = bg._x + 15;
    var beginX;
    var endX;
    boardBack = false; 
    var leftDown;
    var rightDown;
    
    var categoryTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_level.png");
    categoryTab.x = bg._x+85; categoryTab.y = bg._y+7;
    
    var categoryBg = Crafty.e("2D, Canvas, Image, Mouse").image("images/car_back.png");
    categoryBg.x = bg.x; categoryBg.y = categoryTab._y+34;
    
    var selectTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_tab.png");
    selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
   
    
    
    for(var i = 0;i < arrCar.length; i++) {
        arrCatagorySmall[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSmallHeader + "" +arrCar[i][0][0]);
        arrCatagorySmall[i].x = startX1 + (i * 155); arrCatagorySmall[i].y = bg._y + 50; 

        arrCatagory[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSelect + "" +arrCar[i][0][0]);
        arrCatagory[i].x = startX + (i * 480); arrCatagory[i].y = bg._y + 130; 
    }
    
    var lockCategory =  new Array();
    
    if(pGameFinish == false){
        for(var i=1; i<=4; i++) {
            if(getStorageItem("Tournament"+i)==1) {
                unlockCategoryId = i;
            }
            else {
                lockCategory[i-1] = Crafty.e("2D, Canvas, Lock");
                lockCategory[i-1].x = arrCatagorySmall[i-1]._x+35;
                lockCategory[i-1].y = arrCatagorySmall[i-1]._y-2;
                lockCategory[i-1].islocked = true
            }
        }           
    }
    
    
    var lockText = Crafty.e("2D, Canvas, Image, Mouse").image("images/tournament_unlock.png");
    lockText.x = bg._x + 66; lockText.y = bg._y + 160; 
    lockText.alpha = 0.01;
    
    var swipeHover = Crafty.e("2D, Canvas, Color, Mouse").attr({w: 330, h:130}).color("white");
    swipeHover.x = bg._x + 80; swipeHover.y = bg._y+120;
    swipeHover.alpha = 0.01;
    
    
    var leftArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Left2");
    leftArrowHover.x = bg._x+30; leftArrowHover.y = bg._y+150;
    leftArrowHover.alpha = 0.01;
    
    var leftArrow = Crafty.e("2D, Canvas, Image, Mouse, Left1");
    leftArrow.x = bg._x+30; leftArrow.y = bg._y+150;
    leftArrow.alpha = 0.01;
    
    
    var rightArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Right2");
    rightArrowHover.x = bg._x+420; rightArrowHover.y = bg._y+150;
    rightArrowHover.alpha = 0.01;
    
    var rightArrow = Crafty.e("2D, Canvas, Image, Mouse, Right1");
    rightArrow.x = bg._x+420; rightArrow.y = bg._y+150;
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse, BackBtn2");
    backBtnHover.x = bg._x+418;   backBtnHover.y = bg._y+225;   
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse, BackBtn1");
    backBtn.x = bg._x+418;   backBtn.y = bg._y+225;    
    
    var nextBtnHover = Crafty.e("2D, Canvas, Image, Mouse, Select2");
    nextBtnHover.x = bg._x+18; nextBtnHover.y = bg._y+225;   
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse, Select1");
    nextBtn.x = bg._x+18; nextBtn.y = bg._y+225;
    
    
    function rightArrowMovement(){
        if(categoryCount == 2){
            for(var i = 0;i < arrCar.length; i++) {
                arrCatagorySmall[i].x = arrCatagorySmall[i]._x-155; arrCatagorySmall[i].y = bg._y + 50; 
            }
            if(unlockCategoryId<4 && pGameFinish == false){
                lockCategory[3].x = arrCatagorySmall[unlockCategoryId]._x+35;
                lockCategory[3].y = arrCatagorySmall[unlockCategoryId]._y-2;
            }
            rightArrow.alpha = 0.01;
            rightArrowHover.alpha = 0.01;
            console.log("categoryCount == 2");
        }     
        if(categoryCount<3){
            leftArrow.alpha = 1;
            if(categoryCount<2){
                console.log("categoryCount < 2");
                selectTab.x = selectTab._x + 155; selectTab.y = bg._y+45;
            }
            for(var i = 0;i < arrCar.length; i++) {
                arrCatagory[i].x = arrCatagory[i]._x-480; arrCatagory[i].y = bg._y + 130; 
            }
            categoryCount++;
            console.log("categoryCount <3");
        }
        beginX = undefined;
        if(categoryCount<unlockCategoryId){
            if(pGameFinish == false){
                lockText.alpha = 0.01;
            }
        }
        else{
            if(pGameFinish == false){
                lockText.alpha = 1;
            }
        }
    }
    
    
    function leftArrowMovement(){
        if(categoryCount>0){
            rightArrow.alpha = 1;
            for(var i = 0;i < arrCar.length; i++) {
                arrCatagory[i].x = arrCatagory[i]._x+480; arrCatagory[i].y = bg._y + 130;
            }
            console.log("categoryCount >0");
            categoryCount--;
        }
        if(categoryCount == 0){
            selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
            leftArrow.alpha = 0.01;
            leftArrowHover.alpha = 0.01;
        }         
        if(categoryCount>0 && categoryCount<2){
            selectTab.x = selectTab._x - 155; selectTab.y = bg._y+45;
        }
        if(categoryCount == 2){
            for(var i = 0; i < arrCar.length; i++) {
                arrCatagorySmall[i].x = arrCatagorySmall[i]._x+155; arrCatagorySmall[i].y = bg._y + 50; 
            }
            if(pGameFinish == false){
                lockCategory[3].x = arrCatagorySmall[arrCar.length-1]._x+35;
                lockCategory[3].y = arrCatagorySmall[arrCar.length-1]._y-2;
            }
            rightArrow.alpha = 1;
        }
        beginX = undefined;
        if(categoryCount<unlockCategoryId){
            if(pGameFinish == false){
                lockText.alpha = 0.01;
            }
        }
        else{
            if(pGameFinish == false){
                lockText.alpha = 1;
            }
        }
    }
 
    
    function arrowClick(){
        if(leftDown == true){
            leftDown = false;
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        if(rightDown == true){
            rightDown = false;
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
        
    }
    
    swipeHover.bind("MouseDown", function(e) {
        beginX = e.clientX;
    });
    swipeHover.bind("MouseUp", function(e) {
        arrowClick()
        nextBtn.alpha = 1;
        backBtn.alpha = 1;
        endX = e.clientX;
        if(beginX>endX && beginX != undefined){
            rightArrowMovement();
        }
        if(endX>beginX && beginX != undefined){
            leftArrowMovement();
        }
    });
    
     bg.bind("MouseUp", function(e) {
         nextBtn.alpha = 1;
         backBtn.alpha = 1;
         arrowClick()
    });
    
    
    rightArrow.bind("MouseDown", function(e) { 
        if(categoryCount > 2){rightDown = false;}
        else{
            rightDown = true;
            rightArrow.alpha = 0.01;
            rightArrowHover.alpha = 1;
        }
    });
    
    leftArrow.bind("MouseDown", function(e) { 
        if(categoryCount == 0){leftDown = false;}
        else{
            leftDown = true;
            leftArrow.alpha = 0.01;
            leftArrowHover.alpha = 1;
        }
    });
    
    rightArrow.bind("MouseUp", function(e) { 
        if(categoryCount > 2){}
        else{
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
        rightArrowMovement();
    });
    
    leftArrow.bind("MouseUp", function(e) { 
        if(categoryCount == 0){}
        else{
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        leftArrowMovement();
    });
    
        
    nextBtn.bind("MouseDown", function(e) {
        nextBtn.alpha = 0.01;
    });
    nextBtn.bind("MouseUp", function(e) {
        nextBtn.alpha = 1;
        if(pGameFinish == false){
            if(categoryCount<unlockCategoryId){
                Crafty.scene('SelectCar');
            }
            else{  
            }
        }
        else{
            Crafty.scene('SelectCar');
        }
    });   
    
    
    backBtn.bind("MouseDown", function(e) {
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        categoryCount = 0;
        Crafty.scene('SelectMode');
    });
    
    
});


var unlockCarIdCopy;

Crafty.scene('SelectCar', function() {
	console.log("SelectCar SCREEN");
    carCount = 0;
    
    if(categoryCount == 0){pBeginCarId = 1; pEndCarId = pBeginCarId+2;unlockCarIdCopy = 0;}
    else if(categoryCount == 1){pBeginCarId = 4;    pEndCarId = pBeginCarId+2;unlockCarIdCopy = 3;}
    else if(categoryCount == 2){pBeginCarId = 7;    pEndCarId = pBeginCarId+2;unlockCarIdCopy = 6;}
    else if(categoryCount == 3){pBeginCarId = 10;    pEndCarId = pBeginCarId+2;unlockCarIdCopy = 9;}
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var startX = bg._x + 100;
    var startX1 = bg._x + 15;
    var beginX;
    var endX;
    var leftDown;
    var rightDown;
    
    var categoryTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_car.png");
    categoryTab.x = bg._x+130; categoryTab.y = bg._y+7;
    
    var categoryBg = Crafty.e("2D, Canvas, Image, Mouse").image("images/car_back.png");
    categoryBg.x = bg.x; categoryBg.y = categoryTab._y+34;
    
    var selectTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_tab.png");
    selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
    
    
    for(var i = 0;i < arrCar.length-1; i++) {
        arrCarSmall[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSmallHeader + "" +arrCar[categoryCount][i][0]);
        arrCarSmall[i].x = startX1 + (i * 155); arrCarSmall[i].y = bg._y + 50; 

        arrCarType[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSelect + "" +arrCar[categoryCount][i][0]);
        arrCarType[i].x = startX + (i * 480); arrCarType[i].y = bg._y + 130; 
    }
    
    var lockCarArray =  new Array();
    if(pGameFinish == false){
        if(categoryCount == unlockCategoryId-1){
            for(var i=pBeginCarId; i<=pEndCarId; i++) {
                if(getCarStorageItem("Car"+i)==1) {
                    console.log("getCarStorageItem(Car+i)"+getCarStorageItem("Car"+i));
                    unlockCarId = i;
                   // unlockCarIdCopy = i;
                }
                else {
                    lockCarArray[i-pBeginCarId] = Crafty.e("2D, Canvas, Lock");
                    lockCarArray[i-pBeginCarId].x = arrCarSmall[i-pBeginCarId]._x+35;
                    lockCarArray[i-pBeginCarId].y = arrCarSmall[i-pBeginCarId]._y-2;
                    lockCarArray[i-pBeginCarId].islocked = true
                }
            }  
        }
    }
    
    var lockText = Crafty.e("2D, Canvas, Image, Mouse").image("images/tournament_unlock.png");
    lockText.x = bg._x + 66; lockText.y = bg._y + 160; 
    lockText.alpha = 0.01;
    
    var swipeHover = Crafty.e("2D, Canvas, Color, Mouse").attr({w: 330, h:130}).color("white");
    swipeHover.x = bg._x + 80; swipeHover.y = bg._y+120; 
    swipeHover.alpha = 0.01;
    
    var leftArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Left2");
    leftArrowHover.x = bg._x+30; leftArrowHover.y = bg._y+150;
    leftArrowHover.alpha = 0.01;
    
    var leftArrow = Crafty.e("2D, Canvas, Image, Mouse, Left1");
    leftArrow.x = bg._x+30; leftArrow.y = bg._y+150;
    leftArrow.alpha = 0.01;
    
    var rightArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Right2");
    rightArrowHover.x = bg._x+420; rightArrowHover.y = bg._y+150;
    rightArrowHover.alpha = 0.01;
    
    var rightArrow = Crafty.e("2D, Canvas, Image, Mouse, Right1");
    rightArrow.x = bg._x+420; rightArrow.y = bg._y+150;
    
    var nextBtnHover = Crafty.e("2D, Canvas, Image, Mouse, Select2");
    nextBtnHover.x = bg._x+18; nextBtnHover.y = bg._y+225;   
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse, Select1");
    nextBtn.x = bg._x+18; nextBtn.y = bg._y+225;    
    
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse, BackBtn2");
    backBtnHover.x = bg._x+418;   backBtnHover.y = bg._y+225;   
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse, BackBtn1");
    backBtn.x = bg._x+418;   backBtn.y = bg._y+225;
    
    
    function rightArrowMovement(){
        if(carCount == 1){
            rightArrow.alpha = 0.01;
            rightArrowHover.alpha = 0.01;
        }     
        if(carCount<2){
            leftArrow.alpha = 1;
            selectTab.x = selectTab._x + 155; selectTab.y = bg._y+45;
            for(var i = 0;i < arrCar.length-1; i++) {
                arrCarType[i].x = arrCarType[i]._x-480; arrCarType[i].y = bg._y + 130; 
            }
            carCount++;
            unlockCarIdCopy++;
        }
        beginX = undefined;
        if(unlockCarIdCopy<unlockCarId){
            if(pGameFinish == false){
                lockText.alpha = 0.01;
            }
        }
        else{
            if(pGameFinish == false){
                lockText.alpha = 1;
            }
        }
    }
    
    
    function leftArrowMovement(){
        if(carCount>0){
            rightArrow.alpha = 1;
            for(var i = 0;i < arrCar.length-1; i++) {
                arrCarType[i].x = arrCarType[i]._x+480; arrCarType[i].y = bg._y + 130;
            }
            selectTab.x = selectTab._x - 155; selectTab.y = bg._y+45;
            carCount--;
            unlockCarIdCopy--;
        }
        if(carCount == 0){
            selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
            leftArrow.alpha = 0.01;
        }         
        beginX = undefined;
        if(unlockCarIdCopy<unlockCarId){
            if(pGameFinish == false){
                lockText.alpha = 0.01;
            }
        }
        else{
            if(pGameFinish == false){
                lockText.alpha = 1;
            }
        }
    }
 
    
    function arrowClick(){
        if(leftDown == true){
            leftDown = false;
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        if(rightDown == true){
            rightDown = false;
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
    }
    
    
    swipeHover.bind("MouseDown", function(e) {
        beginX = e.clientX;
    });
    swipeHover.bind("MouseUp", function(e) {
        arrowClick()
        nextBtn.alpha = 1;
        backBtn.alpha = 1;
        endX = e.clientX;
        if(beginX>endX && beginX != undefined){
            rightArrowMovement();
        }
        if(endX>beginX && beginX != undefined){
            leftArrowMovement();
        }
    });
    
     bg.bind("MouseUp", function(e) {
         nextBtn.alpha = 1;
         backBtn.alpha = 1;
         arrowClick()
    });
    
    
    rightArrow.bind("MouseDown", function(e) { 
        if(carCount > 1){rightDown = false;}
        else{
            rightDown = true;
            rightArrow.alpha = 0.01;
            rightArrowHover.alpha = 1;
        }
    });
    
    leftArrow.bind("MouseDown", function(e) { 
        if(carCount == 0){leftDown = false;}
        else{
            leftDown = true;
            leftArrow.alpha = 0.01;
            leftArrowHover.alpha = 1;
        }
    });
    
    rightArrow.bind("MouseUp", function(e) { 
        if(carCount > 1){}
        else{
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
        rightArrowMovement();
    });
    
    leftArrow.bind("MouseUp", function(e) { 
        if(carCount == 0){}
        else{
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        leftArrowMovement();
    });
    
  
    nextBtn.bind("MouseDown", function(e) {
        nextBtn.alpha = 0.01;
    });
    nextBtn.bind("MouseUp", function(e) {
        nextBtn.alpha = 1;
        if(categoryCount == unlockCategoryId-1 && pGameFinish == false){
            console.log("categoryCount = "+categoryCount);
            console.log("unlockCategoryId = "+unlockCategoryId);
            if(unlockCarIdCopy<unlockCarId){
           // if(getCarStorageItem("Car"+unlockCarId)==1){
                console.log("unlockCarIdCopy = "+unlockCarIdCopy);
                console.log("unlockCarId = "+unlockCarId);
                Crafty.scene('SelectColor');
            }
        }
        else{
            Crafty.scene('SelectColor');
        }
    });  
    
    
    backBtn.bind("MouseDown", function(e) {
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        categoryCount = 0;
        Crafty.scene('SelectCategory');
    });  

});




Crafty.scene('SelectColor', function() {
    console.log("SelectCar SCREEN");
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0, y:0, w:480, h:280}).image("images/menu_bg.png");
    
    var startX = bg._x + 100;
    var startX1 = bg._x + 15;
    var beginX;
    var endX;
    var leftDown;
    var rightDown;
    
    var categoryTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_color.png");
    categoryTab.x = bg._x+130; categoryTab.y = bg._y+7;
    
    var categoryBg = Crafty.e("2D, Canvas, Image, Mouse").image("images/car_back.png");
    categoryBg.x = bg.x; categoryBg.y = categoryTab._y+34;
    
    var selectTab = Crafty.e("2D, Canvas, Image, Mouse").image("images/select_tab.png");
    selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
    
    
    for(var i = 0;i < arrCar.length-1; i++) {
        arrColorSmall[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSmallHeader + ""+ arrCar[categoryCount][carCount][i]);
        arrColorSmall[i].x = startX1 + (i * 155); arrColorSmall[i].y = bg._y + 50; 

        arrColor[i] = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSelect + "" +arrCar[categoryCount][carCount][i]);
        arrColor[i].x = startX + (i * 480); arrColor[i].y = bg._y + 130; 
    }
    

    var swipeHover = Crafty.e("2D, Canvas, Color, Mouse").attr({w: 330, h:130}).color("white");
    swipeHover.x = bg._x + 80; swipeHover.y = bg._y+120; 
    swipeHover.alpha = 0.01;
    
    var leftArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Left2");
    leftArrowHover.x = bg._x+30; leftArrowHover.y = bg._y+150;
    leftArrowHover.alpha = 0.01;
    
    var leftArrow = Crafty.e("2D, Canvas, Image, Mouse, Left1");
    leftArrow.x = bg._x+30; leftArrow.y = bg._y+150;
    leftArrow.alpha = 0.01;
    
    
    var rightArrowHover = Crafty.e("2D, Canvas, Image, Mouse, Right2");
    rightArrowHover.x = bg._x+420; rightArrowHover.y = bg._y+150;
    rightArrowHover.alpha = 0.01;
    
    var rightArrow = Crafty.e("2D, Canvas, Image, Mouse, Right1");
    rightArrow.x = bg._x+420; rightArrow.y = bg._y+150;
    
   var nextBtnHover = Crafty.e("2D, Canvas, Image, Mouse, Select2");
    nextBtnHover.x = bg._x+18; nextBtnHover.y = bg._y+225;   
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse, Select1");
    nextBtn.x = bg._x+18; nextBtn.y = bg._y+225;    
       
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse, BackBtn2");
    backBtnHover.x = bg._x+418;   backBtnHover.y = bg._y+225;   
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse, BackBtn1");
    backBtn.x = bg._x+418;   backBtn.y = bg._y+225;    

    backBtn.bind("MouseDown", function(e) {
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        carCount = 0;
        colorCount = 0;
        Crafty.scene('SelectCar');
    });      
    
    
    nextBtn.bind("MouseDown", function(e) {
        nextBtn.alpha = 0.01;
    });
    nextBtn.bind("MouseUp", function(e) {
        nextBtn.alpha = 1;
        Crafty.scene('VsBoard');
    });  
        
    function arrowClick(){
        if(leftDown == true){
            leftDown = false;
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        if(rightDown == true){
            rightDown = false;
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
    }
    
    swipeHover.bind("MouseDown", function(e) {
        beginX = e.clientX;
    });
    swipeHover.bind("MouseUp", function(e) {
        arrowClick()
        nextBtn.alpha = 1;
        backBtn.alpha = 1;
        endX = e.clientX;
        if(beginX>endX && beginX != undefined){
            rightArrowMovement();
        }
        if(endX>beginX && beginX != undefined){
            leftArrowMovement();
        }
    });
    
     bg.bind("MouseUp", function(e) {
         nextBtn.alpha = 1;
         backBtn.alpha = 1;
         arrowClick()
    });
    
    
    rightArrow.bind("MouseDown", function(e) { 
        if(colorCount > 1){rightDown = false;}
        else{
            rightDown = true;
            rightArrow.alpha = 0.01;
            rightArrowHover.alpha = 1;
        }
    });
    
    leftArrow.bind("MouseDown", function(e) { 
        if(colorCount == 0){leftDown = false;}
        else{
            leftDown = true;
            leftArrow.alpha = 0.01;
            leftArrowHover.alpha = 1;
        }
    });
    
    rightArrow.bind("MouseUp", function(e) { 
        if(colorCount > 1){}
        else{
            rightArrow.alpha = 1;
            rightArrowHover.alpha = 0.01;
        }
        rightArrowMovement();
    });
    
    leftArrow.bind("MouseUp", function(e) { 
        if(colorCount == 0){}
        else{
            leftArrow.alpha = 1;
            leftArrowHover.alpha = 0.01;
        }
        leftArrowMovement();
    });
    
  
    function rightArrowMovement(){
        if(colorCount == 1){
            rightArrow.alpha = 0.01;
        }     
        if(colorCount<2){
            leftArrow.alpha = 1;
            selectTab.x = selectTab._x + 155; selectTab.y = bg._y+45;
            for(var i = 0;i < arrCar.length-1; i++) {
                arrColor[i].x = arrColor[i]._x-480; arrColor[i].y = bg._y + 130; 
            }
            colorCount++;
        }
        beginX = undefined;
    }
    
    
    function leftArrowMovement(){
        if(colorCount>0){
            rightArrow.alpha = 1;
            for(var i = 0;i < arrCar.length-1; i++) {
                arrColor[i].x = arrColor[i]._x+480; arrColor[i].y = bg._y + 130;
            }
            selectTab.x = selectTab._x - 155; selectTab.y = bg._y+45;
            colorCount--;
        }
        if(colorCount == 0){
            selectTab.x = bg._x + 5; selectTab.y = bg._y+45;
            leftArrow.alpha = 0.01;
        }
        beginX = undefined;
    }
 
        
});



Crafty.scene('AboutUs', function() {
    console.log("ABOUT US SCREEN");
    boardBack = false; 
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');

    var aboutBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/menu_back.png');
    aboutBg.x = bg._x+75; aboutBg.y = bg._y+20;
    
    var aboutTag = Crafty.e("2D, Canvas, Image, Mouse").image("images/aboutus1.png");
    aboutTag.x = aboutBg._x+100; aboutTag.y = aboutBg._y+7;
    
    var aboutText = Crafty.e("2D, Canvas, Image, Mouse").image('images/about_us_text.png');
    aboutText.x = aboutBg._x+27; aboutText.y = aboutBg._y+32;
    
    var backBtnHover = Crafty.e("2D, Canvas, Image, Mouse, BackBtn2");
    backBtnHover.x = bg._x+418;   backBtnHover.y = bg._y+225;   
    
    var backBtn = Crafty.e("2D, Canvas, Image, Mouse, BackBtn1");
    backBtn.x = bg._x+418;   backBtn.y = bg._y+225;    

    
    bg.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
    });
    aboutBg.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
    });
    aboutText.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
    });
    
    
    backBtn.bind("MouseDown", function(e) {
        backBtn.alpha = 0.01;
    });
    
    backBtn.bind("MouseUp", function(e) {
        backBtn.alpha = 1;
        boardBack = true;
        Crafty.scene('GameMenu');
    });

    
});


Crafty.scene('VsBoard', function() {
    console.log("VsBoard SCREEN");
    
    categoryCount1 = Crafty.math.randomInt(0,categoryCount);
    carCount1 = Crafty.math.randomInt(0,carCount);
    colorCount1 = colorCount +1;
    
    if(colorCount == 2){
        colorCount1 = 0;
    }

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

    var vsText = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/tap_msg.png');
    vsText.x = bg._x + 40;  vsText.y = bg._y + 90; 
    
    var userCar = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSmallHeader + "" +arrCar[categoryCount][carCount][colorCount]);
        userCar.x = bg._x + 45;  userCar.y = bg._y + 75; 
    
    var compCar = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSmallHeader + "" +arrCar[categoryCount1][carCount1][colorCount1]);
    compCar.x = bg._x + 285; compCar.y = bg._y + 75;
   
    
    var bgHover = Crafty.e("2D, Canvas, Color, Mouse").attr({w:480,h:280}).color('white');
    bgHover.x = bg.x;  bgHover.y = bg.y; 
    bgHover.alpha = 0.01;

    bgHover.bind("MouseDown", function(e) { 
        Crafty.scene('GamePlay');
    });
    
    
});



Crafty.scene('UserWin2', function() {
    console.log("UserWin2 SCREEN");
    var winText;
    Crafty.audio.stop();
    clearInterval(failedTimer); 
    
//    if(unlockScreen == 1){
//        unlockScreen = 0;
//        winRaceCount = 0;
//        console.log("winRaceCount = "+winRaceCount);
//    }
//    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var winBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    winBg.x = bg._x+50;   winBg.y = bg._y+40; 
    
    var winPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/winning.png');
    winPlayer.x = winBg._x;   winPlayer.y = winBg._y-5; 
    
    if(winRaceCount == 3){
        winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/won_tournament.png');
        winText.x = bg._x+160;   winText.y = bg._y+95; 
    }
    else{    
        winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/won.png');
        winText.x = bg._x+170;   winText.y = bg._y+95; 
    }
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = winBg._x;     mainmenuHover2.y = winBg._y+160;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = winBg._x;     mainmenuHover1.y = winBg._y+160;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = winBg._x+24;      mainmenuBtn.y = winBg._y+170;
    
    
    var nextBtnHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    nextBtnHover2.x = winBg._x+217;    nextBtnHover2.y = winBg._y+160; 
    
    var nextBtnHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    nextBtnHover1.x = winBg._x+217;    nextBtnHover1.y = winBg._y+160;
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/next2.png");
    nextBtn.x = winBg._x+267;      nextBtn.y = winBg._y+170;
    
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        nextBtnHover1.alpha = 1;
    });
    
    winBg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        nextBtnHover1.alpha = 1;
    });
    
    winPlayer.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        nextBtnHover1.alpha = 1;
    });
    
    winText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        nextBtnHover1.alpha = 1;
    });
    
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });

    
    
    nextBtn.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    nextBtnHover1.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    
    nextBtn.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        if(winRaceCount == 3 && pGameFinish == false){
            Crafty.scene("UnlockCarScreen");
        }
        else{
            Crafty.scene("Loading");
        }
    });
    
    nextBtnHover1.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        if(winRaceCount == 3 && pGameFinish == false){
            Crafty.scene("UnlockCarScreen");
        }
        else{
            Crafty.scene("Loading");
        }
    });
    
    playSfxAudio("GameWin");
    
});




Crafty.scene('UserWin1', function() {
    console.log("UserWin1 SCREEN");
    
    Crafty.audio.stop();
    clearInterval(failedTimer);  
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var winBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    winBg.x = bg._x+50;   winBg.y = bg._y+40; 
    
    var winPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/winning.png');
    winPlayer.x = winBg._x;   winPlayer.y = winBg._y-5; 
    
    var winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/won.png');
    winText.x = bg._x+170;   winText.y = bg._y+95; 
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = winBg._x+217;     mainmenuHover2.y = winBg._y+160;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = winBg._x+217;     mainmenuHover1.y = winBg._y+160;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = winBg._x+242;      mainmenuBtn.y = winBg._y+170;
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        resetGame();
        Crafty.scene("GameMenu");
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        resetGame();
        Crafty.scene("GameMenu");
    });
    
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });
    
    winBg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });
    
    winPlayer.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });
    
    winText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });
   
    playSfxAudio("GameWin");
    
});




Crafty.scene('UnlockCarScreen', function() {
	console.log("UnlockCarScreen");
    
    unlockCarCount += 1; 
    winRaceCount = 0;
    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/unlocked_newcar.png");
    winText.x = bg._x + 28; winText.y = bg._y + 40; 
    
    var  showCar = Crafty.e("2D, Canvas, Image, Mouse").image("images/unlockedCar/"+unlockCarId+".png");
    showCar.x = bg._x + 100; showCar.y = bg._y + 90; 
    
    var nextBtnHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    nextBtnHover2.x = bg._x+167;     nextBtnHover2.y = bg._y+205;
    
    var nextBtnHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    nextBtnHover1.x = bg._x+167;     nextBtnHover1.y = bg._y+205;
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/next2.png");
    nextBtn.x = bg._x+217;      nextBtn.y = bg._y+215;
    
    bg.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
    });
    
    showCar.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
    });
    
    nextBtn.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    nextBtnHover1.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    
    nextBtn.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
        Crafty.scene("Loading");
    });
    nextBtnHover1.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
        Crafty.scene("Loading");
    });

});


Crafty.scene('GameCompleted', function() {
	console.log("GameCompleted");
   
    unlockCarCount = 0;
    unlockCategoryCount = 0; 
    pGameFinish = true;
    winRaceCount = 0;
    winCount = 1;
    unlockCarId = 0;
    
    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/won_tournament.png");
    winText.x = bg._x + 120; winText.y = bg._y + 45; 
    
    var winText2 = Crafty.e("2D, Canvas, Image, Mouse").image("images/allcar_unlocked.png");
    winText2.x = bg._x + 15; winText2.y = bg._y + 120; 
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = bg._x+170;     mainmenuHover2.y = bg._y+180;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = bg._x+170;     mainmenuHover1.y = bg._y+180;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = bg._x+195;      mainmenuBtn.y = bg._y+190;
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        resetGame();
        Crafty.scene("GameMenu");
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        resetGame();
        Crafty.scene("GameMenu");
    });
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });
    
    winText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
    });

});


Crafty.scene('UnlockCategoryScreen', function() {
	console.log("UnlockCategoryScreen");
    unlockCarCount = 0;
    unlockCategoryCount += 1; 
    
    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/category_unlock.png");
    winText.x = bg._x + 80; winText.y = bg._y + 20; 
    
    var  showCar = Crafty.e("2D, Canvas, Image, Mouse").image("" + pathSelect + "" +arrCar[unlockCategoryId-1][0][0]);
    showCar.x = bg._x + 100; showCar.y = bg._y + 90; 
    
    var nextBtnHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    nextBtnHover2.x = bg._x+167;     nextBtnHover2.y = bg._y+205;
    
    var nextBtnHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    nextBtnHover1.x = bg._x+167;     nextBtnHover1.y = bg._y+205;
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/next2.png");
    nextBtn.x = bg._x+217;      nextBtn.y = bg._y+215;
    
    bg.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
    });
    
    showCar.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
    });
    
    nextBtn.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    nextBtnHover1.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    
    nextBtn.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
        resetGame();
        Crafty.scene("Loading");
    });
    nextBtnHover1.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
        resetGame();
        Crafty.scene("Loading");
    });

});


Crafty.scene('TournamentWin', function() {
    console.log("TournamentWin SCREEN");
    Crafty.audio.stop();
    clearInterval(failedTimer);  
    winCount = 1;
    winRaceCount = 0;
    unlockCarCount = 0; 
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var winBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    winBg.x = bg._x+50;   winBg.y = bg._y+40; 
    
    var winPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/winning.png');
    winPlayer.x = winBg._x;   winPlayer.y = winBg._y-5; 
    
    var winText = Crafty.e("2D, Canvas, Image, Mouse").image('images/won_tournament.png');
    winText.x = bg._x+160;   winText.y = bg._y+95; 
    
    var nextBtnHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    nextBtnHover2.x = winBg._x+217;     nextBtnHover2.y = winBg._y+160;
    
    var nextBtnHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    nextBtnHover1.x = winBg._x+217;     nextBtnHover1.y = winBg._y+160;
    
    var nextBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/next2.png");
    nextBtn.x = winBg._x+267;      nextBtn.y = winBg._y+170;
    
    
    
    nextBtn.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    nextBtnHover1.bind("MouseDown", function(e) { 
        nextBtnHover1.alpha = 0.01;
    });
    
    nextBtn.bind("MouseUp", function(e) { 
        nextBtnHover1.alpha = 1;
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        if(pGameFinish == false){
            Crafty.scene("UnlockCategoryScreen");
        }
        else{
            Crafty.scene("Loading");
        }
    });
    nextBtnHover1.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        if(pGameFinish == false){
            Crafty.scene("UnlockCategoryScreen");
        }
        else{
            Crafty.scene("Loading");
        }
    });
    
    bg.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
    });
    
    winBg.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
    });
    
    winPlayer.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
    });
    
    winText.bind("MouseUp", function(e) {
        nextBtnHover1.alpha = 1;
    });
        
   
    playSfxAudio("GameWin");
    
});



Crafty.scene('TournamentLost', function() {
    console.log("TournamentLost SCREEN");
 
    Crafty.audio.stop();
    clearInterval(failedTimer);   
    winRaceCount = 0;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var lostBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    lostBg.x = bg._x+50;   lostBg.y = bg._y+40; 
    
    var lostPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/lossing.png');
    lostPlayer.x = lostBg._x+290;   lostPlayer.y = lostBg._y-5; 
    
    var lostText = Crafty.e("2D, Canvas, Image, Mouse").image('images/lost_tournament.png');
    lostText.x = lostBg._x+5;   lostText.y = bg._y+70; 
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = lostBg._x;     mainmenuHover2.y = lostBg._y+160;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = lostBg._x;     mainmenuHover1.y = lostBg._y+160;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = lostBg._x+24;      mainmenuBtn.y = lostBg._y+170;
    
    var tryAgainHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    tryAgainHover2.x = lostBg._x+217;    tryAgainHover2.y = lostBg._y+160;
    
    var tryAgainHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    tryAgainHover1.x = lostBg._x+217;    tryAgainHover1.y = lostBg._y+160;
    
    var tryAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/try_again_button.png");
    tryAgainBtn.x = lostBg._x+242;      tryAgainBtn.y = lostBg._y+170;
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostBg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostPlayer.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    
    tryAgainBtn.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });
    
    tryAgainHover1.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });
    
    
    tryAgainBtn.bind("MouseUp", function(e) { 
        tryAgainHover1.alpha = 1;    
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    tryAgainHover1.bind("MouseUp", function(e) {
        tryAgainHover1.alpha = 1;    
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    playSfxAudio("GameLost");
    
});



Crafty.scene('GameLost', function() {
    console.log("GameLost SCREEN");

    Crafty.audio.stop();
    clearInterval(failedTimer);   
    winRaceCount = 0;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var lostBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    lostBg.x = bg._x+50;   lostBg.y = bg._y+40; 
    
    var lostPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/lossing.png');
    lostPlayer.x = lostBg._x+290;   lostPlayer.y = lostBg._y-5; 
    
    var lostText = Crafty.e("2D, Canvas, Image, Mouse").image('images/lost.png');
    lostText.x = lostBg._x+35;   lostText.y = bg._y+65; 
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = lostBg._x;     mainmenuHover2.y = lostBg._y+160;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = lostBg._x;     mainmenuHover1.y = lostBg._y+160;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = lostBg._x+24;      mainmenuBtn.y = lostBg._y+170;
    
    var tryAgainHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    tryAgainHover2.x = lostBg._x+217;    tryAgainHover2.y = lostBg._y+160;
    
    var tryAgainHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    tryAgainHover1.x = lostBg._x+217;    tryAgainHover1.y = lostBg._y+160;
    
    var tryAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/try_again_button.png");
    tryAgainBtn.x = lostBg._x+242;      tryAgainBtn.y = lostBg._y+170;
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostBg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostPlayer.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    
    tryAgainBtn.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });
    
    tryAgainHover1.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });

    
    tryAgainBtn.bind("MouseUp", function(e) { 
        tryAgainHover1.alpha = 1; 
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    tryAgainBtn.bind("MouseUp", function(e) {
        tryAgainHover1.alpha = 1; 
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    playSfxAudio("GameLost");
    
});




Crafty.scene('RaceFailed', function() {
    console.log("RaceFailed SCREEN");
   
    Crafty.audio.stop();
    clearInterval(failedTimer);
    winRaceCount = 0;
    
    var bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280}).image('images/menu_bg.png');
    
    var lostBg = Crafty.e("2D, Canvas, Image, Mouse").image('images/status_back.png');
    lostBg.x = bg._x+50;   lostBg.y = bg._y+40; 
    
    var lostPlayer = Crafty.e("2D, Canvas, Image, Mouse").image('images/lossing.png');
    lostPlayer.x = lostBg._x+290;   lostPlayer.y = lostBg._y-5; 
    
    var lostText = Crafty.e("2D, Canvas, Image, Mouse").image('images/lost.png');
    lostText.x = lostBg._x+35;   lostText.y = bg._y+65; 
    
    var mainmenuHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    mainmenuHover2.x = lostBg._x;     mainmenuHover2.y = lostBg._y+160;
    
    var mainmenuHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    mainmenuHover1.x = lostBg._x;     mainmenuHover1.y = lostBg._y+160;
    
    var mainmenuBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/main_menu_button.png");
    mainmenuBtn.x = lostBg._x+24;      mainmenuBtn.y = lostBg._y+170;
    
    var tryAgainHover2 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack2");
    tryAgainHover2.x = lostBg._x+217;    tryAgainHover2.y = lostBg._y+160;
    
    var tryAgainHover1 = Crafty.e("2D, Canvas, Image, Mouse, PauseBack1");
    tryAgainHover1.x = lostBg._x+217;    tryAgainHover1.y = lostBg._y+160;
    
    var tryAgainBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/try_again_button.png");
    tryAgainBtn.x = lostBg._x+242;      tryAgainBtn.y = lostBg._y+170;
    
    
    
    mainmenuBtn.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    mainmenuHover1.bind("MouseDown", function(e) { 
        mainmenuHover1.alpha = 0.01;
    });
    
    mainmenuBtn.bind("MouseUp", function(e) { 
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    mainmenuHover1.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        createExitScreen();
    });
    
    bg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostBg.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostPlayer.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    lostText.bind("MouseUp", function(e) {
        mainmenuHover1.alpha = 1;
        tryAgainHover1.alpha = 1;    
    });
    
    
    tryAgainBtn.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });
    
    tryAgainHover1.bind("MouseDown", function(e) { 
        tryAgainHover1.alpha = 0.01;    
    });


    tryAgainBtn.bind("MouseUp", function(e) { 
        tryAgainHover1.alpha = 1;    
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    tryAgainBtn.bind("MouseUp", function(e) {
        tryAgainHover1.alpha = 1;    
        resetGame();
        if(soundDown == false){
            Crafty.audio.stop();
            Crafty.audio.play("BgmMenu", -1)
        }
        Crafty.scene("Loading");
    });
    
    playSfxAudio("GameLost");
    
    
});

