/**
 *
 * Plays audio available at a URL (or file path) depending on the
 *   arguments that are passed in.
 *
 * For audio at a URL, the host and port are baked into the
 *   miniplayer.swf, the only way to test in the different environments
 *   (localhost, staging, prod) is to use a different swf.
 *
 * Arguments:
 *
 *   audioId: a file path, url, username, or aikey
 *
 *   type: 'mp3'      audioId is assumed to be an absolute file path
 *                      or url
 *
 *         'greeting' audioId is assumed to be a username
 *                      and the swf will use the uri:
 *                      /prompt?username=<audioId>
 *
 *         otherwise  audioId is assumed to be an aikey
 *                      and the swf will use the uri:
 *                      /audio?key=<audioId>
 *
 * Callbacks:
 *
 *   'nextCallback'   calls this js function after audio finished
 *                      playing
 *
 *   'playCallback'   calls this js function when audio starts
 *                      playing
 *
 *   'stopCallback'   calls this js function when audio stops
 *                      playing
 *
 *   'sliderCallback' calls this function every interval with
 *                      value 0-100
 *
 *   'loadCallback'   calls this function after audio is loaded
 *
 */
var callbackFunctions;
function debug(text) {
 console.log(text)
}

function nextCallback() {
  if (callbackFunctions["nextCallback"])
    callbackFunctions["nextCallback"]();
}
function sliderCallback(time, seconds) {
  if (callbackFunctions["sliderCallback"]) 
    callbackFunctions["sliderCallback"](time, seconds);
}
function playCallback() {
  if (callbackFunctions["playCallback"])
    callbackFunctions["playCallback"]();
}
 
function playMusic(audioId, type, callback) {
  callbackFunctions = callback;
 getMiniPlayer().init(audioId, type, {
   "nextCallback":"nextCallback",
   "sliderCallback":"sliderCallback",
   "playCallback":"playCallback"});
 getMiniPlayer().playTrack();
}

function stopMusic() {
  try {
    getMiniPlayer().stopTrack();
  } catch (ignored) {}
}

function getMiniPlayer() {
  var movieName = 'miniplayer';
  var isIE = navigator.appName.indexOf('Microsoft') != -1;
  if (isIE && jQuery.browser.version != '7.0') {
    return window[movieName];
  }
  else {
    return document[movieName];
  }
}
