window.addEvent("domready", function()
{
	$$(".auto_focus")[0].focus();

	var fx = new Fx.Slide('result_area');
	fx.hide();
	
	update_recents();
	
	$$('form').addEvent("submit", function(e)
	{
		new Event(e).stop();
	});

	$('pluralize_button').addEvent("click", function(e)
	{
		$('is_pluralize').value = 1;
		$('form2_is_pluralize').value = 1;
		$('form2_word').value = $('word').value;
		
		$$('.operation_display').setText("plural");
		$$('.word_display').setText($('word').value);
		$('result').setText("...");
		
		$('form1').send({
			update: $('result'),
			onComplete: function() {
				update_recents();
				fx.slideIn();
			}
		});
		
	});

/*
	$('singularize_button').addEvent("click", function(e)
	{
		$('is_pluralize').value = 0;
		$('form2_is_pluralize').value = 0;
		$('form2_word').value = $('word').value;
		
		$$('.operation_display').setText("singular");
		$$('.word_display').setText($('word').value);
		$('result').setText("...");
		
		$('form1').send({
			update: $('result'),
			onComplete: function() {
				update_recents();
				fx.slideIn();
			}
		});
		
	});
*/

	$('fixit_button').addEvent("click", function(e)
	{
		$('form2').send({onComplete: update_recents});
	});

	$('show_php').addEvent("click", function(e)
	{
		show_code("php");
	});

	$('show_as').addEvent("click", function(e)
	{
		show_code("as");
	});

	$('show_ror').addEvent("click", function(e)
	{
		show_code("ror");
	});
});

function update_recents()
{
	new Ajax("recents.php", { method: "get", update: $('sidebar'), onComplete: add_links }).request();
	if (cur_language)
		show_code(cur_language);
}

function add_links()
{
	$$(".disagree").addEvent("click", function(e)
	{
		var id = $(this).getAttribute("id");
		id = id.substring("link_".length);
		
		new Ajax("./disagree.php?id=" + id, { method: "get", onComplete: update_recents }).request();
	});
}

var cur_language = null;

function show_code(language)
{
	cur_language = language;
	new Ajax("./show_code.php?lang=" + language, {method: "get", update: $('code_area')}).request();
}
