var praepositionen = new Array("an (D)","an (A)","auf (A)","aus","bei","für","gegen","in (D)","mit","nach","über (A)","um","unter (D)","von","vor (D)", "zu");
var an_d=0, an_a=1, auf_a=2, aus=3, bei=4, fuer=5, gegen=6, in_d=7, mit=8, nach=9, ueber_a=10, um=11, unter_d=12, von=13, vor_d=14, zu=15;
var wordGender = [
  ["abhängen", [von]],
  ["achten", [auf_a]],
  ["anfangen", [mit]],
  ["antworten", [auf_a]],
  ["aufhören", [mit]],
  ["beginnen", [mit]],
  ["berichten", [ueber_a]],
  ["bestehen", [aus]],
  ["bitten (A)", [um]],
  ["danken (D)", [fuer]],
  ["denken", [an_a]],
  ["diskutieren (mit)", [ueber_a]],
  ["eintreten", [fuer]],
  ["erkennen (A)", [an_d]],
  ["es fehlt (D)", [an_d]],
  ["es geht", [um]],
  ["es handelt sich", [um]],
  ["es kommt an", [auf_a]],
  ["es liegt", [an_d]],
  ["fliehen", [vor_d]],
  ["fragen (A)", [nach]],
  ["gehören", [zu]],
  ["glauben", [an_a]],
  ["gratulieren (D)", [zu]],
  ["halten (A)", [fuer]],
  ["handeln", [mit]],
  ["hoffen", [auf_a]],
  ["klagen", [ueber_a]],
  ["kämpfen", [fuer, gegen]],
  ["lachen", [ueber_a]],
  ["leiden", [an_d, unter_d]],
  ["nachdenken", [ueber_a]],
  ["protestieren", [gegen]],
  ["schützen (A)", [vor_d]],
  ["sich aufregen", [ueber_a]],
  ["sich bedanken (bei)", [fuer]],
  ["sich bedanken (für)", [bei]],
  ["sich befassen", [mit]],
  ["sich bemühen", [um]],
  ["sich beschweren (bei)", [ueber_a]],
  ["sich beschweren (über)", [bei]],
  ["sich beschäftigen", [mit]],
  ["sich bewerben", [um]],
  ["sich entscheiden", [fuer]],
  ["sich entschließen", [zu]],
  ["sich entschuldigen (bei)", [fuer]],
  ["sich entschuldigen (für)", [bei]],
  ["sich erinnern", [an_a]],
  ["sich erkundigen", [nach]],
  ["sich freuen", [auf_a, ueber_a]],
  ["sich fürchten", [vor_d]],
  ["sich gewöhnen", [an_a]],
  ["sich informieren", [ueber_a]],
  ["sich interessieren", [fuer]],
  ["sich irren", [in_d]],
  ["sich konzentrieren", [auf_a]],
  ["sich sorgen", [um]],
  ["sich spezialisieren", [auf_a]],
  ["sich trennen", [von]],
  ["sich unterhalten", [ueber_a]],
  ["sich unterscheiden", [von]],
  ["sich verabschieden", [von]],
  ["sich verlassen", [auf_a]],
  ["sich vorbereiten", [auf_a]],
  ["sich wenden", [an_a, gegen]],
  ["sich wundern", [ueber_a]],
  ["sich ärgern", [ueber_a]],
  ["sorgen", [fuer]],
  ["sprechen (mit)", [ueber_a]],
  ["sterben", [an_d]],
  ["stoßen", [auf_a]],
  ["teilnehmen", [an_d]],
  ["trauern", [um]],
  ["vergleichen (A)", [mit]],
  ["verstoßen", [gegen]],
  ["verwechseln (A)", [mit]],
  ["verzichten", [auf_a]],
  ["warnen (A)", [vor_d]],
  ["warten", [auf_a]],
  ["zusammenstoßen", [mit]],
  ["zweifeln", [an_d]],
  ["überreden (A)", [zu]]
  ];

var totalWordsCount = wordGender.length;
var correctAnswerCount = new Array(totalWordsCount);
var pendingWordsCount = totalWordsCount;
var pendingWords = new Array(totalWordsCount);
for (var i = 0; i < pendingWordsCount; ++i)
{
  correctAnswerCount[i] = 0;
  pendingWords[i] = i;
}
var glossary = new Array();

glossary["es"] = new Array();
glossary["es"]["all_words_known"]
= "Ya te sabes todas las palabras en este nivel. ;)";
glossary["es"]["next_level"]
= "Nivel siguiente";
glossary["es"]["this_is_the_last_level"]
= "No hay más niveles disponibles.";
glossary["es"]["level_finished"]
= "Has contestado correctamente todas las preguntas. ¡Buen trabajo!";
glossary["es"]["dictionary"]
= "Diccionario (inglés)";
glossary["es"]["dictionary_url_prefix"]
= "http://dict.tu-chemnitz.de/dings.cgi?glossary=&noframes=1&query=";
glossary["es"]["dictionary_url_suffix"]
= "&service=de-en&optword=1&optcase=1&opterrors=1&optpro=0&dlink=self";

