/* Copyright (c)2002 ELR Software Pty Ltd. http://www.elr.com.au   */
/*                                                                 */
/* This program is free software; you may use, redistribute and/or */
/* modify it under the terms of the GNU General Public License as  */
/* published by the Free Software Foundation; either version 2 of  */
/* the License, or (at your option) any later version.             */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
/* See the GNU General Public License for more details.            */
/* http://www.fsf.org/home.html                                    */

var Btns = 1;
var Cols = 1;
var Rows = 1;
var ScaleX = 256;
var ScaleY = 256;
var Score = 0;
var Scorer = 0;
var FudgeY = 20;
var Lastword = "";
var WA = new Array();
var SignOn = 'Click the target button to play. Press the space bar, or click the screen for the next target.';

function setParams(n) {
  switch (n) { /* number of buttons */
    case 2:  {Cols = 2; Rows = 1; ScaleX = 256; ScaleY = 256; break;}
    case 3:  {Cols = 3; Rows = 1; ScaleX = 180; ScaleY = 180; break;}
    case 4:  {Cols = 2; Rows = 2; ScaleX = 128; ScaleY = 128; break;}
    case 6:  {Cols = 3; Rows = 2; ScaleX = 100; ScaleY = 100; break;}
    case 8:  {Cols = 4; Rows = 2; ScaleX = 100; ScaleY = 100; break;}
    case 12: {Cols = 4; Rows = 3; ScaleX =  80; ScaleY =  80; break;}
    case 16: {Cols = 4; Rows = 4; ScaleX =  64; ScaleY =  64; break;}
    default: {Cols = 1; Rows = 1; ScaleX = 256; ScaleY = 256; break;} /* 1 */
  }
}

function checkSPEAK() {
  LG.SpeakOn = (document.forms[0][0].checked) ? true : false;
}

function makeWTable() {
  var w = Lastword;
  if (document.forms[0][1].checked) {
    while (w == Lastword) {
      n = LG.randInt(WA.length-1);
      w = WA[n]
      LG.Counter = n;
    }
  }
  else {
    LG.Counter++;
    LG.Counter = (LG.Counter >= WA.length) ? 0 : LG.Counter;
    LG.Counter = (LG.Counter < 0) ? 0 : LG.Counter;
    w = WA[LG.Counter];
  }
  Lastword = w;
  var ha = (Cols==1) ? "center" : LG.HAlign[LG.randInt(3)];
  var va = (Rows==1) ? "middle" : LG.VAlign[LG.randInt(3)];
  var s  = '<table id="wtable" border="0" width="'+(ScaleX*Cols)+'px" height="'+(ScaleY*Rows)+'px" cellspacing="12" cellpadding="12">';
      s += '<tr><td align="'+ha+'" valign="'+va+'" style="z-index:-10;" style="Xcursor:hand">';
      s += '<span class="textB" style="color:#000000;Xcursor:default">'+w+'</span></td></tr></table>';
  return(s);
}

function doScore() {
 /* alert(); */
  event.cancelBubble=true;
  Scorer++;
  if (Scorer >= Btns) {
  if (LG.SpeakOn) {MSA.agentSpeak(Lastword,false)}
    /* Score++; */
    Score += Btns; /* bonus for more buttons */
    document.all.score.innerHTML = 'Score&nbsp;<span class="textB">' + Score + '</span>';
  }
  return(true);
}

function makeBTable() {
  var s  = '<table id="btable" border="0" width="'+(ScaleX*Cols)+'px" height="'+(ScaleY*Rows)+'px" cellspacing="0" cellpadding="0">';
  for (var i=0; i<Rows; i++) {
    s += '<tr>';
    for (var j=0; j<Cols; j++) {
      s += '<td onClick="doScore(); this.style.visibility=\'hidden\'" style="cursor:hand;z-index:1;position:relative;"><img src="common/b256.gif" width="'+ScaleX+'px" width="'+ScaleY+'px"></td>';
    }
    s += '</tr>';
  }
  s += '</table>';
  return(s);
}

