//########  Javascript for LC-SiGML-Player Applet  ########
// RE  2007-04
// 2008-07
// Adapted for JNLP applet launcher, which makes the player into its subapplet.

var sLimit = 0;
var fLimit = 0;
var sign = -1;
var gloss = "";
var signMsg = "";
var lcPlayer = null;

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

function resetSignGlobals() {
	sLimit = 0;
	fLimit = 0;
	sign = -1;
	gloss = "";
	signMsg = "";
}

function setPlayButtons( playing ) {
	var playing_bool = !!playing;
	var cform = document.ctrlForm;
	cform.bttnPlay.disabled		= playing_bool;
	cform.bttnStop.disabled		= ! playing_bool;
}

function getLCPlayer() {
	if (lcPlayer == null) {
		lcPlayer = document.lcplayerlauncher.getSubApplet();
		if (lcPlayer == null) {
			document.ctrlForm.statusExtra.value =
			"HELP! null LC Player subapplet.";
		}
	}
	return lcPlayer;
}


//########  HTML start/finish handlers  ########

function initialise() {
	setPlayButtons(false);
	window.status = "HTML initialisation done.";
}

function terminate() {
	var cform = document.ctrlForm;
	cform.bttnPlay.disabled = true;
	cform.bttnStop.disabled = true;
	getLCPlayer().terminate();
	window.status = "LC-SiGML-Player terminated.";
}

//########  LC-SiGML-Player callout handlers  ########

function playerLoadHasStarted() {
	resetSignGlobals();
	window.status = "Loading of frames has started.";
}

function playerLoadProgress( nsigns, nframes ) {
	var prevFLimit = fLimit;
	sLimit = nsigns;
	fLimit = nframes;
	if (prevFLimit == 0 && fLimit != 0) {
		// These are the first frames for this animation that we know of.
		setPlayButtons( true );
		window.status = ""+sLimit+" sign(s) now ready to play.";
	}
}

function playerLoadIsDone( ok, nsigns, nframes ) {

	// Default message.
	var msg = "No frames generated from SiGML.";

	if (ok) {
		var prevFLimit = fLimit;
		sLimit = nsigns;
		fLimit = nframes;
		if (fLimit == 0) {
			// Load failed to generate any frames.
			setPlayButtons( false );
			// Use default message.
		}
		else if (prevFLimit == 0) {
			// Now there are frames, and they're the first we know of.
			setPlayButtons( true );
			msg = "Ready to play.";
		}
		else {
			// We are already playing frames from streamed load.
			msg = "All frames loaded: "+sLimit+" signs, "+fLimit+" frames.";
		}
	}
	else {
		// There are no frames.
		setPlayButtons( false );
		// Use default message.
	}

	document.ctrlForm.statusExtra.value = msg;
	window.status = msg;
}

function playerIsAtFrameAndSign( iframe, isign, glossname ) {
	sign = isign;
	gloss = glossname;
	signMsg = "  Sign="+sign+"/"+sLimit+",  '"+gloss+"'";
	playerIsAtFrame( iframe )
}

function playerIsAtFrame( iframe ) {
	msg = "Frame="+iframe+"  "+signMsg;
	document.ctrlForm.statusExtra.value = msg;
	window.status = msg;
}

function playerIsDroppingFrame( iframe ) {
	msg = "DROPPING: "+iframe;
	document.ctrlForm.statusExtra.value = msg;
	window.status = msg;
}

function playerIsDone(nframes) {
	msg = "Player stopped at frame "+nframes;
	document.ctrlForm.statusExtra.value = msg;
	window.status = msg;
	setPlayButtons( false );
}

//########  Play/Stop button handlers  ########

function startPlayer() {
	resetSignGlobals();
	setPlayButtons( true );
	getLCPlayer().playSiGML( document.ctrlForm.sigmlText.value );
}

function stopPlayer() {
	setPlayButtons( false );
	getLCPlayer().stopPlayingSiGML();
}

// (END javascript)
