
var blackSpeed,whiteSpeed,brownSpeed;
var rankFlag = false;
var isPaused ;
var bgArray = new Array();
var switchPositionX, myVart, myVar;
var count;
var iLen = 0;
var countWenPause = 0;
var finishLine, bg, imageDisp, positionTop;
var imgRetain, beginRace, startLine1;
var raceCompleted = false;
var pauseBtn, bg1;

function resetGame(){
    clearInterval(myVar)
    clearInterval(myVart)
    rankFlag = false;
    iLen = 0;
    countWenPause = 0;
    raceCompleted = false;
}

function rankDisp(imageDisp){
        positionTop = Crafty.e("2D, Canvas, Image, Mouse").attr({x:320,y:10,w:179,h:24}).image(imageDisp);
        rankFlag = true;
}


function bgMovement(){
    switchPositionX = bgArray.length*480;
    myVart = setInterval(function() {
        for(iLen = 0; iLen < bgArray.length ; iLen++){
            bgArray[iLen].tween({ x: bgArray[iLen]._x -100}, 10);
            finishLine.tween({x:finishLine._x - 100}, 10);
     
            if(bgArray[iLen].x <= bg._x-500){
                countWenPause = countWenPause + 1;
                bgArray[iLen].x = bgArray[iLen].x+switchPositionX;
            }
        }
        if(finishLine.x <= bg._x+220){
            clearInterval(myVart)
            BrownHorse.animate('stop', 0, 0);
            WhiteHorse.animate('stop', 0, 0);
            BlackHorse.animate('stop', 0, 0);
            
            clearInterval(myVar);
            rankDisp(imageDisp);
            stillHorsesFunc();
            Crafty.audio.stop();
        }
    },40);
}


function beginRaceFunction(){
    BlackHorse.gravity = true;
    BlackHorse.up = false;
    BlackHorse.rowLevel=0;
    BlackHorse.animate('animate', 1, -1);
    
    WhiteHorse.gravity = true;
    WhiteHorse.up = false;
    WhiteHorse.rowLevel=0;
    WhiteHorse.animate('animate', 1, -1);
          
    BrownHorse.gravity = true;
    BrownHorse.up = false;
    BrownHorse.rowLevel=0;
    BrownHorse.animate('animate', 1, -1);
}


Crafty.scene('GamePlay', function() {
    console.log("GAME PLAY SCREEN");

    blackSpeed = 90;
    whiteSpeed = 86;
    brownSpeed = 70;

    bg = Crafty.e("2D, Canvas, Image").attr({x:0, y:0, w:480, h:280}).image("images/gameBg.png");
    
    bgArray = [Crafty.e("BG11, Tween"), Crafty.e("BG12, Tween"), Crafty.e("BG13, Tween")];
    
    finishLine = Crafty.e("2D, Canvas, Image, Tween").image("images/finish_line.png");
    finishLine.x = bg._x+3500;      finishLine.y = bg._y+130 ;

    var nextX = bg.x;

    for(var i = 0; i < bgArray.length ; i++){
        bgArray[i].x = nextX; bgArray[i].y = bg.y;
        nextX += 480;
    }

    
    bg1 = Crafty.e("BG14").attr({x:0, y:0, w:480, h:280});
    bg1.alpha = 0.01;
    
    
    if(selectedHorse == "whiteHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/white_ horse_border.png");
        horseSelect.x = bg._x+10; horseSelect.y = bg._y+10;
    }
    else if(selectedHorse == "blackHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/black_horse_border.png");
        horseSelect.x = bg._x+10; horseSelect.y = bg._y+10;
    }
    else if(selectedHorse == "brownHorse"){
        var horseSelect = Crafty.e("2D, Canvas, Image, Mouse").image("images/brown_horse_border.png");
        horseSelect.x = bg._x+10; horseSelect.y = bg._y+10;
    }
    
    
    //start_line
    startLine1 = Crafty.e("2D,Canvas,Image,Mouse,Tween").attr({x:188,y:130,w:26,h:131}).image("images/start_line.png");

    
    BlackHorse = Crafty.e("2D, Canvas, SpriteAnimation, BlackHorse");
    BlackHorse.x= 40; BlackHorse.y=100;    
    BlackHorse.gravity = true;
    BlackHorse.up = false;
    BlackHorse.rowLevel=0;
    BlackHorse.animate('stop', 0, 0, 0) //setup animation
    BlackHorse.animate('animate', 0, 0, 9)
    BlackHorse.animate('stop', 1, 0);

    BrownHorse = Crafty.e("2D, Canvas, SpriteAnimation, BrownHorse");
    BrownHorse.x= 40; BrownHorse.y=130;    
    BrownHorse.gravity = true;
    BrownHorse.up = false;
    BrownHorse.rowLevel=0;
    BrownHorse.animate('stop', 0, 0, 0) //setup animation
    BrownHorse.animate('animate', 0, 0, 9)
    BrownHorse.animate('stop', 1, 0);

    WhiteHorse = Crafty.e("2D, Canvas, SpriteAnimation, WhiteHorse");
    WhiteHorse.x= 40; WhiteHorse.y=160;    
    WhiteHorse.gravity = true;
    WhiteHorse.up = false;  
    WhiteHorse.rowLevel=0;
    WhiteHorse.animate('stop', 0, 0, 0) //setup animation
    WhiteHorse.animate('animate', 0, 0, 9)
    WhiteHorse.animate('stop', 1, 0);


    //proceed to start
    beginRace = Crafty.e("2D,Canvas,Image,Mouse,Tween").attr({x:210,y:160,w:179,h:24}).image("images/begin_race.png");
   

    var tempBg = Crafty.e("BG14").attr({x:0, y:0, w:480, h:280});
    tempBg.alpha = 0.01;

    
    tempBg.bind("MouseUp", function(e) {
        tempBg.destroy();
        count = 0;
        beginRace.destroy();
        startLine1.tween({x:startLine1._x - 300}, 15);
        setTimeout(function(){
            startLine1.destroy();
        }, 1500);
        
        playBgMusic("GameMusic");
       
        init();  
        bgMovement();     
    })
  
    bg1.bind("MouseUp", function(e) { 
            pauseBtn.alpha = 1;
    });
    
       

    function init(){
        pauseBtnHover = Crafty.e("2D, Canvas, Image, Mouse").image("images/pause1.png");
        pauseBtnHover.x = 10; pauseBtnHover.y = 230;
        
        pauseBtn = Crafty.e("2D, Canvas, Image, Mouse").image("images/pause.png");
        pauseBtn.x = 10; pauseBtn.y = 230;    
   
        pauseBtn.bind("MouseDown", function(e) { 
            pauseBtn.alpha = 0.01;
        });
        
        pauseBtn.bind("MouseUp", function(e) { 
            pauseBtn.alpha = 1;
            playSfxAudio("BgmMenu");
            if(raceCompleted == false){
                CreatePauseScreen();
                isPaused = true;
                clearInterval(myVart);
                clearInterval(myVar);
                BlackHorse.animate('stop', 0, 0); 
                BrownHorse.animate('stop', 0, 0); 
                WhiteHorse.animate('stop', 0, 0);
            }
        });
    
        var headerTop = Crafty.e("2D, Canvas, Image, Mouse").attr({x:180,y:10,w:179,h:24}).image("images/header.png");
            
        beginRaceFunction();
        changeHorsePosition();
    }

});