function resetN(r,n) {
  Btns = n;
  var src = event.srcElement;
  var s = src.tagName.toLowerCase();
  document.forms[1].elements[r].checked = true; /* Still don't fix WordCue! */
  doItem(0);
  return(false);
}

function doItem(n) {
  cancelBubble = true;
  if (!LG.Firsttime) {document.all.play.innerHTML = ''}
  LG.Firsttime = false;
  var bw = document.body.clientWidth;
  var bh = document.body.clientHeight;
  setParams(Btns);
  document.all.wtable.outerHTML = makeWTable();
  document.all.btable.outerHTML = makeBTable();
  var tw = document.all.wtable.width;
  var th = document.all.wtable.height;
  var x = (n == 0) ? ((bw/2)-(tw/2)) : (LG.randInt(bw-tw));
  var y = (n == 0) ? ((bh/2)-(th/2)+50) : (LG.randInt(bh-th-FudgeY));
  y += FudgeY;
  document.all.btable.style.left = x;
  document.all.btable.style.top = y;
  document.all.wtable.style.left = x;
  document.all.wtable.style.top = y;
  document.all.wtable.style.backgroundColor = LG.matchColor(document.body.style.backgroundColor);
  Scorer = 0;
}

function doNext() {
  cancelBubble = true;
  var src = event.srcElement;
  var s = src.tagName.toLowerCase();
  /* alert(s); */
  var thewordtag = 'span';
  if (WC.WCLoaded) {
    thewordtag = (WC.WCTogState()) ? '' : thewordtag;
  }
  if ((s == 'div') || (s == 'td') || (s == thewordtag)) {doItem(1)}
}

function startup() {
  WA = LG.Data.split2Array();
  document.body.style.backgroundColor = LG.randbgColor;
  document.onkeypress = LG.doHotKey;
  document.all.hed.innerText = LG.Data.title;
  checkSPEAK();
  if (LG.SpeakOn) {MSA.agentSpeak(SignOn,true)}
  doItem(0);
}

function makePage() {
var checked = (LG.Data.speak) ? 'checked' : '';
var s = '<center>'+
        '<table border="0" width="100%" cellspacing="2" cellpadding="0">'+
        '<tr><td align="left" valign="top" class="sysnote" nobreak wid2th="8%">'+
        '<form>'+
        '<input type="checkbox" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="checkSPEAK()" '+checked+'>&nbsp;Speak&nbsp;'+
        '<input type="checkbox" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)">&nbsp;Random&nbsp;</form></td>'+
        '<td align="center" valign="top" class="sysnote" nobreak><form>Buttons&nbsp;'+
        '1<input  type="radio" name="btns" value="1"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(0,1)" checked>&nbsp;'+
        '2<input  type="radio" name="btns" value="2"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(1,2)">&nbsp;'+
        '3<input  type="radio" name="btns" value="3"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(2,3)">&nbsp;'+
        '4<input  type="radio" name="btns" value="4"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(3,4)">&nbsp;'+
        '6<input  type="radio" name="btns" value="6"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(4,6)">&nbsp;'+
        '8<input  type="radio" name="btns" value="8"  onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(5,8)">&nbsp;'+
        '12<input type="radio" name="btns" value="12" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(6,12)">&nbsp;'+
        '16<input type="radio" name="btns" value="16" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="resetN(7,16)">'+
        '</form></td>'+
        '<td id="score" align="right" valign="top"class="sysnote" nobreak width="15%">&nbsp;</td></tr>'+
        '<tr><td align="center" colspan="3"><h1 id="hed"></h1></td></tr>'+
        '</table>'+
        '<span id="play" class="sysnote"><em>'+SignOn+'</em></span>'+
        '</center>'+
        '<div style="width:100%; height:100%" onClick="doNext(1)";>'+
        '<style>#btable {position:absolute;} #wtable {position:absolute;}</style>'+
        '<table id="wtable"  border="0" width="256px" height="256px" cellspacing="0" cellpadding="0"></table>'+
        '<table id="btable"  border="0" width="256px" height="256px" cellspacing="0" cellpadding="0"></table>'+
        '</div>';

  document.body.innerHTML = s;
}
