var mTop = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var move = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var mGot = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var mJump = new Array(204, 204, 204, 204, 204);
var jumps = new Array(0, 0, 0, 0);
var jump = 5;
var dropId;
var score;
var drops;
function start() {
  score = 0;
  drops = 0;
  window.clearTimeout(dropId);
  document.getElementById("timeup").style.visibility = "hidden";
  clear();
  document.getElementById("sc").firstChild.nodeValue = 0;
  dropId = window.setTimeout("drop();", 100);
}

function drop() {
  var theM;
  var theJ;
  for (i=0; i<5; i++) {
    mTop[i] +=5;
    if (mJump[i]!=204) mTop[mJump[i]]+=5;
    theM = "m"+i;
    theJ = "m"+mJump[i];
    if (mTop[i]>=470) {
      if (mJump[i]!=204) document.getElementById(theJ).style.visibility = "hidden";
      drops++;
      mJump[i] = 204;
      if (Math.floor(Math.random()*4)==3) {
        mTop[jump] = 0;
        move[jump] = Math.floor(Math.random()*4);
        mGot[jump] = 0;
        mJump[i] = jump;
        if (++jump==10) jump=5;
        theJ = "m"+mJump[i];
        document.getElementById(theJ).style.visibility = "visible";
        document.getElementById(theJ).className = "move"+move[mJump[i]];
      }
      mTop[i] = 0;
      move[i] = Math.floor(Math.random()*4);
      mGot[i] = 0;
      document.getElementById(theM).className = "move"+move[i];
      clear();
    }
    document.getElementById(theM).style.top = mTop[i] + "px";
    if (mJump[i]!=204) document.getElementById(theJ).style.top = mTop[mJump[i]] + "px";
  }
  if (drops<50) {
    dropId = window.setTimeout("drop();", 100);
  }
  else {
    document.getElementById("end").firstChild.nodeValue = score;
    document.getElementById("timeup").style.visibility = "visible";
  }
}

function getKey(e) {
  if (e.keyCode) keycode=e.keyCode;
  else keycode=e.which;
  switch (keycode) {
    case 114:
      start();
      break;
    case 112:
      alert("Paused");
      break;
    case 52:
      moved(0);
      break;
    case 56:
      moved(1);
      break;
    case 50:
      moved(2);
      break;
    case 54:
      moved(3);
      break;
  }
}

function moved(dir) {
  var dist;
  for (i=0; i<10; i++) {
    if (move[i]==dir && mGot[i]==0) {
      dist = Math.abs(450-mTop[i]);
      switch (dist) {
        case 0:
          score+=5;
          mGot[i] = 1;
          hit("perfect");
          break;
        case 5:
          score+=3;
          mGot[i] = 1;
          hit("good");
          break;
        case 10:
          score+=1;
          mGot[i] = 1;
          hit("close");
          break;
      }
    }
  }
}

function clear() {
  document.getElementById("perfect").style.visibility = "hidden";
  document.getElementById("good").style.visibility = "hidden";
  document.getElementById("close").style.visibility = "hidden";
}

function hit(s) {
  document.getElementById(s).style.visibility = "visible";
  document.getElementById("sc").firstChild.nodeValue = score;
}