/* Events.js
What to do in response to user events.
*/
var xNew;
var yNew;
function IsFreeBlock(Block) {
  switch(Block) { // Look for walls in the way:
    case o.W: case o.R: case o.B: case o.Y: case o.G: case o.C: case o.M: return false;
    default: for(b in Balls) { // Look for balls in the way:
      if((Balls[b][1]==xNew) && (Balls[b][2]==yNew)) {
        return false;
  } } }
  return true;
}
function Move() {
  if(Ball==-1) { // Just move the cursor:
    State=Rolling.No;
    x=xNew; y=yNew; MoveCursor();
  }else { // Move a ball:
    var Block=Levels[GetLevel()][LevelProperty.Map][yNew*DX+xNew];
    var OK=IsFreeBlock(Block); // Could be the Hole this Ball is looking for...
    if(Block==Balls[Ball][0]) { // Ball has found its Hole!
      Position(document.getElementById(Balls[Ball][3]),true); // Hide the icon
      x=xNew; y=yNew; // Move the Cursor onto the Hole
      Balls.splice(Ball,1); // Remove the ball
      if(Balls.length==0) { // Level complete!
        var L=GetLevel();
        if(L==Levels.length-1) L=0; // Game Competed!
        SetLevel(L+1);
        OnEnd();
        if(L==0) Say("Now see how much you've improved...");
        return;
    } } // If Level not complete, fall through to normalise the cursor:
    if(!OK) {
      State=Rolling.No;
      Ball=-1;
      MoveCursor(); // Show Normal Cursor
      return;
    }
    Balls[Ball][1]=x=xNew;
    Balls[Ball][2]=y=yNew;
    Position(document.getElementById(Balls[Ball][3]));
    MoveCursor();
    switch(State) { // Keep moving until an obstacle or hole is hit:
      case Rolling.Up:    window.setTimeout("OnUp   ()",50); break;
      case Rolling.Down:  window.setTimeout("OnDown ()",50); break;
      case Rolling.Left:  window.setTimeout("OnLeft ()",50); break;
      case Rolling.Right: window.setTimeout("OnRight()",50); break;
  } }
}
function OnUp() {
  if(y==0) return;
  xNew=x; yNew=y-1;
  State=Rolling.Up;
  Move();
}
function OnDown() {
  if(y==Levels[GetLevel()][LevelProperty.Height]-1) return;
  xNew=x; yNew=y+1;
  State=Rolling.Down;
  Move();
}
function OnLeft() {
  if(x==0) return;
  xNew=x-1; yNew=y;
  State=Rolling.Left;
  Move();
}
function OnRight() {
  if(x==Levels[GetLevel()][LevelProperty.Width]-1) return;
  xNew=x+1; yNew=y;
  State=Rolling.Right;
  Move();
}
function OnOK() {
  if(Level!=GetLevel()) OnEnd(); // Starting a new level
  else {
    Say('');
    for(b in Balls) { // See if Picking a ball:
      if((Balls[b][1]==x) && (Balls[b][2]==y)) {
        Ball=b;
        MoveCursor(); // Change to CursorBall
        break;
  } } }
}
function OnEnd() {
  SetTitle("Enter a Level Number (1-"+(Levels.length-1)+"):");
  document.getElementById('Status').style.height=(DY*(dy+1))+'px';
  Level=GetLevel();
  State=Rolling.No;
  Ball=-1;
  Balls=[];
  if(Level<1) SetLevel(1);
  else if(Level>=Levels.length) SetLevel(Levels.length-1);
  DrawLevel();
  document.getElementById('Level').focus();
}
