var HSCALE = 1.0;
var VSCALE = 1.0;
var trackerId = null;
var abort = false;
var paused = false;
var length = 0;
var leadLength = 32;
var leads = 0;
var parts = 0;
var tenorsTogether = false;
var nicePE = false;
var optimumBalance = false;
var maxCOM = false;
var calls = 0;
var LHonly = false;

function init()
{
	checkApplet();
	emitVersion();
	emitMethods();
	lhChecked();
	HSCALE = Math.min(70, Math.max(document.all.sizeCheck.offsetWidth, 50))/50;
	VSCALE = Math.min(20, Math.max(document.all.sizeCheck.offsetHeight, 16))/16;
}
function checkApplet()
{
	if (elf!=undefined && elf!=null && elf.available)
		return;
	var s = "Sorry, Elf has failed to load.\n";
	s+= "Please check you are running IE 5.0 or greater, and that you\n";
	s+= "have the Microsoft Java VM installed.\n";
	s+= "If you have the Sun Java plug-in, you will need to disable it.\n";
	s+= "\n";
	s+= 'For more help on Java problems, click the "Java Problems"\n';
	s+= "link on the Elf introduction page.\n"
	s+= "\n"
	s+= 'Click "OK" to return to the introduction page now.';
	alert(s);
	document.location = "index.html";
}
function emitVersion()
{
	document.all.version.innerHTML = "<p>"+elf.getVersionString();
}
function emitMethods()
{
	var n = elf.getNMethods();
	if (n>=1)
	{
		var s = '<select multiple name="methodList" size="'+8+'">'
		for (var i=0; i<n; i++)
			s+= '<option value="'+(i+1)+'">'+elf.getMethod(i)+'</option>';
		s+= '</select>';
		document.all.methods.innerHTML = s;
	}
	updateCompTitle();
}
function openLibrary(page)
{
	window.open(page+".html", "library", "menubar=no, toolbar=no, scrollbars=yes, width="+(480*HSCALE)+", height="+(480*VSCALE));
}
function removeMethod()
{
	var list = getForm().methodList;
	var i = list.selectedIndex;
	var n = elf.getNMethods();
	if (i>=0 && n>1)
	{
		// Select multiple
		for (i=n-1; i>=0; i--)
			if (list.options(i).selected)
			{
				elf.removeMethod(i);
				n--;
				if (n<=1)
					break;
			}
		emitMethods();
	}
}
function popupMusic(page)
{
	window.open(page+"?first", "music", "menubar=no, toolbar=no, scrollbars=yes, width="+(430*HSCALE)+", height="+(500*VSCALE));
}
function startCompose()
{
	if (getParams())
	{
		abort = false;
		unpause();
		if (trackerId!=null)
		{
			clearInterval(trackerId);
			trackerId = null;
		}
		if (elf.compose(leads, parts, tenorsTogether, nicePE, optimumBalance, maxCOM, calls, LHonly))
			trackerId = setInterval("tracker()", 500);
	}
}
function stopCompose()
{
	elf.stop()
	unpause();
	abort = true;
}
function pauseCompose()
{
	if (elf.isPaused())
	{
		unpause();
		elf.resume();
	}
	else
	{
		if (elf.pause())
		{
			document.all.Pause.value = "Resume";
			paused = true;
		}
	}
}
function unpause()
{
	document.all.Pause.value = "Pause";
	paused = false;
}
function tracker()
{
	var finished = elf.isFinished();
	document.all.status.innerHTML = "<p>"+new String(elf.getStatus());
	document.all.showcomps.style.display = "";
	if (elf.isThereNewOutput())
		document.all.output.innerHTML = "<pre>"+new String(elf.getOutput())+"</pre>";
	if (abort || finished)
	{
		if (elf.isError())
			alert("INTERNAL ERROR!\n"+elf.getErrorMsg());
		clearInterval(trackerId);
		trackerId = null;
	}
}
function updateCompTitle()
{
	var form = getForm();
	leads = new Number(form.leads.value);
	parts = new Number(form.parts.value);
	if (leads==0 || isNaN(leads))
		alert("The number of leads must 1 or greater - please reenter");
	else if (parts==0 || isNaN(parts))
		alert("The number of parts must 1 or greater - please reenter");
	if (isNaN(leads) || isNaN(parts))
		length = 0;
	else
		length = leads*parts*leadLength;
	var s = "<b>"+length+" "+elf.getNMethods()+"-spliced</b>"
	document.all.compTitle.innerHTML = s;
}
function lhChecked()
{
	var form = getForm();
	form.bobs.disabled = form.LHonly.checked;
	if (form.LHonly.checked)
		document.all.bobsCaption.style.color = "gray";
	else
		document.all.bobsCaption.style.color = "";
}
function getParams()
{
	updateCompTitle();
	if (length<=0)
		return false;
	var form = getForm();
	calls = 0;
	if (form.bobs.checked)
		calls = 1;
	tenorsTogether = form.tt.checked;
	nicePE = form.nicePE.checked;
	optimumBalance = form.balance.checked;
	maxCOM = form.maxCOM.checked;
	LHonly = form.LHonly.checked;
	return true;
}
function getForm()
{
	return document.all.inputForm;
}
function showAllComps()
{
	elf.pause();
	var w = window.open("output.html", "output", "menubar=yes, toolbar=no, scrollbars=yes, width=440, height=560");
	w.document.open("text/plain");
	w.document.writeln(elf.getAllComps());
	w.document.close();
	w.focus();
	// If not already paused by used, resume
	if (!paused)
		elf.resume();
}

