var __verified = false;
var __kurons = null;	// array of kurons from the last ajax lookup.
var __kuronsi = {};	// hash of known kuron names.
var __timer = null;
var __ac = null;

function do_load()
{
	__kurons = new HashList(10);
	$('kuron').addEvent('keyup', do_kuron_change);
	$(document).addListener('keyup', function(e)
		{
			e = new Event(e);
			if (e.key == 'esc') destroy_suggest_window();
		});
	$('form').setProperty('autocomplete', 'off');
}

function build_query(x)
{
	var k;
	var out = [];
	for (k in x)
	{
		//if (x.prototype[k]) continue;
		out.push(encodeURIComponent(k) + '=' + encodeURIComponent(x[k]));
	}
	return out.join('&');
}

function do_submit()
{
	var award = $('award');
	var kuron = $('kuron');
	var aname = award.value;
	var kname = kuron.value;
	if (aname.length == 0)
	{
		alert('Please enter the award name!');
		award.focus();
		return;
	}
	if (kname.length == 0)
	{
		alert('Please enter the winning kuron\'s name!');
		kuron.focus();
		return;
	}
	var ok = false;
	if (__kuronsi[kname]) ok = true;
	else
	{
		// verify the kuron exists.
		var http = new XHR({
							method: 'post',
							async: false,
							onSuccess: function(text, xml)
							{
								try{
									var o = eval('(' + text + ')');
									ok = (o.length == 1 && o[0] == kname); 
								}
								catch(e) {}
							}
		});
		http.send('/json_get_user.php', build_query({name: kname, exact: 1}));
	}
	if (ok)
	{
		aname = aname.replace(/ /g, '_');
		aname = aname.replace(/\//g, '');
		kname = kname.replace(/\//g, '\\');
		window.location.href = '/awards/' + aname + '/' + kname + '/'; 
	}
	else
	{
		alert('Invalid kuron.  Please check the spelling more carefully.');	
		kuron.focus();
	}
}

function do_kuron_change(e)
{
	e = new Event(e);
	var kuron = $('kuron');
	var kname = kuron.value;

	if (kname.length < 3)
	{
		destroy_suggest_window();
		return;
	}
	if (e.key == 'up' || e.key == 'down')
	{
		do_auto_complete();
		return;
	}

	if (__timer)
	{
		$clear(__timer);
		__timer = null;
	}
	
	if (e.key == 'enter' || e.key == 'esc')
	{	
		destroy_suggest_window();
		return;
	}
	
	__timer = do_auto_complete.delay(250);
}

function do_auto_complete()
{
	if(__timer)
	{
		$clear(__timer);
		__timer = null;
	}
	var kuron = $('kuron');
	var kname = kuron.value;

	if (kname.length < 3) return;

	// check if recently found...
	var o = __kurons.get(kname);
	if (o)
	{
		do_suggest_window(o);
		return;
	}

	var http = new XHR({
				method: 'post',
				async: true,
				onSuccess: function(text, xml)
				{
					try{
						var o = eval('(' + text + ')');
						__kurons.add(kname, o);
						o.forEach(function(v,i,a) { __kuronsi[v] = true; })
						do_suggest_window(o);
					}
					catch(e) {}
				}
	});
	http.send('/json_get_user.php', build_query({name: kname}));
}

function destroy_suggest_window()
{
	if (__ac)
	{
		__ac.setOpacity(0);
		while (__ac.lastChild) __ac.removeChild(__ac.lastChild);
	}
}
function create_suggest_window()
{
	if (!__ac)
	{
		__ac = new Element('div', {
						   id: 'autocomplete',
						   styles: {
							   position: 'absolute',
							   visibility: 'hidden'
						   }
						   });
		document.body.appendChild(__ac);
	}
}
function do_suggest_window(args)
{
	var kuron = $('kuron');
	var kname = kuron.value;

	create_suggest_window();
	while (__ac.lastChild) __ac.removeChild(__ac.lastChild);

	if (args)
	{
		var ul = new Element('ul');
		var a,li;
		__ac.appendChild(ul);
		if (args.length == 0)
		{
			li = new Element('li', {'class': 'nomatch'});
			li.appendText('No matches.');
			ul.appendChild(li);
		}
		else args.forEach(
			function(v,i,a)
			{
				li = new Element('li');
				a = new Element('a', {href: 'javascript: ;'});
				a.setProperty('kuron', v);
				a.addEvent('click', do_select_kuron);
				a.appendText(v);
				li.appendChild(a);
				ul.appendChild(li);
			}
		);
	}
	var x = kuron.getCoordinates();
	__ac.setStyle('top', x.bottom + 4);
	__ac.setStyle('left', x.left);
	__ac.setStyle('width', x.width);
	__ac.setOpacity(1);	
	
}

function do_select_kuron(e)
{
	e = new Event(e);
	var kuron = $('kuron');
	kuron.value = e.target.getProperty('kuron');
	destroy_suggest_window();
	e.stop();
	kuron.focus();
}

var HashList = new Class({
	initialize: function(size)
	{
		this.values = [];
		this.keys = {};
		this.size = size || 32;
		this.head = 0;
	},
	add: function(k, v)
	{
		var x;
		if ((x = this.values[this.head]))
		{
			// remove
			delete this.keys[x.key];
		}
		this.keys[k] = this.head;
		this.values[this.head] = {key: k, value: v};
		this.head = (this.head + 1 ) % this.size;
	},
	get: function(k)
	{
		var x;
		x = this.keys[k];
		if (x === undefined) return null;
		return this.values[x].value;
	}
});
	