/* Javascript support for displaying random Unicode characters */

var scrolling = false;
/** Seconds to show each character when in 'scrolling' mode */
var duration = 2;
var scroller = 0;

/** Function to call from unirandLoaded() once a character has loaded */
var unir_callback = null;

function newchar(style) {
	loadContent("http://www.ark.cs.cmu.edu/nschneid/unirand/unirand.php?sty=" + style);
}
function nextchar() {
	newchar("display: none;");
	scroller = window.setTimeout("nextchar()",1000*duration);
}
function togScroll() {
	if (scrolling) {
		window.clearTimeout(scroller);
		scrolling = false;
		document.getElementById("anext").style.display = "inline";
	}
	else {
		scrolling = true;
		nextchar();
	}
}
/**
 * Dynamically loads a script to display a Unicode character, its metadata, and 
 * controls to advance to another character
 * @param file URL of the server script serving the character display script
 * @param callback Function to call once the character has loaded (optional but 
 * sticky, i.e. a given callback will continue to be used on subsequent invocations 
 * until a new one is provided or clearCallback() is called)
 */
function loadContent(file, callback) {
	var scriptTag = document.getElementById('loadScript');
	var head = document.getElementsByTagName('head').item(0);
	if(scriptTag!=null) {
		head.removeChild(scriptTag);
	}
	script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script);
	if (callback)
		unir_callback = callback;
}
/**
 * Removes the use of a callback function until one is provided on a subsequent 
 * invocation of loadContent()
 */
function clearCallback() {
	unir_callback = null;
}
/**
 * A character has loaded--invoke the most recently specified callback
 */
function unirandLoaded() {
	unir_callback();
}

