/**
 * I18N: function for translating UI strings. Not really functional yet.
 */
function t(str) {
  return str;
}

g_drills = {
  '+': {
    easy: {
      title: t('Easy'),
      desc: '',
      func: 'g_addition'
    }
  },
  '-': {
    easy: {
      title: t('Easy'),
      desc: '',
      func: 'g_substraction'
    }
  }
};

$(function() {

  // Populate the subject (aka 'Operations') dropdown.
  jQuery.each(g_drills, function(subject, rest) {
    $('#subject').append($('<option>').text(subject));
  });
  
  // Populate the drills (aka 'Levels') dropdown.
  $('#subject').change(function() {
    var drills= g_drills[$(this).val()];
    $('#drill').empty();
    jQuery.each(drills, function(drill_id, info) {
      $('#drill').append($('<option>').attr('value', drill_id).text(info.title));
    });
    $('#drill').change();
  }).change();

  // Make the drills dropdown print descriptions.
  $('#drill').change(function() {
    var info = g_drills[ $('#subject').val() ][ $(this).val() ];
    $('#drill-description').html(info.desc || '');
  }).change();

  $('#generate').click(function() {
     var info = g_drills[ $('#subject').val() ][ $('#drill').val() ];
     result = window[info.func]();
     $('#drill-output').html(result.text);
     if (result.number) {
       ab1.setNumber(result.number);
       ab1.unscheduleAll();
       ab1.history.appendSeparator(t('---New exercise--'));
       ab1.recordInHistory();
     }
  });
});

/**
 * Generates a number between min and max, inclusively.
 */
function rand(min, max) {
  return min + Math.round(Math.random() * (max - min));
}

function g_addition() {
  var a = rand(90, 999);
  var b = rand(10, 150);
  var text = a + ' + ' + b + ' = ' + (a + b);
  return { number: a, text: text };
}

function g_substraction() {
  var a = rand(90, 999);
  var b = rand(1, a);
  var text = a + ' - ' + b + ' = ' + (a - b);
  return { number: a, text: text };
}

