google.load("elements", "1", {
  packages: 'transliteration'
  });

//google.load("language", "1"):

$(document).ready(function() {
  var transElem  = $('#id_translations-TOTAL_FORMS').parent();
  transElem.remove();
  var firstFormElem = $('.form-row:first');
  firstFormElem.append(transElem);
    //Add on blur to english field
  $('#id_translations-0-firstname').blur(function() {
    var strArr = $(this).val().split(" ");
    google.language.transliterate(strArr, "en", "hi", function(result) {
      if (!result.error) {
        console.log(result);
        }
      });
    });


  $.getJSON("/getLanguages", {}, function(data) {
    var myLanguages = data;
    var destLangs = [];
    var allTransIds = [];
    for (var l in myLanguages) {
      if (l >= 1) {
        var lShort = myLanguages[l]['short']
        destLangs.push(lShort);
        var thisForm = transElem.find('.inline-related').eq(l);
        thisForm.find('textarea, input').each(function() {
          if ($(this).attr("type") != "hidden") {
            var thisId = $(this).attr('id');
            allTransIds.push(thisId);
            $(this).attr("data-lang", lShort);
            $(this).focus(function() {
              var lang = $(this).attr("data-lang");
              GControl.setLanguagePair("en", lang);              
              });
            }
          });
        }
    
      }
    var options = {
      sourceLanguage: 'en',
      destinationLanguage: destLangs,
      transliterationEnabled: true    
      }
    GControl = new google.elements.transliteration.TransliterationControl(options);
    GControl.makeTransliteratable(allTransIds);
    });
  });
