// Settings variables
var error = 0;
var stage = 2;
var fading = 0;
var types = new Array();
var ids = new Array();
types[0] = 1; ids[0] = "ar_pres";
types[1] = 1; ids[1] = "ar_prog";
types[2] = 1; ids[2] = "er_pres";
types[3] = 1; ids[3] = "er_prog";
types[4] = 1; ids[4] = "comidas";
types[5] = 1; ids[5] = "estar";

// Person types - non changing
var persons = new Array(); persons[0] = "yo"; persons[1] = "tu"; persons[2] = "el/ella"; persons[3] = "nosotros"; persons[4] = "ellos/ellas";
// Forms of estar - non changing
var estar = new Array(); estar[0] = "estoy"; estar[1] = "estas"; estar[2] = "esta"; estar[3] = "estamos"; estar[4] = "estan";

// AR verb Stems
var arv = new Array(); arv[0] = "habl"; arv[1] = "trabaj"; arv[2] = "nad"; arv[3] = "visit"; arv[4] = "estudi";
// AR verb endings - non changing
var arpres = new Array(); arpres[0] = "o"; arpres[1] = "as"; arpres[2] = "a"; arpres[3] = "amos"; arpres[4] = "an";

// ER verb Stems
var erv = new Array(); erv[0] = "tej"; erv[1] = "com";
// ER verb endings - non changing
var erpres = new Array(); erpres[0] = "o"; erpres[1] = "es"; erpres[2] = "e"; erpres[3] = "emos"; erpres[4] = "en";

// Foods for game type 3
var food_es = new Array();
var food_en = new Array();
food_en[0] = "chicken"; food_es[0] = "pollo";
food_en[1] = "water";   food_es[1] = "agua";
food_en[2] = "meat";    food_es[2] = "carne";
food_en[3] = "cheese";  food_es[3] = "queso";
food_en[4] = "rice";    food_es[4] = "arroz";


// FUNCTIONS --------------------------------------------------------------------------------------------------------

function gid(id) { return document.getElementById(id); }
function start() { next(); }

function toggle(which) {
  types[which] = 1 - types[which];
  newclass = (types[which] == 1) ? "enabled" : "disabled";
  gid(ids[which]).setAttribute("class",newclass);

  errchk();

  if (error == 0) {
    stage = 0;
    gid("question").style.opacity = 0;
    gid("answer").style.opacity = 0;
    newq();
  }
}

function enable() { for (j=0; j<types.length; j++) { toggle(j); } }

function errchk() {
  error = 0;
  count = 0;
  for (i=0; i<types.length; i++) { count += types[i]; }
  gid("warn1").style.display = (count == 0) ? "block" : "none";
  if (count == 0) { error = 1; }
}

function randint(a,b) {
  c = (b-a) + 1;
  return Math.ceil(Math.random() * c) + a - 1;
}

function newq() {
  do {
    gametype = randint(1,types.length) - 1;
  } while (types[gametype] == 0);

  switch (gametype) {
  case 0:
    key = randint(0,4);
    verb = arv[randint(1,arv.length)-1];
    gid("question").innerHTML = "Present<br/>" + persons[key] + "/" + verb + "ar";
    gid("answer").innerHTML = verb + arpres[key];
    break;
  case 1:
    key = randint(0,4);
    verb = arv[randint(1,arv.length)-1];
    gid("question").innerHTML = "Present Progressive<br/>" + persons[key] + "/" + verb + "ar";
    gid("answer").innerHTML = estar[key] + " " + verb + "ando";
    break;
  case 2:
    key = randint(0,4);
    verb = erv[randint(1,erv.length)-1];
    gid("question").innerHTML = "Present<br/>" + persons[key] + "/" + verb + "er";
    gid("answer").innerHTML = verb + erpres[key];
    break;
  case 3:
    key = randint(0,4);
    verb = erv[randint(1,erv.length)-1];
    gid("question").innerHTML = "Present Progressive<br/>" + persons[key] + "/" + verb + "er";
    gid("answer").innerHTML = estar[key] + " " + verb + "iendo";
    break;
  case 4:
    whichway = randint(0,1);
    key = randint(1,food_es.length)-1;
    q = (whichway == 0) ? food_es[key] + " en ingles" : food_en[key] + " en espanol";
    a = (whichway == 1) ? food_es[key] : food_en[key];
    gid("question").innerHTML = "Como se dice<br/>" + q;
    gid("answer").innerHTML = a;
    break;
  case 5:
    key = randint(0,4);
    gid("question").innerHTML = "Estar:<br/>" + persons[key];
    gid("answer").innerHTML = estar[key];
    break;
  }
}

function next() {
  if ((error == 0) && (fading == 0)) {
    switch (stage) {
    case 0:
      stage = 1;
      fade("question",1,0);
      break;
    case 1:
      stage = 2;
      fade("answer",1,0);
      break;
    case 2:
    default:
      stage = 0;
      setOpacity("question", 0);
      setOpacity("answer", 0);
      newq();
      break;
    }
  }
}

function fade(id, io, step) {
  fading = 1;

  step = (io == 0) ? step - 0.05 : step + 0.05;
  setOpacity(id, step);

  if (Math.round(step * 100) == (io * 100)) { fading = 0; }
  else { timer = setTimeout("fade('" + id + "'," + io + "," + step + ");",35); } 
}

function setOpacity(id, amt) {
  gid(id).style.filter = "alpha(opacity=" + (amt * 100) + ")";
  gid(id).style.opacity = amt;
}
