   var k = 1;    
var lGameMode;
var nosTimeout;
var nosBarTimer;
var failedTimer;
var startGirl, startLine, startText;
var userwheelmoveX1, userwheelmoveX2; 
var computerWheel1, computerWheel2, computerCar, compCollisionLine;
var SpeedTimer, SpeedTimerUp, SpeedTimerDown, animationTimer, compCarTimer;
var dashBoard, accelerator2, accelerator1, nos2, nos1, arrBar, nosBarX, nosBar, arrBarX
var bg, finishLine, finishLine1, playerCar, userCollisionLine, playerWheel1, playerWheel2;
var arrBg = new Array();
var arrBar = new Array();
var arrBarX = new Array();

var barNo = 0;        
var bgTime = 40;            //  time for track background movement
var bgSpeed = 35;           //  track background speed
var needleNo = 0;   
var bgMoveX = 100;          //  track movemnt in x direction
var compshiftY = 0; 
var compshiftY = 0; 
var usershiftY = 0; 
var userFinish = 0;
var compFinish = 0;
var wheelSpeed = 10;        //  wheel animation speed
var compshiftX1 = 0; 
var compshiftX2 = 0; 
var usershiftX1 = 0; 
var usershiftX2 = 0; 
var nosBarIndex = 7;
var userCarSpeed = 60;
var compCarSpeed = 10;
var failedCounter = 0; 
var compCarMoveX = 60;
var userRaceCompleted = 0;
var compRaceCompleted = 0;
var userwheelX1 = [[1, 3, 4], [8, 7, 8], [7, 9, 6], [7, 6, 9]];      //  wheel placement
var userwheelX2 = [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 1, 0]];      //  wheel placement
var userwheelY  = [[1, 1, 2], [0, 0, 0], [1, 1, 0], [0, 2, 1]];      //  wheel placement

var raceBegin = true;
var compCarFinish = false;
var countDownBegin = false;         //  used for starting countDown before accelerator push 
var aceeleratorDown = false;        //  used for push on accelerator
var aceeleratorDownAgain = false;


//document.addEventListener("visibilitychange", function () {
//    if(deviceButton == true){
//        if (document.hidden) {
//            aceeleratorDown = false;
//            clearInterval(SpeedTimer);
//            compCarMoveX = 0;
//            clearInterval(animationTimer);
//            clearInterval(SpeedTimerUp);
//            clearInterval(SpeedTimerDown);   
//            clearInterval(nosBarTimer);   
//            clearInterval(compCarTimer);
//            clearInterval(failedTimer); 
//            playerWheel1.animate('stop', wheelSpeed, 0) 
//            playerWheel2.animate('stop', wheelSpeed, 0)
//            Crafty.audio.stop();
//            alert("kush");
//        }
//        else {
//            alert("bharadwaj");
//            compCarMoveX = 30;
//            if(compRaceCompleted == 1){
//                pausebackGame();
//            }
//            else{
//                compCarBeginMove();
//            }
//            resumeRace();
//        }
//    }
//});




function resetGame(){
    clearInterval(SpeedTimer);
    clearInterval(nosTimeout);
    clearInterval(nosBarTimer); 
    clearInterval(failedTimer); 
    clearInterval(SpeedTimerUp);
    clearInterval(compCarTimer);   
    clearInterval(SpeedTimerDown);   
    clearInterval(animationTimer);
    raceBegin = true;
    compCarFinish = false;
    countDownBegin = false;
    aceeleratorDown = false;
    aceeleratorDownAgain = false;
    
    barNo = 0;
    bgTime = 40; 
    bgSpeed = 35;
    needleNo = 0;
    carCount = 0;
    bgMoveX = 100;   
    carCount1 = 0;
    colorCount = 0;
    usershiftY = 0; 
    userFinish = 0;
    compFinish = 0;
    compshiftY = 0; 
    compshiftY = 0; 
    wheelSpeed = 10;
    colorCount1 = 0;
    usershiftX1 = 0; 
    usershiftX2 = 0; 
    compshiftX1 = 0; 
    compshiftX2 = 0; 
    nosBarIndex = 7;
    userCarSpeed = 60;
    compCarSpeed = 10;
    compCarMoveX = 60;
    categoryCount = 0;
    failedCounter = 0;
    categoryCount1 = 0;
    userwheelmoveX1 = 0;
    userwheelmoveX2 = 0; 
    userRaceCompleted = 0;
    compRaceCompleted = 0;
  
}


