var HSCALE = 1.0;
var VSCALE = 1.0;
var LIBRARYWIDTH = 500;
var LIBRARYHEIGHT = 480;
var MUSICWIDTH = 440;
var MUSICHEIGHT = 480;
var trackerId = null;
var abort = false;
var paused = false;
var lengthOK = true;
var minPartLength = 0;
var maxPartLength = 0;
var leads = 0;
var parts = 0;
var tenorsTogether = false;
var nicePE = false;
var optimumBalance = false;
var maxCOM = false;
var calls = 0;
var LHonly = false;

var elf;

function init()
{
	elf = document.getElementById("elf");
	checkApplet();
	emitVersion();
	emitMethods();
	lhChecked();
	var sizeCheck = document.getElementById("sizeCheck");
	HSCALE = Math.min(70, Math.max(sizeCheck.offsetWidth, 50))/50;
	VSCALE = Math.min(20, Math.max(sizeCheck.offsetHeight, 16))/16;
}
function checkApplet()
{
	if (elf && elf.getVersionString)
		return;
	return;
	var s = "Sorry, Elf has failed to load.\n";
	s+= "Please check you have a Java VM installed, such as the Sun Java plugin.\n";
	s+= "Note the obsolete Microsoft Java VM is not longer supported by Elf Undomiel.\n";
	s+= "\n";
	s+= 'For more help on Java problems, click the "Java Help"\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.getElementById("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.getElementById("methods").innerHTML = s;
	}
	updateCompTitle();
}
function openLibrary(page)
{
	window.open(page+".html", "library", "menubar=no, toolbar=no, scrollbars=yes, width="+(LIBRARYWIDTH*HSCALE)+", height="+(LIBRARYHEIGHT*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.item(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="+(MUSICWIDTH*HSCALE)+", height="+(MUSICHEIGHT*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, minPartLength, maxPartLength))
			trackerId = setInterval("tracker()", 500);
	}
}
function stopCompose()
{
	elf.stop()
	unpause();
	abort = true;
}
function pauseCompose()
{
	if (elf.isPaused())
	{
		unpause();
		elf.resume();
	}
	else
	{
		if (elf.pause())
		{
			document.getElementById("Pause").value = "Resume";
			paused = true;
		}
	}
}
function unpause()
{
	document.getElementById("Pause").value = "Pause";
	paused = false;
}
function tracker()
{
	var finished = elf.isFinished();
	document.getElementById("status").innerHTML = "<p>"+new String(elf.getStatus());
	document.getElementById("showcomps").style.display = "";
	if (elf.isThereNewOutput())
		document.getElementById("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 = Number(form.leads.value);
	parts = 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");
	var min = 0;
	var max = 0;
	if (isNaN(leads) || isNaN(parts))
	{
		lengthOK = false;
	}
	else
	{
		lengthOK = true;
		min = leads*parts;
		max = min*elf.getLongestLead();
		min*= elf.getShortestLead();
	}
	var s = "<b>";
	if (min==max)
		s+= min;
	else
		s+= min+"-"+max;
	s+= " "+elf.getNMethods()+"-spliced</b>"
	document.getElementById("compTitle").innerHTML = s;
	if (lengthOK)
	{
		formMin = Number(form.minLength.value);
		formMax = Number(form.maxLength.value);
		if (isNaN(formMin) || formMin<min || formMin>=max)
			formMin = min;
		if (isNaN(formMax) || formMax<formMin || formMax>max)
			formMax = max;
		form.minLength.value = formMin;
		form.maxLength.value = formMax;
		minPartLength = Math.round(formMin/parts);
		maxPartLength = Math.round(formMax/parts);
		document.getElementById("lengthLimits").style.display = (min!=max? "" : "none");
	}
}
function lhChecked()
{
	var form = getForm();
	form.bobs.disabled = form.LHonly.checked;
	if (form.LHonly.checked)
		document.getElementById("bobsCaption").style.color = "gray";
	else
		document.getElementById("bobsCaption").style.color = "";
}
function getParams()
{
	updateCompTitle();
	if (!lengthOK)
		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.getElementById("inputForm");
}
function showAllComps()
{
	elf.pause();
	var w = window.open("output.html", "output", "menubar=yes, toolbar=no, scrollbars=yes, width=440, height=560");
	// Safari and Opera don't seem to honour the mime type for the new window, so display in HTML. 
	var useHtml = navigator.userAgent.indexOf("Safari")>0 || navigator.userAgent.indexOf("Opera")>0;
	if (useHtml)
	{
		w.document.open("text/html");
		w.document.write("<html><head></head><body><pre>");
	}
	else
	{
		w.document.open("text/plain");
	}
	w.document.writeln(elf.getAllComps());
	if (useHtml)
		w.document.writeln("</pre></body></html>");
	w.document.close();
	w.focus();
	// If not already paused by used, resume
	if (!paused)
		elf.resume();
}
function updateCompsToKeep(elt)
{
	var n = Number(elt.value);
	if (n==0 || isNaN(n))
	{
		alert("The number of comps to keep must 1 or greater - please reenter");
	}
	else
	{
		elf.setNCompsToKeep(n);
		document.getElementById("ncompswarning").style.display = (n>20? "block" : "none");
	}
}
