$(document).ready(function()
{
	$("#message").dialog(
	{
		bgiframe: true,
		closeOnEscape: true,
		resizable: false,
		modal: false,
		buttons:
		{
			'Close': function()
			{
				$(this).dialog('close');
			}
		},
		show: 'blind',
		hide: 'blind',
		open: function(event, ui)
		{
			$(document).oneTime(2500, "Message", function()
			{
				if ($("#message").dialog('isOpen'))
					$("#message").dialog('close');
			});
		}
	});
	
	$("#dialog").dialog(
	{
		bgiframe: true,
		autoOpen: false,
		closeOnEscape: true,
		resizable: false,
		width: 230,
		modal: true,
		position: 'center',
		buttons: {
				'Login': function()
				{
					data = $("#loginform").serialize();
					$.post('http://ufotr.ca/index.php/en/account/ajax_login.html', data, function(data)
					{
						if (!data.success)
						{
							$("#dialog p.error").show('highlight').html('<h4>Invalid login!</h4>').fadeOut(500, function()
							{
								$("#username").select().focus();
							});
						}
						else
						{
							$("#dialog").dialog('close');
							$("#header p.user").html(data.name + ' - ' + data.header);
							
							window.location = data.page;
						}
					}, "json");
				},
				Cancel: function()
				{
					$(this).dialog('close');
				}
		},
		show: 'drop',
		hide: 'drop',
		open: function()
		{
			$(document).oneTime(500, "Test", function()
			{
				$("#username").focus();
			});
		}
	});
	
	$("a#login").click(function()
	{
		$("#dialog").dialog('open');
	});
});


$().ready(function()
{
	$(document).keypress(function(e)
	{
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
		{
			if ($('#dialog').dialog('isOpen'))
			{
            	$('[aria-labelledby$=dialog]').find(":button:contains('Login')").click(); 
			}
			
            return false; 
		}
	});
});

