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

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

var RETURN_CHAR = 13;

var sLimit = 0;
var fLimit = 0;
var sign = -1;
var gloss = "";
var signMsg = "";
var curSpeed = 1.0;

var lcURLPlayer = null;

//########  LC URL Player Subapplet Access  ########

function getLCURLPlayer() {
	if (lcURLPlayer == null) {
		lcURLPlayer = document.lcupApplet.getSubApplet();
		if (lcURLPlayer == null) {
			document.ctrlForm.statusExtra.value =
			"HELP! null LC URL Player subapplet.";
		}
	}
	return lcURLPlayer;
}

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

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

function resetGlobals() {
	resetSignGlobals();
	curSpeed = 1.0;
}

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

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

function setPlayButtons( avenable, playenable, stopenable ) {
	var cform = document.forms.ctrlForm;
	cform.avSelect.disabled	    = ! avenable;
//	cform.bttnAvatar.disabled	= ! avenable;
	cform.bttnPlay.disabled     = ! playenable;
	cform.bttnStop.disabled     = ! stopenable;
}

function disableAllButtons() {
	setPlayButtons( false, false, false );
	setSpeedButtons( false );
}

function setButtonsForPlaying() {
	setPlayButtons( false, false, true );
	setSpeedButtons( true );
}

function setButtonsForIdling() {
	setPlayButtons( true, true, false );
	setSpeedButtons( false );
}

function setButtonsForNoAvatar() {
	setPlayButtons( true, false, false );
	setSpeedButtons( false );
}

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

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

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

//########  LC-SiGML-URL-Player event/callout 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.
//  getLCURLPlayer().setAvatar(defaultAvatar);
}

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

function playerLoadFramesHasStarted() {
	resetSignGlobals();
	showMessage( "Loading of frames has started." );
}

function playerLoadFramesProgress( scount, fcount ) {
	var prevFLimit = fLimit;
	sLimit = scount;
	fLimit = fcount;
	if (prevFLimit == 0 && fLimit != 0) {
		// These are the first frames for this animation that we know of.
		setButtonsForPlaying();
		showMessage( ""+scount+" sign(s) now ready to play." );
	}
}

function playerLoadFramesIsDone(ok, scount, fcount) {

//	window.alert("Load done: "+ok+" "+scount+" "+fcount+"    fLimit="+fLimit);

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

	if (ok) {
		var prevFLimit = fLimit;
		sLimit = scount;
		fLimit = fcount;
		if (fLimit == 0) {
			// Load failed to generate any frames.
			setButtonsForIdling();
			// Use default message.
		}
		else if (prevFLimit == 0) {
			// Now there are frames, and they're the first we know of.
			setButtonsForPlaying();
			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.
		setButtonsForIdling();
		// Use default message.
	}

	// If animation is in progress, or is about to be so, then this message
	// will be swamped, but there's no harm in trying.
	showMessage( msg );
}

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

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

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

function playerAnimationIsDone( iframe ) {
	var f_num = iframe - 0, flimit_num = fLimit - 0;
	var smsg = (f_num == flimit_num ? "sign "+sLimit+", " : "");
	if (f_num != 0) {
		showMessage( "Player stopped at "+smsg+"frame "+iframe+"." );
	}
	setButtonsForIdling();
}

//########  HTML button/input handlers  ########

function selectAvatar() {
	var avatar = document.forms.ctrlForm.avSelect.value;
	getLCURLPlayer().setAvatar( avatar );
}

function setSpeed( speedval ) {
	var speedstr = String( speedval );
	getLCURLPlayer().setSpeed( speedval );
	// The per-frame messages drown out the following one.
	//showMessage( "Speed="+speedstr );
}

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

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

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

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

function stopPlayer() {
	setButtonsForIdling();
	getLCURLPlayer().stopPlayingSiGML();
}

function doPreventDefault( evt ) {
	// Fix IE non-standardness.
	if (evt.preventDefault != undefined) {
		evt.preventDefault();		// DOM
	}
	else {
		evt.returnValue = false;	// IE nonstandard
	}
}

function handleURLKey( evt ) {
	// IE doesn't do evt.which, but always does evt.keyCode;
	var chr = evt.which || evt.keyCode;
	if (chr == RETURN_CHAR) {
		doPreventDefault( evt );
		startPlayer();
	}
}

function handleAvatarKey( evt ) {
	// IE doesn't do evt.which, but always does evt.keyCode;
	var chr = evt.which || evt.keyCode;
	if (chr == RETURN_CHAR) {
		doPreventDefault( evt );
		setAvatar();
	}
}

// (END javascript)
