// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* google translate functions */

function lltranslate(source_text, destination_form_element, from_lang, to_lang) {
  // alert("source: " + source_text);
  google.language.translate(source_text, from_lang, to_lang, function(result) {
      var translated = document.getElementById(destination_form_element);
      if (result.translation) {
          // alert(result.translation);
          translated.value = result.translation;
      }
  });
}

function lltranslate_div(source_id, destination_form_element, from_lang, to_lang) {
  google.language.translate($(source_id).value, from_lang, to_lang, function(result) {
      var translated = document.getElementById(destination_form_element);
      if (result.translation) {
          // alert(result.translation);
          translated.value = result.translation;
      }
  });
  
}

/* flash card functions */

function flip() {
  FLIP_STATE = 1;
 $('other_side').show();
 $('flip_button').hide();
 $('answer_buttons').show(); 
 $('extras').show(); 
}

function onKeyup(e) {
  if (ADD_NOTE_STATE == 1) {
    return;
  }
  var character = String.fromCharCode(e.keyCode);
  if ((character == 'f') || (character == 'F') || (character == ' ')) {
    if (FLIP_STATE == 0) {
      flip();
    }
  }
  else if ((character == 'y') || (character == 'Y') || (character == 'r') || (character == 'R')) {
    if (FLIP_STATE == 1) {
      answer_yes();
    }
  }
  else if ((character == 'n') || (character == 'N') || (character == 'w') || (character == 'W')) {
    answer_no();
  }
}

function genderOnKeyup(e) {
  var character = String.fromCharCode(e.keyCode);
  if (FLIP_STATE == 1) {
    if (character == ' ') {
      next_card();
    }
  }
  else if (FLIP_STATE == 0) {
    if ((character == 'm') || (character == 'M')) {
      answer_masc();
    }
    else if ((character == 'n') || (character == 'N')) {
      answer_neut();
    }
    else if ((character == 'f') || (character == 'F')) {
      answer_fem();
    }
  }
}

function show_loader() {
  $('loader').show();
}

function hide_loader() {
  $('loader').hide();
}

/* table row functions */

// otype is the object type.  It should be "word", "sentence", oder "page"
function highlight_row(id, cb_id, otype) {
  if ($(cb_id).checked == true) {
    $(id).removeClassName(otype);
    $(id).addClassName(otype + "_selected");  }
  else {
    $(id).removeClassName(otype + "_selected");
    $(id).addClassName(otype);
  }
}

// otype is the object type.  It should be "word", "sentence", oder "page"
function update_highlights(otype) {
  var elist = document.getElementsByClassName("cb");
  for (x = 0; x < elist.length; x++) {
    var id = elist[x].id.split("_")[2];
    highlight_row(otype + "_" + id, otype + "_ids_" + id, otype);
  }
}

// otype is the object type.  It should be "word", "sentence", oder "page"
function select_all(otype) {
  var elist = document.getElementsByClassName("cb");
  // alert(elist);
  for (x = 0; x < elist.length; x++) {
    elist[x].checked = true;
    var id = elist[x].id.split("_")[2];
    highlight_row(otype + "_" + id, otype + "_ids_" + id, otype);
  }
}

// otype is the object type.  It should be "word", "sentence", oder "page"
function select_none(otype) {
  var elist = document.getElementsByClassName("cb");
  // alert(elist);
  for (x = 0; x < elist.length; x++) {
    elist[x].checked = false;
    var id = elist[x].id.split("_")[2];
    highlight_row(otype + "_" + id, otype + "_ids_" + id, otype);
  }
}

function check_part_of_speech() {
  //alert("part of speech: " + $('word_part_of_speech').value);
  if ($('word_part_of_speech').value == 'Noun') {
    $('word_gender').show();
  }
  else {
    $('word_gender').hide();
  }
}

