// Globals
var mmplay= 0; // 0=setup; 10..1=play;
var mmpegi= new Array(); // Peg image references on board
var mmpini= new Array(); // Pin image references on board
var mmpegs= new Array(); // Peg states on board (0=blank, 1..8=color)
var mmtrayi= new Array(); // Tray cover image references on box
var mmtraysel= new Array(); // Tray select layer references on box (visible=selected; hidden=not selected)
var mmtrays= new Array(); // Tray state (0=disable; 1=enable; 2=selected)
var mmrndpeg= new Array(); // Random pegs in a tray
var mmcolor= 0; // Selected tray color (0=none; 1..8=color)

// Preload images
mmimgpeg= new Array();
mmimgpeg[0]= new Image(); mmimgpeg[0].src= "blankpeg.gif";
mmimgpeg[1]= new Image(); mmimgpeg[1].src= "redpeg.gif";
mmimgpeg[2]= new Image(); mmimgpeg[2].src= "greenpeg.gif";
mmimgpeg[3]= new Image(); mmimgpeg[3].src= "bluepeg.gif";
mmimgpeg[4]= new Image(); mmimgpeg[4].src= "purplepeg.gif";
mmimgpeg[5]= new Image(); mmimgpeg[5].src= "brownpeg.gif";
mmimgpeg[6]= new Image(); mmimgpeg[6].src= "cyanpeg.gif";
mmimgpeg[7]= new Image(); mmimgpeg[7].src= "blackpeg.gif";
mmimgpeg[8]= new Image(); mmimgpeg[8].src= "orangepeg.gif";
mmimgtray= new Array();
mmimgtray[0]= new Image(); mmimgtray[0].src= "trayx.gif";
mmimgtray[1]= new Image(); mmimgtray[1].src= "tray.gif";
mmimgtray[2]= new Image(); mmimgtray[2].src= "traysel.gif";
mmimgpin= new Array();
mmimgpin[0]= new Image(); mmimgpin[0].src= "blankpin.gif";
mmimgpin[1]= new Image(); mmimgpin[1].src= "whitepin.gif";
mmimgpin[2]= new Image(); mmimgpin[2].src= "blackpin.gif";

// Utils ***********************************************

// Find object by name or id
function mmfindobj(id) {
  var i, x, d;
  d= arguments.length==2 ? arguments[1] : document;
  x= d[id];
  if (!x && d.all) x= d.all[id];
  for (i=0; !x && i<d.forms.length; i++) x= document.forms[i][id];
  if (!x && document.getElementById) x= document.getElementById(id);
  if (!x && document.getElementByName) x= document.getElementByName(id);
  for (i=0; !x && d.layers && i<d.layers.length; i++) x= findobj(id, d.layers[i].document);
  return(x);
}

