// This object maintains the current values of (p,q,r), as selected by the user.
var angles = {pValue:2, qValue:2, rValue:2};

// These variables manage the current selection code.
var centerSelection = "P";
var centerCode = {P:0, Q:1, R:2, PQ:3, PR:4, QR:5, PQR:6};

// This variable maintains the selection of what to display. Possible
// values are:
// 0: Fundamental Regions
// 1: Uniform Polyhedra and Tessellations
// 2: Dual UNiform Polyhedra and Tessellations
var displaySelection = 0;

// This variable maintains whether fundamental regions are being displayed.
var displayFundamentalRegions = true;

// This variable maintains the current selection of a Wythoff Symbol.
var wythoffSelection = 0;

// This variable maintains the selection of a polyhedron or tessellation.
var polyhedronSelected = false;

// This variable maintains selection of a spherical display.
var sphericalDisplay = true;

// This variable maintains whether or not to display the polyhedron option.
var displayPolyhedronOption = false;

function initialization()
{
	debugline("initialization");
}

// This function is called when the values of (p,q,r) are changed by the user.
// It calls other functions to affect the HTML and to communicate with the  applet.
function selectAngle(selName)
{
	var theSel=document.getElementById(selName);
	angles[selName] = theSel.options[theSel.selectedIndex].value;
	var spherical  = document.getElementById("spherical");
	var euclidean  = document.getElementById("euclidean");
	var hyperbolic = document.getElementById("hyperbolic");
	var centerSelect = document.getElementById("centerSelect");
	var c = angles.pValue*angles.qValue + angles.qValue*angles.rValue 
			+ angles.rValue*angles.pValue - angles.pValue*angles.qValue*angles.rValue;
	if(c >0){
		// Spherical case
		setSphericalDisplay(true);
		spherical .style.visibility = "visible";
		euclidean .style.visibility = "hidden";
		hyperbolic.style.visibility = "hidden";
	} else if (c == 0){
		// Euclidean case
		setSphericalDisplay(false);
		spherical .style.visibility = "hidden";
		euclidean .style.visibility = "visible";
		hyperbolic.style.visibility = "hidden";
	} else {
		// Hyperbolic case
		setSphericalDisplay(false);
		spherical .style.visibility = "hidden";
		euclidean .style.visibility = "hidden";
		hyperbolic.style.visibility = "visible";
		centerSelect.selectedIndex = 0;
	}
	//debugline(" P=" + angles.pValue + " q=" + angles.qValue + " r=" + angles.rValue );
	var theAp = document.getElementById("kalapplet");
	theAp.getAngles(angles.pValue, angles.qValue, angles.rValue);
}

// This function is called when the value of "sphericalDisplay" may change.
function setSphericalDisplay(isSpherical){
	if(isSpherical != sphericalDisplay){
		sphericalDisplay = isSpherical;
		if(!displayFundamentalRegions)setDisplayPolyhedronOption(isSpherical);
	}
}

// This function is called when the display selection is changed by the user.
// It calls other functions to affect the HTML and to communicate with the applet.
function selectDisplay(){
	var theSel = document.getElementById("displaySelect");
	//if(theSel == null)window.alert("JS displaySelect comes up null");
	displaySelection = theSel.selectedIndex;
	document.kalapplet.selectDisplay(displaySelection);
	if(displayFundamentalRegions != (displaySelection == 0)){
		if(sphericalDisplay){
			setDisplayPolyhedronOption(displaySelection != 0);
		}
		displayFundamentalRegions = (displaySelection == 0);
		var wythoffMenu = document.getElementById("wythoffMenu");
		if(displaySelection == 0){
			wythoffMenu.style.visibility = "hidden";
		} else{
			wythoffMenu.style.visibility = "visible";
			var wythoffSelect = document.getElementById("wythoffSelect");
			wythoffSelect.selectedIndex = 0;
			wythoffSelection = 0;
			document.getElementById("kalapplet").selectWythoffSymbol(0);
		}
	}
}

// This function manages the display of the option to show a polyhedron or tessellation.
function setDisplayPolyhedronOption(displayIt){
	if(displayIt != displayPolyhedronOption){
		var polyhedronOption = document.getElementById("polyhedronOption");
		polyhedronOption.style.display = displayIt? "block" : "none";
		if(displayIt){
			var polySelect = document.getElementById("polySelect");
			polySelect.selectedIndex = 0;
			polyhedronSelected = false;
			document.getElementById("kalapplet").selectPolyhedron(false);
		}
		displayPolyhedronOption = displayIt;
	}
}

function centerSelect()
{
	var sel = document.getElementById("centerSelect");
	centerSelection = sel.options[sel.selectedIndex].value;
	//debugline("centerSelection: " + centerSelection);
	//debugline("center code: " + centerCode[centerSelection]);
	var theAp = document.getElementById("kalapplet");
	theAp.getCenterCode(centerCode[centerSelection]);
}

// This tells the applet to select a Wythoff symbol for a uniform
// tesselation
function wythoffSelect(s){
	var wythoffSelect = document.getElementById("wythoffSelect");
	var theAp = document.getElementById("kalapplet");
	theAp.selectWythoffSymbol(wythoffSelect.selectedIndex);
}

// This function tells the applet whether to display a
// polyhedron or a tesselation.
function polySelect(){
	var polySelect = document.getElementById("polySelect");
	var theAp = document.getElementById("kalapplet");
	polyhedronSelected = (polySelect.selectedIndex == 1);
	theAp.selectPolyhedron(polyhedronSelected);
}

function debugline(s)
{
	var tell = document.getElementById("telltale");
	if(tell != null)tell.innerHTML = s;
}

