/* 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 Signon       = 'Click these "VCR Buttons" to go to different pages of words.'
var Heading      = '';
var li_style     = '"line-height:125%;list-style:disc"';
var book_style   = '"border-color:#666666;border-style:groove;background:url(common/spirbook.gif)"';
var LastItem     = 0;
var INTERVAL     = 2000; /* 2 secs - enough for most words */
var IntervalID   = -1;
var WordsPerPage = 10; /* Max words allowed per page - more will go on inserted pages */

function doItem() { /* Allow hotkey control of VCR */
  doVCR();
}

function doSpeak() {
  LG.SpeakOn = (document.forms[0][0].checked) ? true : false;
  if (IntervalID) {clearInterval(IntervalID)}
  if (LG.SpeakOn) {IntervalID = setInterval('speakWord()',INTERVAL)}
}

function speakWord() {
  /* window.status = LastItem; */
  if (LastItem >= 0) {
    var wordsA  = LG.Data.split2Array()[LG.Counter].split(LG.Data.FieldSep);
    for (var i=0; i<wordsA.length; i++) {
      var lx = eval('document.all.list'+i);
      lx.style.fontWeight ='normal';
      lx.style.listStyle  = 'disc';
      lx.style.textDecoration  = 'none';
    }
    if (LastItem < wordsA.length) {
      var w = eval('document.all.list'+LastItem).innerText;
      if (w > '') {
        lx = eval('document.all.list'+LastItem);
        lx.style.fontWeight ='bold';
        lx.style.listStyle  = 'circle';
        lx.style.textDecoration  = 'underline';
        if (LG.SpeakOn) {MSA.agentSpeak(wordsA[LastItem],false)}
      }
      LastItem++;
    }
    else {
      LastItem = 0;
    }
  }
}

function doVCR(n) {
  switch (n) {
    case 0: {
      LG.Counter = 0;
      break;
    }
    case 1: {}
    case -1: {
      LG.Counter += n;
      LG.Counter = LG.minMax(LG.Counter,0,LG.Data.split2Array().length-1);
      break;
    }
    case 2: {
      LG.Counter = LG.Data.split2Array().length-1;
      break;
    }
    default: {
      LG.Counter++;
      if (LG.Counter >= LG.Data.split2Array().length) {LG.Counter = 0}
      break;
    }
  }
  updatePage();
  LastItem = 0;
  doSpeak();
}

function setupLists() {
  var W = LG.Data.split2Array();               /* Temp array of word sets */
  var T = new Array(); var i = j = 0;          /* Working variables */
  var regexp = '([^\\'+LG.Data.FieldSep+']+';  /* Assemble a regexp for field separators */
  for (i=0; i<WordsPerPage-1; i++) {           /* according to max number of words per book page */
    regexp += '[\\'+LG.Data.FieldSep+'][^\\'+LG.Data.FieldSep+']+';
  }
  regexp += ')[\\'+LG.Data.FieldSep+']';
  regexp = new RegExp(regexp,"g");             /* And make a regexp object */
  for (i=0; i<W.length; i++) {                 /* Replace every n'th field sep with a record sep */
    W[i] = W[i].replace(regexp,"$1"+LG.Data.RecordSep);
    for (j=0; j<=W[i].getCount(LG.Data.RecordSep); j++) { /* At the same time, look at page titles */
      T[T.length] = LG.Data.split2Array('subtitles')[i];  /* - keep (or reuse if a "page break" has been added) */
    }
  }
  LG.Data.data      = W.join(LG.Data.RecordSep); /* Recreate string with at least the original record breaks */
  LG.Data.subtitles = T.join(LG.Data.FieldSep);  /* Save (with revisons) the subtitles string */
}

function updatePage() {
  var chapter = LG.Data.split2Array('subtitles')[LG.Counter]; /* The current title for this page */
      chapter = (chapter > '') ? chapter : 'Chapter';         /* - or at least something */
  var whitespace = new RegExp(' +','g');
      chapter = chapter.replace(whitespace,'&nbsp;');
  /* Get the current word list for this page */
  var wordsA  = LG.Data.split2Array()[LG.Counter].split(LG.Data.FieldSep);
  var s  = '<ul>'; /* Make a html lists */
  for (var i=0; i<LG.minMax(wordsA.length,0,10); i++) {
     s += (typeof(wordsA[i]) != 'undefined') ? '<li id="list'+i+'" style='+li_style+'">' + wordsA[i] : '';
  }
  s += '</ul>';    /* Finished list - now write the required page */
  if (!LG.Firsttime) {
    document.all.prompt.innerHTML = '';
    LG.Firsttime = false;
  }
  LG.Firsttime = false;
  document.all.ltitle.innerHTML = '<h2>'+Heading+'</h2>';
  document.all.rtitle.innerHTML = '<h3>'+(LG.Counter+1) +'.&nbsp;'+chapter+'</h3>';
  document.all.rpage.innerHTML =  s;
}

function makePage() {
var checked = (LG.Data.speak) ? 'checked' : '';
var s = '<div id="thebook" style="width:100%"><center>'+
        '<table border="0" width="100%" cellspacing="2" cellpadding="2">'+
        '<tr><td width="1%"><form>'+
        '<input type="checkbox" onMouseOver="WC.toggleWC(0)" onMouseOut="WC.toggleWC(1)" onClick="doSpeak()" '+checked+'>&nbsp;Speak&nbsp;'+
        '</form></td>'+
        '<td align="center"><h1>Word Book</h1></td></tr></table>'+
        '<table id="booktable" width="660" border="5" cellpadding="5" rules="none" style='+book_style+'>'+
        '<tr><th id="ltitle" class="titleB" width="280" height="20" align="center" valign="top">'+
        '<td td width="40">&nbsp;</td>'+
        '<th id="rtitle" class="titleB" width="280" height="20" align="center" valign="middle"></th></tr>'+
        '<tr><td id="lpage" class="text" width="280" height="300" align="center" valign="bottom">'+
        '<p id="prompt" class="sysnote"><em>'+Signon+'</em><br>&nbsp;</p>'+
        LG.vcr+'</td>'+
        '<td>&nbsp;</td>'+
        '<td id="rpage" class="text" width="280" align="left" valign="top"></td></tr>'+
        '</table></center></div>';
  document.body.innerHTML = s;
}

function startup() {
  setupLists(); /* Examines data to make word arrays, re-page long word lists etc */
  document.body.style.backgroundColor = LG.randbgColor;
  document.all.booktable.style.backgroundColor = LG.matchColor(document.body.style.backgroundColor);
  document.onkeypress = LG.doHotKey;
  document.all.vcr0.style.borderColor = document.all.booktable.style.backgroundColor;
  document.all.vcr1.style.borderColor = document.all.booktable.style.backgroundColor;
  document.all.vcr2.style.borderColor = document.all.booktable.style.backgroundColor;
  document.all.vcr3.style.borderColor = document.all.booktable.style.backgroundColor;
  doVCR(0);
}