// Write table ********************************************
function mmwr() {
  var img;
  var x, y, a=false;

  if (arguments.length==2) {
    x= arguments[0];
    y= arguments[1];
    a= true;
  }

  // Container begin ****************************************
  img=
    "<div id='mmly' style='position:"+
      (a ?
	"absolute; left:"+x+"px; top:"+y+"px; " :
	"relative; ")+
      "width:340px; height:330px'>\n";
  // Board begin
  img+=
    "<div id='mmboardly' style='position:absolute; "+
      "top:0px; left:0px"+
      "width:142px; height:330px'>\n"+
    "  <img src='board.gif' style='width:142px; height:330px'>\n";
  // Cover
  img+=
    "<div id='mmcover' style='position:absolute; "+
      "top:8px; left:10px; "+
      "width:89px; height:28px; visibility:visible; z-index:3'>\n"+
    "  <img src='cover.gif' style='width:89px; height:28px'>\n"+
    "</div>\n";

	// Pegs *************************************************
	
  for (y=0; y<11; y++) {
    for (x=0; x<4; x++) {
      img+=
        "<div style='position:absolute; "+
	  "left:"+(14+21*x)+"px; top:"+(14+28*y)+"px; "+
	  "width:18px; height:18px; z-index:1'>\n"+
        "  <img name='mmpegi"+y+"x"+x+"' src='"+mmimgpeg[0].src+"' "+
	    "onMouseDown=\"mmpegclick("+y+","+x+")\" "+
	    "style='width:18px; height:18px'>\n"+
        "</div>\n";
    }
  }
  
  // Pins *******************************************************
  
  for (y=1; y<11; y++) {
    for (x=0; x<4; x++) {
      img+=
        "<div style='position:absolute; "+
	  "left:"+(101+12*(x&1))+"px; top:"+(8+28*y+6*(x&2))+"px; "+
	  "width:12px; height:12px'>\n"+
        "  <img name='mmpini"+y+"x"+x+"' src='"+mmimgpin[0].src+"' "+
	  "style='width:12px; height:12px'>\n"+
        "</div>\n";
    }
  }

  // Board end ************************************************

  img+=
    "</div>\n";

  // Box begin ***************************************************
  
  img+=
    "<div id='mmboxly' style='position:absolute; "+
      "top:0px; left:200px; "+
      "width:137px; height:248px'>\n"+
    "  <img src='box.gif' style='width:137px; height:248px'>\n";
  // Trays + random pegs + cover
  for (x=0; x<8; x++) {
    // Select
    img+=
      "<div id='mmtraysel"+(x+1)+"' style='position:absolute; "+
        "left:"+(10+57*(x&1))+"px; top:"+(10+28.5*(x&14))+"px; "+
	"width:54px; height:54px; z-index:1; visibility:hidden'>\n"+
      "  <img src='"+mmimgtray[2].src+"'>\n"+
      "</div>\n";
    // Inside begin
    img+=
      "<div style='position:absolute; "+
        "left:"+(10+57*(x&1))+"px; top:"+(10+28.5*(x&14))+"px; "+
	"width:54px; height:54px; z-index:2'>\n";
    // Random pegs
    for (y=0; y<15; y++) {
      img+=
	"<div id='mmrndpeg"+(x+1)+"_"+y+"' style='position:absolute; "+
	  "left:"+Math.floor(2+32*Math.random())+"px; "+
	  "top:"+Math.floor(2+32*Math.random())+"px; "+
	  "width:18px; height:18px; z-index:2'>\n"+
	"  <img src='"+mmimgpeg[x+1].src+"' style='width:18px; height:18px'>\n"+
        "</div>\n";
    }
	
    // Inside end ***********************************************
	
    img+=
      "</div>\n";
    // Cover
    img+=
      "<div style='position:absolute; "+
        "left:"+(10+57*(x&1))+"px; top:"+(10+28.5*(x&14))+"px; "+
	"width:54px; height:54px; z-index:3; visibility:visible'>\n"+
      "  <img name='mmtrayi"+(x+1)+"' src='"+mmimgtray[2].src+"' "+
        "onMouseDown=\"mmtrayclick("+(x+1)+")\" "+
	  "style='cursor:hand; width:54px; height:54px'>\n"+
      "</div>\n";
  }
  
  // Box end ******************************************************
  
  img+=
    "</div>\n";

  // Button ******************************************************
  
  img+=
    "<div id='mmbuttonly' style='position:absolute; "+
      "top:265px; left:200px; width:137px; height:20px'>\n"+
    "<form name='mmfrm'>\n"+
    "<div id='mmmsgly' style='text-align:center; font-weight:bold; height:30px'>\n"+
    "Select colors"+
    "</div>\n"+
    "  <input type='button' name='mmbutton' value='Start' "+
        "onMouseDown='mmgo()' style='width:100%; font-weight:bold'>\n"+
    "</form>\n"+
    "</div>\n";

  // Container end ***********************************************
  
  img+=
    "</div>\n";
  // Write
  document.write(img);

  // Initialize references *****************************************
  // Pegs **********************************************************
  
  for (y=0; y<11; y++) {
    mmpegi[y]= new Array();
    mmpegs[y]= new Array();
    for (x=0; x<4; x++) {
      mmpegi[y][x]= mmfindobj("mmpegi"+y+"x"+x);
      mmpegs[y][x]= 0;
    }
  }
  
  // Pins **********************************************************
  
  for (y=1; y<11; y++) {
    mmpini[y]= new Array();
    for (x=0; x<4; x++) {
      mmpini[y][x]= mmfindobj("mmpini"+y+"x"+x);
    }
  }
  
  // Trays ***********************************************************
  
  for (x=1; x<9; x++) {
    mmtrayi[x]= mmfindobj("mmtrayi"+x);
    if (x<7) {
      mmtrays[x]= true;
      mmtrayi[x].src= mmimgtray[1].src;
    } else {
      mmtrays[x]= false;
      mmtrayi[x].src= mmimgtray[0].src;
    }
  }
  
  // Trays select layer **********************************************
  
  for (x=1; x<9; x++) {
    mmtraysel[x]= mmfindobj("mmtraysel"+x);
  }
  
  // Random pegs ***************************************************
  
  for (x=1; x<9; x++) {
    mmrndpeg[x]= new Array();
    for (y=0; y<15; y++) {
      mmrndpeg[x][y]= mmfindobj("mmrndpeg"+x+"_"+y);
    }
  }
}

// Tray click ****************************************************

