﻿$(function () {
    var width = $_GET["w"];
    if (width) {
        setGameWidth(width);
    }
});

function ui_init() {
}

function sendEndWait() {
    window.setTimeout(function () {
        $("#fldUIMsg").val("endwait");
        $("#cmdSubmit").click();
    }, 100);
    waitEnded();
}

function sessionTimeout() {
    disableInterface();
}

function afterSendCommand() {
}

function afterSave() {
}

var _currentPlayer = "";

function playWav(filename, sync, looped) {
    if (!document.createElement('audio').canPlayType) {
        // no <audio> support, so we must play WAVs using <embed> as the 
        // jPlayer Flash fallback does not support WAV.

        var extra = "";

        if (looped) {
            extra = " loop=\"true\"";
        }

        $("#audio_embed").html("<embed src=\"" + filename + "\" autostart=\"true\" width=\"0\" height=\"0\" type=\"audio/wav\"" + extra + ">");
        _currentPlayer = "embed";
    }
    else {
        playAudio(filename, "wav", sync, looped);
    }
}

function playMp3(filename, sync, looped) {
    playAudio(filename, "mp3", sync, looped);
}

function playAudio(filename, format, sync, looped) {
    _currentPlayer = "jplayer";

    $("#jquery_jplayer").unbind($.jPlayer.event.ended);

    if (looped) {
        // TO DO: This works in Firefox. In Chrome the event does fire but the audio doesn't restart.
        endFunction = function () { $("#jquery_jplayer").jPlayer("play"); };
    }
    else if (sync) {
        _waitingForSoundToFinish = true;
        endFunction = function () { finishSync($("#txtCommandDiv").is(":visible")); };
        $("#txtCommandDiv").hide();
    }
    else {
        endFunction = null;
    }

    //$("#jquery_jplayer").bind($.jPlayer.event.error, function (event) { alert(event.jPlayer.error.type); });

    if (endFunction != null) {
        $("#jquery_jplayer").bind($.jPlayer.event.ended, function (event) { endFunction(); });
    }

    if (format == "wav") $("#jquery_jplayer").jPlayer("setMedia", { wav: filename });
    if (format == "mp3") $("#jquery_jplayer").jPlayer("setMedia", { mp3: filename });
    $("#jquery_jplayer").jPlayer("play");
}

function stopAudio() {
    if (_currentPlayer == "jplayer") {
        $("#jquery_jplayer").jPlayer("stop");
    }
    else if (_currentPlayer == "embed") {
        $("#audio_embed").html("");
    }
}

function finishSync(showCommandDiv) {
    _waitingForSoundToFinish = false;
    window.setTimeout(function () {
        if (showCommandDiv) $("#txtCommandDiv").show();
        $("#fldUIMsg").val("endwait");
        $("#cmdSubmit").click();
    }, 100);
}
@@ -1142,32 +1142,90 @@
  looped);%0D%0A%7D%0D%0A%0D%0A
+var showCommandDiv = isElementVisible(%22#txtCommandDiv%22);%0D%0A
 function playAud
@@ -1389,15 +1389,8 @@
   //
- TO DO:
  Thi
@@ -1411,69 +1411,19 @@
 efox
-. In
+ and
  Chrome
- the event does fire but the audio doesn't restart
 .%0D%0A 
@@ -1540,40 +1540,325 @@
     
-_waitingForSoundToFinish = true;
+// Added the following line to set the variable properly. 3-4-2018%0D%0A%09%09showCommandDiv = isElementVisible(%22#txtCommandDiv%22);%0D%0A        _waitingForSoundToFinish = true;%0D%0A        // Altered finishSync to use the showCommandDiv variable.%0D%0A        // It was using txtCommandDiv visibility, which the last line sets to false!
 %0D%0A  
@@ -1902,23 +1902,20 @@
 ishSync(
-$(%22#txt
+show
 CommandD
@@ -1920,25 +1920,8 @@
 dDiv
-%22).is(%22:visible%22)
 ); %7D
@@ -2957,12 +2957,14 @@
  %7D, 100);%0D%0A%7D
+%0D%0A

Fix bug in playAudio() function