/* 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 Score   = 0;
var ScoreIt = false;
var hitword = '';
var UPCASE = false;
var Signon1 = 'Click the Go button (or press the space bar) to play, then find the matching word.';
var Signon2 = '(You can use keys 1 to 4 to try matching a word)';

function doItem(key) {
  if ((key == 32) || (key == 13) || (typeof(key) == 'undefined')) {doGO()}
  key = key - 49; /* Convert 1-4 to 0-3 */
  if ((key>=0) && (key<=3)) {matchWord(key)}
}

var MaxW = 4;
var WA = new Array(MaxW);
function doGO() {
  if (LG.Firsttime) {
    promptrow.innerHTML = '';
    promptrow.style.height = '0px';
    wordrow.style.height = '100px';
    LG.Firsttime = false;
  }
  WA = LG.shuffleA(LG.Data.split2Array());
  for (var i=0; i<MaxW; i++) {
    WA[i] = ((WA[i]=='') || (typeof(WA[i]) != 'string')) ? (WA[0]+' '+i) : WA[i];
    var w  = eval('wrd'+i);
    var wn = eval('wrdno'+i);
    w.innerText = (UPCASE) ? WA[i].toUpperCase() : WA[i];
    wn.innerText = (i+1)+'.';
    w.style.color = '#000080';
    wn.style.color = '#000080';
  }
  w = eval('wrd'+MaxW);
  hitword = WA[LG.randInt(MaxW-1)];
  w.innerText = (UPCASE) ? hitword.toUpperCase() : hitword;
  if (LG.SpeakOn) {MSA.agentSpeak(hitword,false)}
  ScoreIt = true;
  startAnts();
}

ANTS = 0; /* TimeOut handle */

function startAnts() {
  if (is.ie5up) {
    try {updateAnts()}
    catch(err) {/* alert(err)*/}
  }
  else {updateAnts()}
}

function stopAnts() {if (ANTS > 0) {clearTimeout(ANTS)}}

function checkSPEAK() {
  LG.SpeakOn = (document.forms[0][0].checked) ? true : false;
}

function checkCASE() {
  UPCASE = (document.forms[0][1].checked) ? true : false;
  /* doItem(); */
}

function checkANTS() {
  ANTS_ON = (document.forms[0][2].checked) ? true : false;
  ants_init();
}

function thisWordIs(w) {
  if (LG.SpeakOn) {MSA.agentSpeak('This word is '+w,false)}
}

function matchWord(n) {
  if (!WC.WCTogState()) {
    var wrd = eval('wrd'+n);
    var X = displaytable.offsetLeft + wrd.parentElement.offsetLeft;
    var Y = displaytable.offsetTop  + wrd.parentElement.offsetTop;
    if (WA[n].toLowerCase() == hitword.toLowerCase()) { /* Correct */
      var w = eval('wrd'+n);
      w.style.color = '#ff0000';
      var w = eval('wrd'+MaxW);
      w.style.color = '#ff0000';
      stopAnts();
      if (ScoreIt) {
       if (LG.SpeakOn) {MSA.agentSpeak(LG.AgentYN[0],false)}
        Score++;
        score.innerHTML = 'Score&nbsp;<span class="textB">' + Score + '</span>';
        ScoreIt = false;
      }
      else {
        if (LG.SpeakOn) {MSA.agentSpeak(LG.AgentYN[2],true,X,Y)} /* Already done */
    /*    if (LG.SpeakOn) {MSA.agentSpeak(LG.AgentYN[2],true,event.screenX-event.offsetX,event.screenY-event.offsetY)} */ /* Already done */
      }
    }
    else {
      if (LG.SpeakOn) {MSA.agentSpeak(LG.AgentYN[1],true,X,Y)} /* Wrong */
    /*   if (LG.SpeakOn) {MSA.agentSpeak(LG.AgentYN[1],true,event.screenX-event.offsetX,event.screenY-event.offsetY)} */ /* Wrong */
    }
  }
}

