﻿//***************************************
// Keepy Uppy Source Code   *
//***************************************
//Programmer:       Matthew Bradley
//Date:             03/12/2007
//Licenced by:      A trouble Halved ltd

 var intBoardx = 510;
 var intBoardy = 425;
 var intCurrX = 10;
 var intCurrY = 10;
 var timerThreadID = null;
 var timerThread2ID = null;
 var intSpeed = 80;
 var dirX = 1;
 var dirY = 0;
 var intClickCount = 0;
 var intSecs = 0;
 var dirDirection = 2;
 var SpeedIncrease = 10;
 var randomRange = 5;
 var HardGame = 0;
 
function rand ( n )
{  return ( Math.floor ( Math.random ( ) * n + 1 ) ); }
  
function initGame(n) {
    endLoop;
    document.getElementById('Start').style.visibility = "hidden";
    document.getElementById('Gameover').style.visibility = "hidden";
    document.getElementById('scoreRemaining').style.visibility = "hidden";
    intClickCount = 0;
    intCurrX = 10;
    intCurrY = 10;
    intSpeed = 80;
    intSecs = 0;
    timerThread2ID = setInterval(CountClock,1000);
    mainLoop();
    
    if (n == 1) {
        randomRange = 10;
        SpeedIncrease = 20;
        HardGame = n;
        intSpeed = 50;
    }
    updateScore();
}

function mainLoop() {
   updateScore();
   
   //Calc the cooridinates if the ball has reached the top
   if(intCurrY == 0) { dirY = 0; } 
   
   if(intCurrX >= intBoardx) { dirX = 0; }
   
   if(intCurrX <= 0) {dirX = 1; }
             
   //Calc which direction the system should move
   if(dirX == 1) { goRight(); } else { goLeft(); }
   
   if(dirY == 1) {  goUp(); } else { goDown(); }
   
   //Move the pudding!!
   movePud();
   moveShadow();
   //keep running if the ball hasn't hit the floor yet
   if(intCurrY <= intBoardy) {
        timerThreadID = setTimeout("mainLoop();",intSpeed);
   } else {
        endLoop();
   }
}

function endLoop() {
    clearInterval(timerThreadID);
    clearInterval(timerThread2ID);
    gameOver();
}

function movePud() {
    document.getElementById('pud').style.left = intCurrX + 'px';
    document.getElementById('pud').style.top = intCurrY + 'px';
}

function moveShadow() {
    document.getElementById('shadow').style.left = intCurrX - 40 + 'px';
    document.getElementById('shadow').style.width = 47 + Math.floor(intCurrY / 5) + 'px';
}

function goRight() {
    intCurrX = intCurrX + dirDirection;
}

function goLeft() {
    intCurrX = intCurrX - dirDirection;
}

function goUp() {
    intCurrY = intCurrY - 5;
}

function goDown() {
    intCurrY = intCurrY + 5;
}

function imageClick() {
        dirY = 1;
        dirDirection = rand(randomRange);
        intClickCount = intClickCount + 1;
        
        //Increase the speed to make it worse over time
        if( intClickCount % 2 == 0) {
            if(intSpeed != 0) { intSpeed = intSpeed - SpeedIncrease;}
        }
        updateScore();
        
        //If its close to the edge then go the other way anyway     
        if(intCurrX >= intBoardx - 150) { dirX = 0; }
        //If its close to the other edge then go the other way anyway     
        if(intCurrX <= 150) { dirX = 1; }
}

function updateScore() {
    var strDetails;
    
    if(HardGame == 1) {
        strDetails = "<strong>HARD GAME</strong><br />"
    } else {
        strDetails = "<strong>EASY GAME</strong><br />"
    }
    
    strDetails = strDetails+"<strong>Your Kicks: </strong>"+intClickCount;
    strDetails = strDetails+"<br/>";
    strDetails = strDetails+"<strong>Air Time: </strong>"+intSecs+" sec";
    document.getElementById('score').innerHTML = strDetails;
}

function gameOver() {

    var strResults = "";
    
    if (HardGame == 0 ) {
    
        if(intSecs >= 60 ) { strResults = "You must be cheating!";}
        if(intSecs <= 59 ) { strResults = "You're just showing off now!";}
        if(intSecs <= 40 ) { strResults = "Looking good. Top marks!";}
        if(intSecs <= 30 ) { strResults = "Hey! Not bad at all.";}
        if(intSecs <= 20 ) { strResults = "You're getting there, but you could do better!";}
        if(intSecs <= 10 ) { strResults = "Forgotten where your mouse is?!";}    
    
    } else {
        if(intSecs >= 50 ) { strResults = "This is impossible! You must be Superman!";}
        if(intSecs <= 49 ) { strResults = "Superhuman reactions! Amazing!";}
        if(intSecs <= 40 ) { strResults = "Wow, thats some football control!";}
        if(intSecs <= 30 ) { strResults = "Hey! Not bad at all.";}
        if(intSecs <= 20 ) { strResults = "Maybe the hard game isn't for you!";}
        if(intSecs <= 10 ) { strResults = "Forgotten where your mouse is?!";}   
   
    }
    
    document.getElementById('scoreRemaining').innerHTML = "You've kept the football going for "+intSecs+" seconds!<br/><br/><strong>"+strResults+"</strong><br/><br/>";
    document.getElementById('scoreRemaining').style.visibility = "visible";
    document.getElementById('Gameover').style.visibility = "visible";
}

function CountClock() {
    intSecs++;
    updateScore();
}