function setGameMode(pGameMode){
	if(pGameMode) {
		lGameMode = pGameMode;
		console.log("set game mode ", lGameMode);
		if(pGameMode == "Tournament") {
		}
	}
}

function getGameMode() {
	return lGameMode;
}


//   start computer car movement
function compCarBeginMove(){
    computerWheel1.animate('move', wheelSpeed, -1)    //  start computer wheels animation
    computerWheel2.animate('move', wheelSpeed, -1)    //  start computer wheels animation
        
    compCarTimer = setInterval(function(e) {
        computerCar.tween({x:computerCar._x+compCarMoveX}, compCarSpeed);
        compCollisionLine.tween({x:compCollisionLine._x+compCarMoveX}, compCarSpeed);
        computerWheel1.tween({x:computerWheel1._x+compCarMoveX}, compCarSpeed);
        computerWheel2.tween({x:computerWheel2._x+compCarMoveX}, compCarSpeed);
    },100);
}



Crafty.scene('GamePlay', function() {
    console.log("GAME PLAY SCREEN");
    Crafty.audio.stop();
        
    var nosDown = false;
    var nosUp = false;
    
    //   used for placing user car tyres at exact place 
    usershiftX1 = userwheelX1[categoryCount][carCount];
    usershiftX2 = userwheelX2[categoryCount][carCount];
    usershiftY = userwheelY[categoryCount][carCount];
        
    //   used for placing computer car tyres at exact place
    compshiftX1 = userwheelX1[categoryCount1][carCount1];
    compshiftX2 = userwheelX2[categoryCount1][carCount1];
    compshiftY = userwheelY[categoryCount1][carCount1];
    
    
    bg = Crafty.e("2D, Canvas, Image, Mouse").attr({x:0,y:0,w:480,h:280})
    .image("images/trackBg"+trackNo+"/gameBg1.png");

    //  array for track background
    arrBg = [Crafty.e("BG11, Tween"), Crafty.e("BG12, Tween"), Crafty.e("BG13, Tween")];
    
    
    // place the track bg
    for(var i = 0; i<arrBg.length; i++){
        arrBg[i].x = bg._x +(i*480);   arrBg[i].y = 0;   
    }
    
    
    //  placed start Line
    startLine = Crafty.e("2D, Canvas, Image, Mouse, Tween").image("images/startLine.png");
    startLine.x =  bg._x + 360;    startLine.y = bg._y + 65;
    
    //  fake finish line
    finishLine = Crafty.e("EndLine")
    finishLine.x =  bg._x + 10050;    finishLine.y = bg._y + 65;

    //  race finish line for identify winner
    finishLine1 = Crafty.e("EndLine1")
    finishLine1.x =  bg._x + 10200;    finishLine1.y = bg._y + 65;
    finishLine1.alpha = 0.01;        
    
    //  computer car
    computerCar = Crafty.e("2D, Canvas, Image, Mouse, Tween")
    .image("" + pathIngame + "" +arrCar[categoryCount1][carCount1][colorCount1]);
    computerCar.x = bg._x + 120;  computerCar.y = bg._y + 65; 
    
    //  user car
    playerCar = Crafty.e("2D, Canvas, Image, Mouse, Tween")
    .image("" + pathIngame + "" +arrCar[categoryCount][carCount][colorCount]);
    playerCar.x = bg._x + 40; playerCar.y = bg._y + 130;     
    
    
    //  line placed on user car for detect collision
    userCollisionLine = Crafty.e("2D, Canvas, Color, Mouse, Tween, Collision").attr({w: 2, h:50}).color("red");
    userCollisionLine.x = playerCar._x + 135; userCollisionLine.y = playerCar._y; 
    userCollisionLine.alpha = 0.01;
    
    
    //  line placed on computer car for detect collision
    compCollisionLine = Crafty.e("2D, Canvas, Color, Mouse, Tween, Collision").attr({w: 2, h:50}).color("red");
    compCollisionLine.x = computerCar._x + 135; compCollisionLine.y = computerCar._y; 
    compCollisionLine.alpha = 0.01;
    
 
    // user car wheels placement
    playerWheel1 = Crafty.e("2D, Canvas, Tween, SpriteAnimation, Wheel");
    playerWheel1.x = playerCar._x + 14 + usershiftX1;    playerWheel1.y = playerCar._y + 31 - usershiftY; 

    playerWheel2 = Crafty.e("2D, Canvas, Tween, SpriteAnimation, Wheel");
    playerWheel2.x = playerCar._x + 104 + usershiftX2;    playerWheel2.y = playerCar._y + 31 - usershiftY; 
            
    userwheelmoveX1 = playerWheel1.x;
    userwheelmoveX2 = playerWheel2.x;
    
    
    playerWheel1.animate('move', 0, 0, 1)       //  setup player wheels animation
    playerWheel1.animate('stop', 0, 0, 0)       //  setup player wheels animation
    
    playerWheel2.animate('move', 0, 0, 1)       //  setup player wheels animation
    playerWheel2.animate('stop', 0, 0, 0)       //  setup player wheels animation
   
    
    // computer car wheels placement
    computerWheel1 = Crafty.e("2D, Canvas, Tween, SpriteAnimation, Wheel");
    computerWheel1.x = computerCar._x + 14 + compshiftX1;    computerWheel1.y = computerCar._y + 31 - compshiftY; 

    computerWheel2 = Crafty.e("2D, Canvas, Tween, SpriteAnimation, Wheel");
    computerWheel2.x = computerCar._x + 104 + compshiftX2;    computerWheel2.y = computerCar._y + 31 - compshiftY; 
             
    computerWheel1.animate('move', 0, 0, 1)      //   setup computer wheel1 animation
    computerWheel1.animate('stop', 0, 0, 0)      //   setup computer wheel1 animation
    
    computerWheel2.animate('move', 0, 0, 1)      //   setup computer wheel2 animation
    computerWheel2.animate('stop', 0, 0, 0)      //   setup computer wheel2 animation
    
    
    var hideBg = Crafty.e("2D, Canvas, Color, Mouse").attr({x:0,y:0,w:480,h:280}).color("white");
    hideBg.alpha = 0.01;    
    
    
    //  show speedometer & nos changes
    dashBoard = Crafty.e("2D, Canvas, Image, Mouse").image("images/dashboard.png");
    dashBoard.x =  bg._x + 175;    dashBoard.y = bg._y + 215;   
        
    //  user car accelerator
    accelerator2 = Crafty.e("2D, Canvas, Mouse, Tween, SpriteAnimation, Accelerator2");
    accelerator2.x = bg._x + 410;    accelerator2.y = bg._y + 205;
    accelerator2.alpha = 0.01;
        
    accelerator1 = Crafty.e("2D, Canvas, Mouse, Tween, SpriteAnimation, Accelerator1");
    accelerator1.x = bg._x + 410;    accelerator1.y = bg._y + 205;     
        
    //  user car NOS
    nos2 = Crafty.e("2D, Canvas, Mouse, Tween, SpriteAnimation, Nos2");
    nos2.x = bg._x + 90;    nos2.y = bg._y + 205;
    nos2.alpha = 0.01;
        
    nos1 = Crafty.e("2D, Canvas, Mouse, Tween, SpriteAnimation, Nos1");
    nos1.x = bg._x + 90;    nos1.y = bg._y + 205;          
        
    
    
    //  meter needle images array
    meterNeedle = [Crafty.e("2D, Canvas, SpriteAnimation, Needle0"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle1"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle2"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle3"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle4"),
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle5"),
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle6"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle7"), 
                   Crafty.e("2D, Canvas, SpriteAnimation, Needle8")];
    
    
    //  accelerator bar array 
    arrBar = [Crafty.e("2D, Canvas, Bar1"), 
              Crafty.e("2D, Canvas, Bar1"), 
              Crafty.e("2D, Canvas, Bar1"), 
              Crafty.e("2D, Canvas, Bar1"), 
              Crafty.e("2D, Canvas, Bar1"),
              Crafty.e("2D, Canvas, Bar1"),
              Crafty.e("2D, Canvas, Bar1"),
              Crafty.e("2D, Canvas, Bar1"),];
                     
    arrBarX = dashBoard._x +96; 
    
    //  nos bar Array
    nosBar = [Crafty.e("2D, Canvas, NosBar"), 
              Crafty.e("2D, Canvas, NosBar"), 
              Crafty.e("2D, Canvas, NosBar"), 
              Crafty.e("2D, Canvas, NosBar"), 
              Crafty.e("2D, Canvas, NosBar"),
              Crafty.e("2D, Canvas, NosBar"),
              Crafty.e("2D, Canvas, NosBar"),
              Crafty.e("2D, Canvas, NosBar")];
                     
    nosBarX = dashBoard._x +96; 
    
    
    //  place speed meter needle
    for(var i = 0; i<meterNeedle.length; i++){
        meterNeedle[i].x = dashBoard._x + 57;   meterNeedle[i].y = dashBoard._y + 22;   
    }
    
    //  place accelerator meter bar
    for(var i = 0; i<arrBar.length; i++){
        arrBar[i].x = arrBarX;   arrBar[i].y = dashBoard._y + 12; 
        arrBar[i].alpha = 0.01;
        arrBarX += 6;
    }
    
    //  place nos meter bar
     for(var i = 0; i<nosBar.length; i++){
        nosBar[i].x = nosBarX;   nosBar[i].y = dashBoard._y + 37; 
        nosBarX += 6;
    }
        
    meterNeedleMovement();


    //  execute when click on aceelerator paddle
    accelerator1.bind("MouseDown", function(e) { 
        if(countDownBegin == true){
            if(aceeleratorDown == false){
                accelerator2.alpha = 1;
                accelerator1.alpha = 0.01;
                arrBar[0].alpha = 1;
                if(aceeleratorDownAgain == true){
                    aceeleratorDownAgain = false;
                    clearInterval(animationTimer);
                    clearInterval(SpeedTimer);
                    clearInterval(SpeedTimerUp);
                    clearInterval(SpeedTimerDown);                
                }
                beginTrackSpeed();
                userCarBeginMove();
                playerWheel1.animate('move', wheelSpeed, -1)     //  start player wheels animation
                playerWheel2.animate('move', wheelSpeed, -1)     //  start player wheels animation
                
                increaseTrackSpeed();
                aceeleratorDown = true;
            }
        }
    }); 
   
    
    accelerator1.bind("MouseUp", function(e) { 
        if(aceeleratorDown == true){
            accelerator2.alpha = 0.01;
            accelerator1.alpha = 1;
            clearInterval(SpeedTimerUp);
            decreaseTrackSpeed();
            aceeleratorDown = false;
            aceeleratorDownAgain = true;
            nosDown = true;
        }
    }); 
    
    
        
    
    //  execute when click on NOS btn
    nos1.bind("MouseDown", function(e) { 
        if(nosDown == true){
            nos2.alpha = 1;
            nos1.alpha = 0.01;
            pauseBtn.alpha = 0.01;
            clearInterval(SpeedTimer)
            clearInterval(SpeedTimerUp)
            clearInterval(SpeedTimerDown)
            for(var i = 0; i<arrBg.length; i++){
                arrBg[i].x = bg._x+(i*480);  
                arrBg[i].y = 0;   
            }
            bgSpeed = 4;
            bgTime = 40;
            bgMoveX = 100;
            compCarMoveX = -20;
            beginTrackSpeed();
            nosUp = true;
            countDownBegin = false;
            nosBarRemoval();
            nos1.unbind("MouseDown", this.nos1);
            accelerator2.alpha = 0.01;
            accelerator1.alpha = 0.01;
            
            nosTimeout = setTimeout(function(){
                countDownBegin = true;
                nosUp = false;
                nos1.unbind("MouseUp", this.nos1);
                nos1.alpha = 0.01;
                nos2.alpha = 0.01;
                accelerator1.alpha = 1;
                if(userRaceCompleted == 0){
                    pauseBtn.alpha = 1;
                }
                compCarMoveX = 20;
                decreaseTrackSpeed();
            }, 5000);
        }
    });
    
    nos1.bind("MouseUp", function(e) { 
        if(nosUp == true){
            nos1.alpha = 0.01;
            nos2.alpha = 0.01;
            nos1.unbind("MouseDown", this.nos1);
            nos1.unbind("MouseUp", this.nos1);
            
        }
    });
    
   
    //  move player car to center of track
    function userCarBeginMove(){
        playerCar.tween({x:bg._x+215}, userCarSpeed);
        userCollisionLine.tween({x:bg._x+350}, userCarSpeed);
        playerWheel1.tween({x:userwheelmoveX1+175}, userCarSpeed);
        playerWheel2.tween({x:userwheelmoveX2+175}, userCarSpeed);
    }

    
    //  placed start Girl
    startGirl = Crafty.e("2D, Canvas, Image, Mouse, Tween").image("images/start_girl.png");
    startGirl.x =  bg._x + 300;    startGirl.y = bg._y + 67;
    
    
    //  placed countdown text
    startText = Crafty.e("2D, Canvas, Start, SpriteAnimation");
    startText.x =  bg._x + 180;    startText.y = bg._y + 70;
    
    startText.animate('count', 0, 0, 11)   //   setup countDown animation
    startText.animate('count', 150, 0)     //   start countDown animation
    
    
    
    //  timeout used for destroy countDown text
    setTimeout(function(){
        startText.destroy();
        countDownBegin = true;
        pauseBtn.alpha = 1;
        console.log("===== CountDown =====");
        compCarBeginMove();
    }, 4000);
   
    
    
        
    //  placed pause button
    var pauseBtnHover = Crafty.e("2D, Canvas, Image, Mouse, PauseBtn2");
    pauseBtnHover.x =  bg._x + 10;    pauseBtnHover.y = bg._y + 227; 
    pauseBtnHover.alpha = 0.01;
    
    var pauseBtn = Crafty.e("2D, Canvas, Image, Mouse, PauseBtn1");
    pauseBtn.x =  bg._x + 10;    pauseBtn.y = bg._y + 227; 
    pauseBtn.alpha = 0.01;
    
    
    hideBg.bind("MouseMove", function(e) { 
        if(countDownBegin == true && nosUp == false){
            pauseBtnHover.alpha = 0.01;
            pauseBtn.alpha = 1;
        }
        if(aceeleratorDown == true && nosUp == false){
            accelerator2.alpha = 0.01;
            accelerator1.alpha = 1;
            clearInterval(SpeedTimerUp);
            decreaseTrackSpeed();
            aceeleratorDown = false;
            aceeleratorDownAgain = true;
            nosDown = true;
        }
        if(nosUp == true){
            nos1.alpha = 0.01;
            nos2.alpha = 0.01;
        }
    });
    
    hideBg.bind("MouseUp", function(e) { 
        if(countDownBegin == true && nosUp == false){
            pauseBtnHover.alpha = 0.01;
            pauseBtn.alpha = 1;
        }
        if(aceeleratorDown == true && nosUp == false){
            accelerator2.alpha = 0.01;
            accelerator1.alpha = 1;
            clearInterval(SpeedTimerUp);
            decreaseTrackSpeed();
            aceeleratorDown = false;
            aceeleratorDownAgain = true;
            nosDown = true;
        }
        if(nosUp == true){
            nos1.alpha = 0.01;
            nos2.alpha = 0.01;
        }
    });
    
    
    
    //  execute when click on pause btn  
    pauseBtn.bind("MouseDown", function(e) {
        if(countDownBegin == true && nosUp == false){
            pauseBtnHover.alpha = 1;
            pauseBtn.alpha = 0.01;
        }
    });
    
    pauseBtn.bind("MouseUp", function(e) { 
        if(countDownBegin == true && nosUp == false){
            pauseBtnHover.alpha = 0.01;
            pauseBtn.alpha = 1;
            accelerator2.alpha = 0.01;
            accelerator1.alpha = 1;
            aceeleratorDown = false;
            clearInterval(SpeedTimer);
            compCarMoveX = 0;
            if(soundDown == false){
                Crafty.audio.stop();
                Crafty.audio.play("BgmMenu", -1)
            }
            clearInterval(animationTimer);
            clearInterval(SpeedTimerUp);
            clearInterval(SpeedTimerDown);   
            clearInterval(nosBarTimer);   
            clearInterval(compCarTimer);
            clearInterval(failedTimer); 
            playerWheel1.animate('stop', wheelSpeed, 0) 
            playerWheel2.animate('stop', wheelSpeed, 0)

            createPauseScreen();
        }
    });
    
    
    userCollisionLine.onHit("EndLine", function(e) {
       if(compRaceCompleted == 1){
       //   countDownBegin = false;
           nosUp = true;
       } 
        pauseBtn.alpha = 0.01;
        pauseBtnHover.alpha = 0.01;
    });
    
    
    //  user car collision with finish line
    userCollisionLine.onHit("EndLine1", function(e) {
        userCollisionLine.destroy();
        nos1.unbind("MouseDown", this.nos1);
        nos1.unbind("MouseUp", this.nos1);
        nos1.alpha = 0.01;
        nos2.alpha = 0.01;
        if(compFinish == 1){
            userFinish = 0;
        }
        else{
            userFinish = 1;
        }
        userRaceCompleted = 1;
        clearInterval(animationTimer);
        clearInterval(SpeedTimerUp);
        clearInterval(SpeedTimerDown);   
        clearInterval(SpeedTimer);   
        clearInterval(nosBarTimer);   
        clearInterval(nosTimeout);
        setTimeout(function(){
            playerWheel1.animate('stop', wheelSpeed, 0)      //   stop player wheels animation
            playerWheel2.animate('stop', wheelSpeed, 0)      //   stop player wheels animation
            
            for(var i = 0; i<arrBar.length; i++){
                arrBar[i].alpha = 0.01;
            }
            for(var i = 0; i<nosBar.length; i++){
                nosBar[i].alpha = 0.01;
            }
            nosBarIndex = 0;
            barNo = 0;
            needleNo = 0;
            bgSpeed = 35;
            meterNeedleMovement();
            if(compRaceCompleted == 1){
                gameWinner();
            }
        }, 50);
            
        clearInterval(SpeedTimer);
        if(compRaceCompleted == 1){}
        countDownBegin = false;
        compCarFinish = false;
        compCarMoveX = 30;
        
    });
    
    
    //  computer car collision with finish line
    compCollisionLine.onHit("EndLine1", function(e) {
        compCollisionLine.destroy();
        if(userFinish == 1){
            compFinish = 0;
        }
        else{
            compFinish = 1;
        }
        compRaceCompleted = 1;
        clearInterval(compCarTimer);
       
        setTimeout(function(){
            computerWheel1.x = computerCar._x + 14 + compshiftX1;  
            computerWheel2.x = computerCar._x + 104 + compshiftX2; 
        }, 1000);
      
        setTimeout(function(){
            computerWheel1.animate('stop', wheelSpeed, 0)      //   stop player wheels animation
            computerWheel2.animate('stop', wheelSpeed, 0)      //   stop player wheels animation
            if(userRaceCompleted == 1){
                gameWinner();
            }
        }, 50);
        
        compCarFinish = true;
        pausebackGame();
            
    });

    
    //  check race winning condition
    function gameWinner(){
        if(userRaceCompleted == 1 && compRaceCompleted == 1){
            pauseBtn.alpha = 0.01;
            nosUp = true;
            clearInterval(animationTimer);
            clearInterval(SpeedTimer);
            clearInterval(SpeedTimerUp);
            clearInterval(SpeedTimerDown);
            clearInterval(failedTimer);
            if(userFinish == 1){
                setTimeout(function(e){
                    if(gameMode == 2){
                        winRaceCount += 1;
                        console.log("unlockCarId = "+unlockCarId);
                        if(pGameFinish == false){
                            if(winRaceCount == 3){
                                unlockCarId += 1;
                                console.log("if unlockCarId = "+unlockCarId);
                                setStorageItem("Car"+unlockCarId, 1)
                                k = unlockCarId-1;
                                console.log("k = "+k);
                                setStorageItem("CarUnlock", k)
                            }
                            if(k%3==0){
                                k = 1;
                                unlockCategoryId++;
                                setStorageItem("Car"+unlockCarId, 1)
                                setStorageItem("Tournament"+unlockCategoryId, 1)
                                console.log("unlockCategoryId = "+unlockCategoryId);
                                if(unlockCategoryId>4){
                                    console.log("unlockCategoryId > 4");
                                    setStorageItem("GameUnlock", 1)
                                    Crafty.scene("GameCompleted");
                                }
                                else{
                                    Crafty.scene("TournamentWin");
                                }
                            }
                            else{
                                Crafty.scene("UserWin2");
                            }
                        }
                        else{
                            if(winRaceCount == 3){
                                console.log(" pGameFinish = true executed");
                                Crafty.scene("TournamentWin");
                            }
                            else{
                                Crafty.scene("UserWin2");
                            }
                        }
                    }
                    else{
                        Crafty.scene("UserWin1");
                    }
                }, 1000);
            }
            if(compFinish == 1){
                if(gameMode == 1){
                    setTimeout(function(e){
                        Crafty.scene("GameLost");
                    }, 1000);
                }
                else{
                    setTimeout(function(e){
                        Crafty.scene("TournamentLost");
                    }, 1000);
                }
            }   
        }
    }

    playBgMusic("GameMusic")

});


