/* -*- C++ -*- */

$(document).ready(function(){

    $('#record_button').click(function(){
        Recorder.record('audio', 'audio.wav');
      });

    $('#play_button, #stop_button').click(function(){
        Recorder.playBack('audio');
      });

    $('#discard_button').click(function(){
        $('#discard_button, #play_button, #stop_button').hide();
        Recorder.recorder_handle.hide();
        mic_event_handler('microphone_connected');
      });

  });

function mic_event_handler ()
{
  //console.log(arguments[0]);

  switch (arguments[0]) {

   case 'ready':
    Recorder.connect('recorder_widget', 0);
    break;

   case 'no_microphone_found':
    $('#pronunciation_tooltip').text('We were unable to find a suitable microphone to use.');
    break;

   case 'microphone_user_request':
    $('#pronunciation_tooltip').text('You must allow Flash to access your microphone in order to continue.');
    if (!Recorder.has_permission) {
      Recorder.showPermissionWindow();
    }
    else {
      mic_event_handler('microphone_connected');
    }
    break;

   case 'microphone_connected':
    $('#pronunciation_tooltip').text('You may now record a pronunciation. Click the record button to start recording.');
    $('#save_button').addClass('button_sized').removeClass('permission_sized');
    $('#record_button').show();
    Recorder.has_permission = true;
    break;

   case 'microphone_not_connected':
    $('#pronunciation_tooltip').text('It appears your microphone is not connected.');
    $('#save_button').addClass('button_sized').removeClass('permission_sized');
    break;

   case 'recording':
    Recorder.has_recorded = true;
    Recorder.recorder_handle.hide();
    $('#play_button').hide();
    $('#record_button').addClass('recording');
    $('#pronunciation_tooltip').html('<strong style="color: red">RECORDING!</strong> Press the Record button again to stop recording.');
    break;

   case 'recording_stopped':
    $('#pronunciation_tooltip').text('Recording has completed. Please review your pronunciation by pressing the Play button.');
    $('#record_button').removeClass('recording').hide();
    $('#play_button, #save_button, #discard_button').show();
    Recorder.recorder_handle.show();
    break;

   case 'playing':
    $('#pronunciation_tooltip').text('Playing your pronunciation...');
    $('#play_button').hide();
    $('#stop_button').show();
    break;

   case 'playback_started':
    break;

   case 'stopped':
    $('#stop_button').hide();
    $('#play_button').show();
    $('#discard_button').show();
    $('#pronunciation_tooltip').text('Press the save button if you are satisfied with your pronunciation, or press discard to record a new one.');

    break;

   case 'save_pressed':
    $('#pronunciation_tooltip').text('Saving your pronunciation...');
    Recorder.updateForm();
    break;

   case 'saving':
    break;

   case 'saved':
    var name = arguments[1];
    var data = $.parseJSON(arguments[2]);

    if(data.result && data.result == 'success') {
      $('#pronunciation_tooltip').html('Saving your pronunciation... <strong class="success">SUCCESS</strong>');

      /**
       * Add the identifier to the form so it gets sent with this edit.
       */

      $('#audio_file').val(data.identifier);
      $('#play_button').hide();
      Recorder.recorder_handle.hide();

      setTimeout(function(){ $('#pronunciation_tooltip').html('Your pronunciation will be included in your submission. Click the Discard button if you want to record a new pronunciation.'); }, 1000);

    }
    else {
      $('#pronunciation_tooltip').html('Saving your pronunciation... <strong class="failure">FAILURE</strong>');
    }
    break;

   case 'save_failed':
    var name = arguments[1];
    var errorMessage = arguments[2];

    $('#pronunciation_tooltip').html('Saving your pronunciation... <strong class="failure">FAILURE</strong>. Error: ' + errorMessage);
    break;

   case 'save_progress':
    var name = arguments[1];
    var bytesLoaded = arguments[2];
    var bytesTotal = arguments[3];
    $('#pronunciation_tooltip').html('Saving your pronunciation... <strong>' + bytesLoaded + 'kB</strong> loaded.');
    break;
  }
}

Recorder = {
  recorder_handle: null,
  upload_form: '#audio_form',
  has_recorded: false,
  has_permission: false,
  upload_field: 'audio_file',

/**
 * Connect the flash interface
 */

  connect: function(name, attempts)
  {
    Recorder.recorder_handle = document[name];

    if(attempts >= 100) {
      return;
    }

    /**
     * Once flash app is initialized, show the permission dialog.
     */

    if (Recorder.recorder_handle && Recorder.recorder_handle.init) {
      var audio_form = $('#audio_form');


      Recorder.recorder_handle.init(audio_form.attr('action').toString(), Recorder.upload_field, audio_form.serializeArray());
      mic_event_handler('microphone_user_request');

      //Recorder.showPermissionWindow();
      return;
    }

    setTimeout(function() {Recorder.connect(name, attempts+1);}, 50);
  },

/**
 * Show the security dialog that asks if Flash can access the microphone.
 */

  showPermissionWindow: function()
  {

    /**
     * Resize the Flash applet so the security check will fit.
     */

    Recorder.recorder_handle.width  = '240px';
    Recorder.recorder_handle.height = '160px';

    /**
     * Avoid resizing race conditions.
     */

    setTimeout(function(){Recorder.recorder_handle.permit();}, 1);
  },

/**
 * Play a recording
 */

  playBack: function(name) {
    Recorder.recorder_handle.playBack(name);
  },

  record: function(name, filename) {
    Recorder.recorder_handle.record(name, filename);
  },

  show: function()
  {
    Recorder.recorder_handle.show();
  },

  hide: function()
  {
    Recorder.recorder_handle.hide();
  },

  updateForm: function()
  {
    Recorder.recorder_handle.update(Recorder.upload_form.serializeArray());
  }
}


