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

// Flash Player:

var ap_instances = new Array();

function ap_stopAll(playerID) {
  for(var i = 0;i<ap_instances.length;i++) {
    try {
      if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
      else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
    } catch( errorObject ) {
      // stop any errors
    }
  }
}

function ap_registerPlayers() {
  var objectID;
  var objectTags = document.getElementsByTagName("object");
  for(var i=0;i<objectTags.length;i++) {
    objectID = objectTags[i].id;
    if(objectID.indexOf("audioplayer") == 0) {
      ap_instances[i] = objectID.substring(11, objectID.length);
    }
  }
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );

// Questionnaire form

var intervalID
var timeoutID
var sliders = new Hash()

function show_player() {
  $('soundbadge_preview').show()
  $('waiting_for_create_spinner').hide()
  if ($('describe_yourself') != null)
    $('describe_yourself').className='inactive'
  if ($('listen_to_sound') != null)
    $('listen_to_sound').className='active'
}

function start_preview() {
  window.clearInterval(intervalID)
  $('waiting_for_create_spinner').show()
  $('soundbadge_preview').hide()
}

function start_spontaneity() {
  sliders['spontaneity'].setValue(1)
  sliders['spontaneity'].setDisabled()
  intervalID = window.setInterval('decrease_spontaneity()', 1000)
  new Tip($('spontaneity_handle'), "Guess what: We're measuring this one, so hurry up!", {className: 'tooltip', effect: 'appear', duration: 0.2})
}

function decrease_spontaneity() {
  if (sliders['spontaneity'].value > 0) {
    sliders['spontaneity'].setValueBy(-0.01)
  } else {
    window.clearInterval(intervalID)
  }
}

function init_sliders() {
  sliders.keys().each(function(key) {
    sliders[key].setValue($('questionnaire_' + key).value)
  }); 
}