//########  Javascript for LC-SiGML-URL-Player Applet  ########
// RE  2007-04

//########  Global Data  ########

    var sign = -1;
    var gloss = "";
    var signMsg = "";
    var curSpeed = 1.0;

//########  Utility Functions  ########

    function showMessage(msg) {
        document.ctrlForm.statusExtra.value = msg;
        window.status = msg;
    }

    function setSpeedButtons(enabled) {
        document.ctrlForm.bttnSlower.disabled     = !enabled;
        document.ctrlForm.bttnResetSpeed.disabled = !enabled;
        document.ctrlForm.bttnFaster.disabled     = !enabled;
    }

    function disableAllButtons() {
        document.ctrlForm.bttnAvatar.disabled     = true;
        document.ctrlForm.bttnPlay.disabled       = true;
        document.ctrlForm.bttnStop.disabled       = true;
        setSpeedButtons(false);
    }

    function setButtonsForPlaying() {
        document.ctrlForm.bttnAvatar.disabled     = true;
        document.ctrlForm.bttnPlay.disabled       = true;
        document.ctrlForm.bttnStop.disabled       = false;
        setSpeedButtons(true);
    }

    function setButtonsForIdling() {
        document.ctrlForm.bttnAvatar.disabled     = false;
        document.ctrlForm.bttnPlay.disabled       = false;
        document.ctrlForm.bttnStop.disabled       = true;
        setSpeedButtons(false);
    }

    function setButtonsForNoAvatar() {
        document.ctrlForm.bttnAvatar.disabled     = false;
        document.ctrlForm.bttnPlay.disabled       = true;
        document.ctrlForm.bttnStop.disabled       = true;
        setSpeedButtons(false);
    }

//########  HTML Start/Finish Handlers  ########

    function initialise() {
        setButtonsForNoAvatar();
        showMessage("HTML initialisation done.");
    }

    function terminate() {
        document.lcurlplayer.terminate();
        disableAllButtons();
        window.status = "LC-SiGML-URL-Player terminated.";
    }

//########  LC-SiGML-URL-Player Call-out Handlers  ########

    function playerIsReadyForAvatar(defaultAvatar) {
        showMessage("Player is ready for avatar.");
        // To load the avatar, uncomment the "setAvatar()" line
        // below; to do nothing comment it out.
        // If loading, any other avatar name may be used if desired
        // in place of the default suggested by the applet.
        //
        // Since this mechanism is not supported by the Windows
        // IE/LiveConnect implementation, it is probably best
        // AVOIDED.
//      document.lcurlplayer.setAvatar(defaultAvatar);
    }

    function playerLoadAvatarIsDone(avatar) {
        showMessage("Avatar loaded: "+avatar+".");
        setButtonsForIdling();
    }

    function playerLoadFramesIsDone(ok) {
        var msg = "";
        if (ok) {
            setButtonsForPlaying();
            msg = "Ready to play.";
        }
        else {
            setButtonsForIdling();
            msg = "SiGML-to-CAS error.";
        }
        showMessage(msg);
    }

    function playerIsAtFrameAndSign(ifrm, isgn, glss) {
        sign = isgn;
        gloss = glss;
        signMsg = "  Sign="+sign+",  \""+gloss+"\"";
        playerIsAtFrame(ifrm)
    }

    function playerIsAtFrame(ifrm) {
        showMessage("Frame="+ifrm+"  "+signMsg);
    }

    function playerIsDroppingFrame(ifrm) {
        showMessage("DROPPED frame "+ifrm);
    }

    function playerAnimationIsDone(ifrm) {
        showMessage("Player stopped at frame "+ifrm+".");
        setButtonsForIdling();
    }

//########  HTML Button Handlers  ########

    function setAvatar() {
        var avatar = document.ctrlForm.avName.value;
        document.lcurlplayer.setAvatar(avatar);
    }

    function setSpeed(speedval) {
        var speedstr = String(speedval);
        document.lcurlplayer.setSpeed(speedstr);
        // Not much point in the following line: the per-frame
        // messages drown it out.
        //showMessage("Speed="+speedstr);
    }

    function raiseSpeed() {
        curSpeed *= Math.SQRT2;
        setSpeed(curSpeed);
    }

    function lowerSpeed() {
        curSpeed *= Math.SQRT1_2;  // sqrt(1/2)
        setSpeed(curSpeed);
    }

    function resetSpeed() {
        curSpeed = 1.0;
        setSpeed(curSpeed);
    }

    function startPlayer() {
        setButtonsForPlaying();
        var sigmlURL = document.ctrlForm.urlText.value;
        document.lcurlplayer.playSiGMLURL(sigmlURL);
    }

    function stopPlayer() {
        setButtonsForIdling();
        document.lcurlplayer.stopPlayingSiGML();
    }

// (END javascript)