function changeHorsePosition(){
    myVar = setInterval(function() {
        count = count + 1;
        if(count < 29){
            ondu  = Math.floor(Math.random()*(35)+1);
            eradu = Math.floor(Math.random()*(35)+1);
            mooru = Math.floor(Math.random()*(35)+1);
      
            blackSpeed = Math.floor(Math.random()*(ondu)+80);
            whiteSpeed = Math.floor(Math.random()*(eradu)+80);
            brownSpeed = Math.floor(Math.random()*(mooru)+80);
                
            var maxOne = Math.max(blackSpeed,whiteSpeed,brownSpeed);
            
            if(blackSpeed > whiteSpeed){
                if(whiteSpeed > brownSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/1st.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/2nd.png";
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/3rd.png";
                    }
                }
                else if(blackSpeed > brownSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/1st.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/3rd.png"
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/2nd.png"
                    }
                }
            }
            else if(whiteSpeed > brownSpeed){
                if(brownSpeed > blackSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/3rd.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/1st.png";
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/2nd.png";
                    }
                }
                else if(whiteSpeed > blackSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/2nd.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/1st.png"
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/3rd.png"
                    }
                }             
            }
            else if(brownSpeed > blackSpeed){
                if(blackSpeed > whiteSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/2nd.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/3rd.png";
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/1st.png";
                    }
                }
                else if(brownSpeed > whiteSpeed){
                    if(selectedHorse == "blackHorse"){
                        imageDisp = "images/3rd.png";
                    }else if(selectedHorse == "whiteHorse"){
                        imageDisp = "images/2nd.png"
                    }else if(selectedHorse == "brownHorse"){
                        imageDisp = "images/1st.png"
                    }
                }             
            }

            if(rankFlag == true){
                positionTop.destroy();
            }
            imgRetain = imageDisp;
            rankDisp(imageDisp);
            BlackHorse.x= blackSpeed; BlackHorse.y=80;    
            WhiteHorse.x= whiteSpeed; WhiteHorse.y=170;  
            BrownHorse.x= brownSpeed; BrownHorse.y=125;    
        }
        else{
            pauseBtn.destroy();
            clearTimeout(myVar);
            stillHorsesFunc();
        }
    }, 250);
    
}


function stillHorsesFunc(){
    BlackHorse.gravity = true;
    BlackHorse.up = false;
    BlackHorse.rowLevel=0;
    BrownHorse.gravity = true;
    BrownHorse.up = false;
    BrownHorse.rowLevel=0;
    WhiteHorse.gravity = true;
    WhiteHorse.up = false;
    WhiteHorse.rowLevel=0;
    console.log("imgRetain = "+imgRetain);
    if(imgRetain == "images/1st.png"){ 
        raceCompleted = true;
        countWenPause = 0;
        setTimeout(function() { 
            Crafty.scene('GameWin');
        },1000);
    }   
    else{
        raceCompleted = true;
        countWenPause = 0;
        setTimeout(function() { 
            Crafty.scene('GameLost');
        },1000);
    }
}


