// Scripts for the scripture challenge box.

var scripture_activated = false;
var displayed_text = '';
var target_text = '';

function show_ref(url) {
  window.open(url, '', 'width=640, height=480, resizable, scrollbars, status');
  return false;
}

function addBlanks(text, frequency, allow_digits) {
  var c, i, j, w;
  var words=text.replace(/_/g, '-').split(" ");
  var orig='', blanked='';

  if (!frequency)
    frequency = 0.25;
  for (i = 0; i < words.length; i++) {
    word = words[i];
    w = word;
    if (Math.random() <= frequency) {
      w = '';
      for (j = 0; j < word.length; j++) {
        c = word.charAt(j);
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ||
            (allow_digits && c >= '0' && c <= '9')) {
          c = '_';
        }
        w = w + c;
      }
    }
    if (orig)
      orig = orig + ' ' + word;
    else
      orig = word;
    if (blanked)
      blanked = blanked + ' ' + w;
    else
      blanked = w;
  }
  return new Array(orig, blanked);
}

function changeDisplayedText(s) {
  var i, pos, fillin = document.getElementById("scripture-fillin");
  var line, lines, show_cursor=scripture_activated;

  for (i = fillin.childNodes.length - 1; i >= 0; i--) {
    fillin.removeChild(fillin.childNodes[i]);
  }

  lines = s.split('\n');
  for (i = 0; i < lines.length; i++) {
    line = lines[i];
    pos = line.indexOf('_');
    if (show_cursor && pos >= 0) {
      var cursor;
      show_cursor = false;
      fillin.appendChild(document.createTextNode(line.slice(0, pos)));
      cursor = document.createElement('u');
      cursor.appendChild(document.createTextNode('*'));
      fillin.appendChild(cursor);
      fillin.appendChild(document.createTextNode(line.slice(pos + 1)));
    }
    else
      fillin.appendChild(document.createTextNode(line));
    fillin.appendChild(document.createElement('br'));
  }
  displayed_text = s;
}

function keyPressed(e) {
  var c, need_c, pos;
  if (!scripture_activated)
    return true;  // Propagate.
  if (!e)
    e = event;
  pos = displayed_text.indexOf('_');
  if (pos >= 0) {
    // Moz uses charCode.  IE uses keyCode.  No standard exists yet.
    c = String.fromCharCode(e.charCode || e.keyCode).toLowerCase();
    if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') {
      need_c = target_text.charAt(pos);
      if (c == need_c.toLowerCase()) {
        var new_s = (target_text.slice(0, pos) + need_c
                     + displayed_text.slice(pos + 1));
        changeDisplayedText(new_s);
        if (new_s.indexOf('_') < 0) {
          window.alert("Congratulations!");
        }
      }
      else {
        // boo
      }
      // Don't allow Mozilla's typeahead until the scripture is completed.
      if (e.stopPropagation)
        e.stopPropagation();
      return false;
    }
  }
}

function activateScripture(elem) {
  scripture_activated = true;
  elem.style.backgroundColor = '';
  changeDisplayedText(displayed_text);
  if (document.addEventListener)
    document.addEventListener('keypress', keyPressed, false);
  else
    document.onkeypress = keyPressed;
}