function makePage() {
var checked = (LG.Data.speak) ? 'checked' : '';
var s = '<div id="ant1" class="ant"><img name="antimg1" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant2" class="ant"><img name="antimg2" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant3" class="ant"><img name="antimg3" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant4" class="ant"><img name="antimg4" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant5" class="ant"><img name="antimg5" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant6" class="ant"><img name="antimg6" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant7" class="ant"><img name="antimg7" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<div id="ant8" class="ant"><img name="antimg8" src="./ants/transp.gif" width="13" height="13"></div>'+
        '<script language="JavaScript">'+
        '/* createLoadBar(240, 20, 1, "#000000", "#cccccc", "#999999", "MS Sans serif,Arial,Helvetica", 1, "<b>Loading ants, please wait...</b>"); */'+
        'createLoadBar(240, 20, 1, "#000000", "#000000", "#cccccc", "Arial,Helvetica,MS Sans serif", 1, "<b>Loading ants, please wait...</b>");'+
        '</script>'+
        '<center>'+
        '<table id="displaytable" border="0" width="100%" cellspacing="2" cellpadding="2">'+
        '<tr id="header">'+
        '<td align="left" valign="top" nobreak>'+
        '<form>'+
        '<input type="checkbox" tabindex="-1" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="checkSPEAK()" '+checked+'>&nbsp;Speak&nbsp;'+
        '<input type="checkbox" tabindex="-1" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="checkCASE()">&nbsp;Capitals&nbsp;'+
        '<input type="checkbox" tabindex="-1" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="checkANTS()">&nbsp;Ants'+
        '</form>'+
        '</td>'+
        '<th colspan="2" align="center"><h1 id="hed">&nbsp</h1></th>'+
        '<td id="score" align="right" valign="top" class="sysnote"></td></tr>'+
     /* '<tr height="100"><td id="promptrow" colspan="4" align="center" class="sysnote"><em>Click the "Go Button" to play,<br>then find the matching word.</em></td></tr>'+ */
        '<tr height="100"><td id="promptrow" colspan="4" align="center" class="sysnote"><em>'+ Signon1+'<br>'+Signon2+'</em></td></tr>'+
        '<tr id="wordrow" height="0">'+
        '<td width="25%" align="center" valign="middle"><span id="wrdno0" class="sysnote2"></span>&nbsp;<span id="wrd0" class="textB" style="font-size:150%;cursor:hand" onClick="matchWord(0)"></span></td>'+
        '<td width="25%" align="center" valign="middle"><span id="wrdno1" class="sysnote2"></span>&nbsp;<span id="wrd1" class="textB" style="font-size:150%;cursor:hand" onClick="matchWord(1)"></span></td>'+
        '<td width="25%" align="center" valign="middle"><span id="wrdno2" class="sysnote2"></span>&nbsp;<span id="wrd2" class="textB" style="font-size:150%;cursor:hand" onClick="matchWord(2)"></span></td>'+
        '<td width="25%" align="center" valign="middle"><span id="wrdno3" class="sysnote2"></span>&nbsp;<span id="wrd3" class="textB" style="font-size:150%;cursor:hand" onClick="matchWord(3)"></span></td>'+
        '</tr>'+
        '<tr height="125">'+
        '<td align="center" valign="bottom" colspan="4" id="wrd4" class="textB" style="font-size:150%;cursor:default" onclick="thisWordIs(this.innerText)"></td>'+
        '</tr>'+
        '</table>'+
        '</center>';
    thepage.innerHTML = s;
}

function startup() {
  hed.innerText = LG.Data.title;
  document.body.style.backgroundColor = LG.randbgColor;
  checkCASE();
  checkANTS();
  LG.HotKey[1] = 13; /* add enter to hotkey list */
  LG.HotKey[2] = 49; /* add 1-4 to hotkey list */
  LG.HotKey[3] = 50;
  LG.HotKey[4] = 51;
  LG.HotKey[5] = 52;
  document.onkeypress = LG.doHotKey;
  go0.style.className = 'mouse_Off';
  go0.style.borderColor = document.body.style.backgroundColor
  checkSPEAK();
  if (LG.SpeakOn) {
     MSA.agentSpeak('Welcome!\\pau=300\\'+Signon1+' '+Signon2,true);
  }
}