function mmtrayclick(x) {
  if (mmplay==0) {
    // Open/close
    if (mmtrays[x]) {
      mmtrays[x]= false;
      mmtrayi[x].src= mmimgtray[0].src;
    } else {
      mmtrays[x]= true;
      mmtrayi[x].src= mmimgtray[1].src;
      mmrndtray(x);
    }
  } else {
    // Select
    if (mmtrays[x]) {
      if (mmcolor>0) {
        mmtraysel[mmcolor].style.visibility= "hidden";
      }
      mmcolor= x;
      mmrndtray(x);
      mmtraysel[x].style.visibility= "visible";
    }
  }
}

// Peg click ************************************************

function mmpegclick(y, x) {
  if (y==mmplay && mmplay>0) {
    mmpegs[y][x]= mmcolor;
    mmpegi[y][x].src= mmimgpeg[mmcolor].src;
  }
}

// Randomize pegs in a tray ************************************

function mmrndtray(x) {
  var y;
  for (y=0; y<15; y++) {
    mmrndpeg[x][y].style.left= Math.floor(2+32*Math.random())+"px";
    mmrndpeg[x][y].style.top= Math.floor(2+32*Math.random())+"px";
  }
}

// Start/guess *****************************************************

function mmgo() {
  var x, y, pos, cols, win= false;
  var col= new Array();
  var hid= new Array();

  // Evaluate guess *************************************************
  
  if (mmplay>0) {
    for (x=0; x<4; x++) {
      if (mmpegs[mmplay][x]==0) { // Error
        return;
      }
      col[x]= mmpegs[mmplay][x];
      hid[x]= mmpegs[0][x];
    }
    pos=0;
    for (x=0; x<4; x++) {
      if (hid[x]==col[x]) {
	col[x]= -1; hid[x]= -2; pos++;
      }
    }
    cols=0;
    for (x=0; x<4; x++) {
      for (y=0; y<4; y++) {
        if (hid[x]==col[y]) {
	  col[y]= -1; hid[x]= -2; cols++;
	  break;
	}
      }
    }
    win= pos==4;
    for (x=0; x<4; x++) {
      if (0<pos--) {
        mmpini[mmplay][x].src= mmimgpin[2].src;
      } else if (0<cols--) {
        mmpini[mmplay][x].src= mmimgpin[1].src;
      } else {
        mmpini[mmplay][x].src= mmimgpin[0].src;
      }
    }
  }

  // Hand cursor off ***********************************************
  
  if (mmplay<11) {
    for (x=0; x<4; x++) {
      mmpegi[mmplay][x].style.cursor= "auto";
    }
  }

  // Win/lose/start *****************************************************
  
  if (win || mmplay==1) {
    if (win) {
      mmfindobj("mmmsgly").innerHTML= "Congratulation!";
    } else {
      mmfindobj("mmmsgly").innerHTML= "Maybe next time...";
    }
    mmfindobj("mmbutton").value= "Start";
    mmfindobj("mmcover").style.visibility= "hidden";
    mmplay=0;
    for (x=1; x<9; x++) {
      mmtrayi[x].style.cursor= "hand";
    }
  } else if (mmplay==0) {
    // Count colors
    cols= 0;
    for (x=1; x<9; x++) {
      if (mmtrays[x]) {
        col[cols++]=x;
      }
    }
	
    // At least two colors *************************************
	
    if (cols==0) {
      mmtrayclick(1); mmtrayclick(2);
      cols=2; col[0]=1; col[1]=2;
    } else if (cols==1) {
      if (col[0]==1) {
        mmtrayclick(2);
	cols=2; col[1]=2;
      } else {
        mmtrayclick(1);
	cols=2; col[1]=1;
      }
    }
	
    // Inactive trays ***********************************************
	
    for (x=1; x<9; x++) {
      if (!mmtrays[x]) {
        mmtrayi[x].style.cursor= "auto";
      }
    }
	
    // Cover **********************************************************
	
    mmfindobj("mmcover").style.visibility= "visible";
    
	// Hide ********************************************************
	
    for (x=0; x<4; x++) {
      mmpegs[0][x]= col[Math.floor(cols*Math.random())];
      mmpegi[0][x].src= mmimgpeg[mmpegs[0][x]].src;
    }
	
    // Erase pegs and pins ******************************************
	
    for (y=1; y<11; y++) {
      for (x=0; x<4; x++) {
        mmpegs[y][x]= 0;
	mmpegi[y][x].src= mmimgpeg[0].src;
	mmpegi[y][x].style.cursor= "auto";
	mmpini[y][x].src= mmimgpin[0].src;
      }
    }
	
    // Go ********************************************************
	
    mmplay=11;
    mmfindobj("mmmsgly").innerHTML= "";
    mmfindobj("mmbutton").value= "Guess";
    mmtrayclick(col[0]);
  }

  // Next row *****************************************************
  
  if (mmplay>0) {
    mmplay--;
    for (x=0; x<4; x++) {
      mmpegi[mmplay][x].style.cursor= "hand";
    }
  }
}

