/* 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 Instr = 'Click the picture button, or press the space bar, to show the words.';
var Instr2 = 'Use "Browse" to load a new picture.';
var Heading = 'Word and Sentence Practice';
var AltTag  = 'Click this picture button, or press the space bar to show the words or sentences.';
var current_imgsrc = '';
var new_imgsrc = '';
var tout = 0;

function checkSPEAK() {
  LG.SpeakOn = (document.forms[0][0].checked) ? true : false;
}

function doItem() {
  if (document.forms[0][1].checked) { /* Random */
    var n = LG.Counter;
    while (n == LG.Counter) {
      n++;
        n = LG.randInt(LG.Data.split2Array().length-1);
    }
    LG.Counter = n;
  }
  else {
    LG.Counter++;
  }
  LG.Counter = (LG.Counter >= LG.Data.split2Array().length) ? 0 : LG.Counter;
  LG.Counter = (LG.Counter < 0) ? 0 : LG.Counter;
  document.all.sen.innerText = LG.Data.split2Array()[LG.Counter];
  document.all.sen.style.textAlign = (LG.Data.split2Array()[LG.Counter].indexOf(' ') > 0) ? "left" : "center";
  document.all.pic.className = 'button_Dn';
  document.all.pic.alt = '';
  if (LG.SpeakOn) {MSA.agentSpeak(LG.Data.split2Array()[LG.Counter],false)}
  setTimeout(resetBtn,250);
}

function resetBtn() {
  document.all.pic.className = 'button_Up';
}

function makePage(speakOnOff) {
  var checked = (speakOnOff) ? 'checked' : '';
  var s = '<div id="flyout"><center>'+
          '<table border="0" cellspacing="0" cellpadding="0" width="98%">'+
          '<tr><td width="15%" align="center" class="sysnoteB">'+
          '<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</form>'+
          '</td><td align="center"><h1 id="hed"></h1></td></tr></table>'+
          '<table border="0" cellspacing="5" cellpadding="5" width="96%">'+

          '<tr><td align="right" width="1%" style="border:wide;border-color:black;">'+
/*           '<img id="pic" width="320" height="240" style="border:outset thick" onClick="doItem(32);this.style.border=\'inset\'" onmouseover="WC.toggleWC(0)" onmouseout="WC.toggleWC(1);this.style.border=\'outset\'" alt="'+AltTag+'"></td>'+ */
          '<img id="pic" width="320" height="240" class="button_Up" onClick="doItem(32)" onmouseover="WC.toggleWC(0)" onmouseout="WC.toggleWC(1)" alt="'+AltTag+'"></td>'+
          '<th id="sen" width="50%" align="left" valign="middle" class="text" style="background-color:#fffff0;border:double thick #000000"></th></tr>'+

          '<tr><td align="center" class="sysnote2">'+
          '<form name="picselectform">'+
          '<input title="Use this button to load a new picture from your computer" type="file" name="filename" size="30" maxlength="100" onclick="checkChange()" onmouseover="WC.toggleWC(0)" onmouseout="WC.toggleWC(1)">'+
/*        '<input title="Use this button to load a new picture from your computer" type="file" name="filename" size="30" maxlength="100" onclick="checkChange()">'+ */
/*        '<input type="button" value="Open File" onclick="whatFile(this.form)">'+ */
          '</form>'+Instr2+'</td><td></td></tr>'+

          '</table></center>'+

          '</div>';
  document.body.innerHTML = s;
}

var tout = 0;
function checkChange() {
  new_imgsrc = document.picselectform.filename.value.toLowerCase();
  if ((new_imgsrc > '') && (new_imgsrc != current_imgsrc)) {
    var posdot = new_imgsrc.lastIndexOf('.')+1;
    var rest = new_imgsrc.length-posdot;
    var ext = new_imgsrc.substr(posdot,rest);
    if ((ext=='gif') || (ext=='jpg') || (ext=='jpeg')) {
      document.all.pic.src = 'file:///'+new_imgsrc;
      current_imgsrc = new_imgsrc;
      /* adjustSize(); */
      if (tout) {clearTimeout(tout)}
      blur();
    }
  }
  else {
    tout = setTimeout('checkChange()',100);
  }
}

function adjustSize() {
  var x = document.all.pic.width;
  var y = document.all.pic.height;
  if (x > 320) {x =  320};
  if (y > 240) {y =  240};
  document.all.pic.width = x;
  document.all.pic.height = y;
  /* alert(document.all.pic.width +' ' + document.all.pic.height); */
}

function startup() {
  document.body.style.backgroundColor = LG.randbgColor;
  document.all.hed.innerText = LG.Data.title;
  document.all.pic.src = LG.Data.imagesrc;
  /* adjustSize(); */
  document.picselectform.filename.value = document.all.pic.src;
  current_imgsrc = document.all.pic.src;
  new_imgsrc = current_imgsrc;
  if (LG.Firsttime) {
    document.all.sen.innerHTML = '<span class="sysnote"><em>'+Instr+'</em></span>';
    LG.Firsttime = false;
  }
  document.onkeypress = LG.doHotKey;
  checkSPEAK();
  if (LG.SpeakOn) {
     MSA.agentSpeak('Welcome!\\pau=300\\'+Instr,true);
  }
}