glossary["en"] = new Array();
glossary["en"]["all_words_known"]
= "You already know all the words in this level. ;)";
glossary["en"]["next_level"]
= "Next level";
glossary["en"]["this_is_the_last_level"]
= "There are no more available levels.";
glossary["en"]["level_finished"]
= "You have answered correctly all the questions. Well done!";
glossary["en"]["dictionary"]
= "Dictionary";
glossary["en"]["dictionary_url_prefix"]
= "http://dict.tu-chemnitz.de/dings.cgi?glossary=&noframes=1&query=";
glossary["en"]["dictionary_url_suffix"]
= "&service=de-en&optword=1&optcase=1&opterrors=1&optpro=0&dlink=self";

function updatePendingWords(object)
{
  pendingWordsCount = 0;
  for (var i = 0; i < totalWordsCount; ++i)
    {
      if (correctAnswerCount[i] < 2)
        {
          pendingWords[pendingWordsCount] = i;
          ++pendingWordsCount;
        }
    }
  object.ownerDocument.getElementById("pending_words").innerHTML
    = pendingWordsCount;
  if (0 == pendingWordsCount)
    {
      alert(glossary[lang]["level_finished"]);
    }
}

function correct(object, index)
{
  object.parentNode.style.backgroundColor="#33FF33";
  if (object.style.backgroundColor == "#33FF33") return false;
  object.style.backgroundColor="#33FF33";
  ++correctAnswerCount[index];
  updatePendingWords(object);
  return false;
}

function incorrect(object, index)
{
  object.parentNode.style.backgroundColor="#FF3333";
  if (object.style.backgroundColor == "#FF3333") return false;
  object.style.backgroundColor="#FF3333";
  object.style.textDecoration="line-through";
  --correctAnswerCount[index];
  updatePendingWords(object);
  return false;
}

function help(object, index, helpText)
{
  --correctAnswerCount[index];
  updatePendingWords(object);
  alert(helpText);
}

function newQuestions(questionsPerPage, object)
{
  object.innerHTML = makeButtonList(questionsPerPage);
}

function makeButton(pendingWordsIndex)
{
  var index;
  if (pendingWordsIndex < pendingWordsCount)
    {
      index = pendingWords[pendingWordsIndex];
    }
  else
    {
      alert("Internal problem. Continuing.");
      index = 0;
    }
  var word;
  var gender;
  word   = wordGender[index][0];
  gender = wordGender[index][1];
  var genderMask;
  var correctAnswer;
  var button = "";
  button += "<tr>";
  genderMask = 1;// 001
  //button += "<select onchange='javacript:this.options[this.selectedIndex].onclick()'>";
  button += "<select>";
  correctAnswer = "";
  for (var p in gender)
    {
      if (correctAnswer) correctAnswer += "/";
      correctAnswer += praepositionen[gender[p]];
    }
  correctAnswer = word + " " + correctAnswer;
  button += "<option style='background-color: #CCCCCC' onclick='javascript:help(this, " + index + ", \"" + correctAnswer + "\");'>" + word + " ...?</option>";
  for (var i = 0; i < praepositionen.length; ++i)
    {
      var valid = false;
      for (p in gender) valid |= (gender[p] == i);
      if (valid)
        {
          button += "<option style='background-color: #FFFFFF' onclick='javascript:return correct(this, "
            + index + ");'> " + word + " " + praepositionen[i] + " </option>";
        }
      else
        {
          button += "<option style='background-color: #FFFFFF' onclick='javascript:return incorrect(this, "
            + index + ");'> " + word + " " + praepositionen[i] + " </option>";
        }
      genderMask = genderMask << 1;
    }
  button += "</select>";
  button += "</td>";
  button += "<td><a href='" + glossary[lang]["dictionary_url_prefix"] + word + glossary[lang]["dictionary_url_suffix"] +"'>" + glossary[lang]["dictionary"] + "</a></td>";
  button += "</tr>";

  return button;
}

function check(option)
{
}


function makeButtonList(questionsPerPage)
{
  var i;
  var now;
  now = new Date();
  var index;
  var buttons = "";
  if (pendingWordsCount > 0)
    {
      if (questionsPerPage > pendingWordsCount)
        {
          questionsPerPage = pendingWordsCount;
        }
      buttons += "<table>";
      for (i = 0; i < questionsPerPage; ++i)
        {
          index = Math.round(Math.random() * (pendingWordsCount - 0.5));
          buttons += makeButton(index);
        }
      buttons += "</table>";
    }
  else
    {
      buttons = "<b>" + glossary[lang]["all_words_known"] + "</b>";
    }

  return buttons;
}

