<!--

function Board()
{
 // Properties
  this.marble = new Array();
  this.selected = -1;
  this.textout = "";
  this.oldtext = "";
 // Methods
  this.redraw = Board_redraw;
  this.beginGame = Board_beginGame;
  this.click = Board_click;
  this.checkBoard = Board_checkBoard;
}

function Board_redraw()
{
  for (var x = 0; x < 15; x++)
  {
    this.marble[x].target.src = "" + this.marble[x].status + ".gif";
  }
  if (this.selected != -1)
  {
    this.marble[this.selected].target.src = "selected.gif";
  }
  this.textout.value = this.oldtext;
}

function Board_beginGame()
{
  this.selected = -1;
  for (var x = 0; x < 15; x++)
  {
    this.marble[x].status = 1;
  }
  this.marble[Math.round(Math.random() * 14)].status = 0;
  this.oldtext = " Game in Progress... "
  this.redraw();
}

function Board_click(clicked)
{
  var middleman
  if (this.marble[clicked].status == 0)
  {
    if (this.selected != -1)
    {
      if ((this.marble[clicked].jumpable & Math.pow(2, this.selected)) == Math.pow(2, this.selected))
      {
        middleman = Math.floor((Math.max(clicked, this.selected) - Math.min(clicked, this.selected)) / 2) + Math.min(clicked, this.selected);
        if (this.marble[middleman].status == 1)
        {
          this.marble[this.selected].status = 0;
          this.marble[this.selected].target.src = "0.gif";
          this.marble[clicked].status = 1;
          this.marble[clicked].target.src = "1.gif"
          this.marble[middleman].status = 0;
          this.marble[middleman].target.src = "vanish.gif";
          window.setTimeout(this.marble[middleman].name + ".target.src = '0.gif'", 500);
          this.selected = -1;
          this.checkBoard();
        }
      }
    }
  }
  else
  {
    if (this.selected != -1)
    {
      this.marble[this.selected].target.src = "1.gif";
    }
    this.marble[clicked].target.src = "selected.gif";
    this.selected = clicked;
  }
}

function Board_checkBoard()
{
  var win = 0;
  var checkit = 0;
  for (var x = 0; x < 15; x++)
  {
    win += this.marble[x].status
  }
  if (win == 1)
  {
    this.oldtext = "      You've Won!!!      ";
    this.textout.value = this.oldtext;
    alert("Well, done!  You've solved the puzzle!\nTruely you are a master.");
  }
  else
  {
    for (var x = 0; x < 15; x++)
    {
      if (this.marble[x].status == 1)
      {
        for (var y = 0; y < 15; y++)
        {
          if(((this.marble[x].jumpable & Math.pow(2,y)) == Math.pow(2,y)) &&  (this.marble[y].status == 0))
          {
            middleman = Math.floor((Math.max(x, y) - Math.min(x, y)) / 2) + Math.min(x, y);
            if (this.marble[middleman].status == 1) {checkit++;}
          }
        }
      }
    }
    if (checkit == 0)
    {
      this.oldtext = " No More Moves Possible! ";
      this.textout.value = this.oldtext;
      alert("No more moves are possible.\nSorry. Try again...");
    }
  }
}


//============================================================================
// Object marble
//  Properties:
//   jumpables               integer binary map
//   status                  integer status indicator
//   name                    string name of variable
//   target                  string name of image
//----------------------------------------------------------------------------

function marble()
{
 // Properties
  this.jumpables = 0;
  this.status = 1;
  this.name = "";
  this.target = "";
}

function init()
{
  myMove = new Board;
  for (var x = 0; x < 15; x++)
  {
    myMove.marble[x] = new marble();
    myMove.marble[x].name = "myMove.marble[" + x.toString() + "]";
    myMove.marble[x].target = eval("document.marble" + x.toString());
  }
  myMove.marble[0].jumpable = 40;
  myMove.marble[1].jumpable = 320;
  myMove.marble[2].jumpable = 640;
  myMove.marble[3].jumpable = 5153;
  myMove.marble[4].jumpable = 10240;
  myMove.marble[5].jumpable = 20489;
  myMove.marble[6].jumpable = 258;
  myMove.marble[7].jumpable = 516;
  myMove.marble[8].jumpable = 66;
  myMove.marble[9].jumpable = 132;
  myMove.marble[10].jumpable = 4104;
  myMove.marble[11].jumpable = 8208;
  myMove.marble[12].jumpable = 17448;
  myMove.marble[13].jumpable = 2064;
  myMove.marble[14].jumpable = 4128;
  myMove.textout = document.gui.outtext;
  myMove.oldtext = " Welcome to Solitaire "
  myMove.textout.value = myMove.oldtext
}

var myMove = "notready";


//-->
