/**
 * Copyright (c) 2015, Samsung Electronics Co., Ltd
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *  * Neither the name of Samsung Electronics nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @file example.js
 * @brief Example specific UI configuration.
 * Variables below will configure this demo example for it needs after document
 * load finishes.
 * @see common.js for more details.
 */

var demo_name = "Audio";
var demo_description = "The Audio example demonstrates how to use " +
        "pp::Audio interface.<br> Three WAVE files can be loaded and played." +
        "Also, there is a simple mixer implemented, so loaded sounds can be " +
        "played simultaneously.<br>" +
        "Files are read from <i>sounds</i> folder in widget folder. " +
        "In the text fields input file names and click appropriate <b>Load</b> button.";
var nmf_path_name = "CurrentBin/audio.nmf";
var nacl_width = 0;
var nacl_height = 0;
var uses_logging = true; // If "false" - hides logs area on the HTML page.

function exampleSpecificActionAfterNaclLoad() {
    domContentsLoaded();
}

var maxNumberOfFiles = 3;

/**
* Attach listeners to buttons and enable buttons loading sounds.
*/
function domContentsLoaded() {
  // enabling load buttons
  var number;
  for (number=1; number <= maxNumberOfFiles; number++){
    document.getElementById('load' + number).disabled = false;
  }
  // attaching listeners to buttons
  attachListeners();
}

function onAudioButtonClick(e) {
  var id = e.target.id;
  // trim the 'img' beginning, if clicked on button image
  if (id.substr(0,3) == 'img')
    id = id.substr(3, id.length - 3);
  // send appropriate message to NaCl module
  nacl_module.postMessage(id);
}

function onLoadButtonClick(e) {
  var fileName = document.getElementById(e.target.id + 'name').value;
  var message = e.target.id.substr(0, e.target.id.length);
  nacl_module.postMessage(message + ' ' + fileName);
}

/**
* Attach listeners to every button.
*/
function attachListeners() {
  var audioButtonNames = ['play1', 'pause1', 'stop1',
                          'play2', 'pause2', 'stop2',
                          'play3', 'pause3', 'stop3'];
  var loadButtonNames = ['load1', 'load2', 'load3'];
  var index;
  for (index = 0; index < audioButtonNames.length; index++) {
    document.getElementById(audioButtonNames[index]).
        addEventListener('click', onAudioButtonClick);
  }
  for (index = 0; index < loadButtonNames.length; index++) {
    document.getElementById(loadButtonNames[index]).
        addEventListener('click', onLoadButtonClick);
  }
}
