//This handles synthesis of improvisations.

enableform = true; //Buttons start clickable.
changeSynthLinks();

waittext = document.createElement("span");
waittext.className = "WaitText";
waittext.appendChild(document.createTextNode("This may take a minute..."));

function changeSynthLinks() {
	var improvform = document.getElementById("ImprovForm");
	if (!improvform)
		//If the form doesn't exist, there's nothing more to do.
		return;
		
	var links = improvform.getElementsByTagName('input');
	
	//Find the links that point to MP3s.
	for (var linkidx = 0; linkidx < links.length; linkidx++) {
		if (links[linkidx].type.toLowerCase() == 'image') {
			if (links[linkidx].className.indexOf("PlayButton") >= 0)
				links[linkidx].onclick = synthImprov;
			else
				links[linkidx].onclick = goIfFormEnabled;
		}
	}
}

function synthImprov() {
	if (!enableform) {
		alert("Please wait for the current synthesis to finish.");
		return false;
	}
	
	var formpath = this.form.action;
	var protoend = formpath.indexOf("//");
	if (protoend >= 0)
		formpath = formpath.substring(formpath.indexOf('/', protoend+2), formpath.length);
	
	var querystr = "?";
	for (var elemidx = 0; elemidx < this.form.elements.length; elemidx++)
		querystr += encodeURI(this.form.elements[elemidx].name) + "=" + encodeURI(this.form.elements[elemidx].value) + "&";
	
	//The form hasn't been submitted yet, so the button's not included! We need to add that, obviously.
	//Also let the script know we're handling this via AJAX.
	querystr += encodeURI(this.name) + "=" + encodeURI(this.value) + "&ajax=1";
	
	//Use AJAX to get the response, whether an error message or a link to the file.
	var fetchmusic = new SafeXMLRequest();
	
	//Don't allow the user to click on the form again until this is done.
	enableform = false;
	
	//Let the user know we're synthesizing.
	oldsrc = this.src;
	oldr = this.style.marginRight;
	this.src="images/hourglass_spinning.gif";
	this.style.marginRight = "-2px";

	this.parentNode.insertBefore(waittext, this.parentNode.firstChild);
	
	var outerThis = this;
	fetchmusic.setCallback(
		function(ResponseCode, Contents, ContentNode) {
			if (ResponseCode >= 400) {
				//The error handler will catch it below.
				Contents = "Server returned HTTP code " + ResponseCode + ".";
			}
			
			var locsentinel = "Location: ";
			clocpos = Contents.indexOf(locsentinel);
			if (clocpos < 0) {
				//Something went wrong.
				alert("Error: " + Contents);
				Recaptcha.reload ();
			}
			else {
				loadURLInPlayer(Contents.substring(clocpos + locsentinel.length, Contents.length));
			}

			outerThis.src = oldsrc;
			outerThis.style.marginRight = oldr;
			outerThis.parentNode.removeChild(waittext);
			enableform = true;
		}
	);
	
	fetchmusic.open("POST", formpath + querystr);

	//Since we're handling this here, don't pass through to the default action.
	return false;
}

function goIfFormEnabled() {
	if (!enableform) {
		alert("Please wait for the current synthesis to finish.");
		return false;
	}
	
	return true;
}

