// Globals.js
// Globals and Interfaces to DOM objects

// Global Constants:
var DX=13; // Grid dimensions in blocks:
var DY=13;
var dx=21; // Block Dimensions (Width and Height in Pixels)
var dy=21;
var x0=2*dx; // Game board origin:
var y0=280;
// Global Variables:
var x=0; // Current Position:
var y=0;
// enum  o=Floor, W=Wall, R=Red Hole, r=Red Ball; same for Green, Yellow, Cyan, Magenta, X=Initial Cursor Position
// The name is 'o' to make the Level Maps legible.
var o={o:0,W:1,R:2,B:3,Y:4,G:5,C:6,M:7,r:8,b:9,y:10,g:11,c:12,m:13,X:14};
var Level=0; // Taken from form. Zero means no level selected. This is used to see if the form changed.
var Rolling={No:0,Up:1,Down:2,Left:3,Right:4};
var State=Rolling.No;
var Ball=-1;
var Balls=[]; // Each Ball entry is [Hole o,x,y]

// Methods:
function Position2String(Pos) {return Pos<10 ? '0'+Pos : ''+Pos;}
function SetTitle(Message) {document.getElementById('Title').innerHTML=Message;}
function Say(Message) {document.getElementById('Status').innerHTML=Message;}
function Hint(Message) {document.getElementById('Hint').innerHTML=Message;}
function GetLevel() {var N=parseInt(document.getElementById('Level').value,10); return isNaN(N) ? 1 : N;}
function SetLevel(NewLevel) {document.getElementById('Level').value=Level=NewLevel;}
function Position(ID,Hide) {
  ID.style.top = Position2String(y0+y*dy)+'px';
  ID.style.left= Position2String(x0+x*dx)+'px';
  ID.style.display=(Hide ? 'none' : 'block');
}
function HideCursors() {
  document.getElementById("Cursor"    ).style.display='none'; // Hide the Cursors
  document.getElementById("CursorBall").style.display='none';
  document.getElementById("CursorWall").style.display='none';
}
function MoveCursor() {
  HideCursors();
  if(OverBall()) Say('Tap Ctrl, Return or Enter to pick up the ball.');
  else           Say('Use the arrow keys to move the cursor over the ball you want to move:');
  if(OverWall()) Position(document.getElementById("CursorWall"));
  else if(Ball!=-1) {
    Position(document.getElementById("CursorBall"));
    Say('Press an arrow key to throw the ball in that direction.');
  }else{ // Over Floor or Hole:
    Position(document.getElementById("Cursor"));
  }
}
function ShowWall(x,y,Hide) {Position(document.getElementById("Wall"+Position2String(x)+Position2String(y)),Hide);}
function OverWall() {return Levels[GetLevel()][LevelProperty.Map][DX*y+x]==o.W;}
function OverBall() {
  for(b in Balls) { // Look for balls underneath:
    if((Balls[b][1]==x) && (Balls[b][2]==y)) return true;
  }
  return false;
}