//  check race failed 
function pausebackGame(){
    failedTimer = setInterval(function(){
        failedCounter++;
        if(compRaceCompleted == 1 && userRaceCompleted == 0 && failedCounter>=30){
            countDownBegin = false;
            clearInterval(failedTimer);
            Crafty.scene("RaceFailed");
        }
    }, 1000);
}

//  continue race after click on resume btn
function resumeRace(){
    if(raceBegin == false && bgSpeed<35){
        beginTrackSpeed();
        decreaseTrackSpeed();
        playerWheel1.animate('move', wheelSpeed, -1)     
        playerWheel2.animate('move', wheelSpeed, -1)    
    }
}


//  function for start track movement
function beginTrackSpeed(){
    if(raceBegin == true){
        startLine.tween({x:bg._x-bgMoveX}, bgSpeed+35);
        startGirl.tween({x:bg._x-bgMoveX}, bgSpeed+25);
        raceBegin = false;
    }
    var positionX = arrBg.length*480;
    SpeedTimer = setInterval(function(e) {
        for(var i = 0;  i < arrBg.length; i++){
            arrBg[i].tween({x:arrBg[i]._x-bgMoveX}, bgSpeed);
            finishLine.tween({x:finishLine._x-bgMoveX}, bgSpeed);
            finishLine1.tween({x:finishLine1._x-bgMoveX}, bgSpeed);
            if(compCarFinish == true){
                computerCar.tween({x:computerCar._x - bgMoveX}, bgSpeed);
                compCollisionLine.tween({x:compCollisionLine._x - bgMoveX}, bgSpeed);
                computerWheel1.tween({x:computerWheel1._x - bgMoveX}, bgSpeed);
                computerWheel2.tween({x:computerWheel2._x - bgMoveX}, bgSpeed);
            }
            if(arrBg[i].x <= bg._x-500){
                startGirl.destroy();
                startLine.destroy();
                arrBg[i].x = arrBg[i]._x + positionX;
            }
        }
    },bgTime);
}
    
    
//   function for increase track speed
function increaseTrackSpeed(){
    SpeedTimerUp = setInterval(function(e) { 
        if(bgSpeed <= 5){
            bgSpeed = 5;
            needleNo = 8;
        }
        else{
            bgSpeed -= 5;                 
            needleNo += 1;
        }
        compCarMoveX = -10;
        compCarSpeed = 5;
        acceleratorBarUp();
        meterNeedleMovement();
    },1000);
}
    
    
//   function for decrease track speed
function decreaseTrackSpeed(){
    SpeedTimerDown = setInterval(function(e) { 
        if(bgSpeed > 35){
            bgSpeed = 35;
            needleNo = 1;
        }
        else{
            bgSpeed += 5;                 
            needleNo -= 1;
        }
        if(needleNo < 1){
            needleNo = 1;
        }
        wheelSpeed = 10;
        if(bgSpeed >= 35){
            wheelSpeed = 10;
            bgSpeed = 35;
                
            animationTimer = setTimeout(function(){
                needleNo = 0;
                playerWheel1.animate('move', wheelSpeed, 0)      //   stop player wheels animation
                playerWheel2.animate('move', wheelSpeed, 0)      //   stop player wheels animation
                meterNeedleMovement();
                for(var i=0; i<arrBar.length; i++){
                    arrBar[i].alpha = 0.01;
                }
            },450);
            clearInterval(SpeedTimer);
            clearInterval(SpeedTimerUp);
            clearInterval(SpeedTimerDown);
        }
        compCarMoveX = 30;
        acceleratorBarDown();
        meterNeedleMovement();
    },1000);
}
    

//  change NOS bar position on speedometer  
function nosBarRemoval(){
    nosBarTimer = setInterval(function(){
        needleNo += 1;
        if(needleNo >7){
            needleNo = 8;
        }
        meterNeedleMovement();
        acceleratorBarUp();
        nosBar[nosBarIndex].destroy();
        nosBarIndex -= 1;
        if(nosBarIndex < 0){
            clearInterval(nosBarTimer)
        }
    }, 625);
}
    
    
//  change in meter needle alpha position
function meterNeedleMovement(){
    for(var i = 0; i<meterNeedle.length; i++){
        meterNeedle[i].alpha = 0.01;   
    }
    meterNeedle[needleNo].alpha = 1;
}
    

//  increase the accelerator bar
function acceleratorBarUp(){
    arrBar[barNo].alpha = 1;
    barNo += 1;
    if(barNo > 7){
        barNo = 7;
    }
}
    
//  decrease the accelerator bar
function acceleratorBarDown(){
    arrBar[barNo].alpha = 0.01;
    barNo -= 1;
    if(barNo < 0){
        barNo = 0;
    }
}

