$(function(){
	$('#shoutbox_signup').bind('submit', function(e){
		e.preventDefault();
		var form = $(this);
		$.post(
			'/page/ys_signup',
			form.serialize(),
			function(json){
				if (json.success) {
					form.children(':input').val('').end().parent().hide();
					$('#shoutbox_user_actions').show();
				}
				$('#shoutbox_message').text(json.message).show();
			},
			'json'
		);
	});

	$('#shoutbox_login').bind('submit', function(e){
		e.preventDefault();
		var form = $(this);
		$.post(
			'/page/ys_login',
			form.serialize(),
			function(json){
				if (json.success) {
					form.children(':input').val('').end().parent().hide();
					$('#ys-input-nickname').val(json.username).hide();
					$('#ys-input-message').css('width', '290px');
					$('span', '#shoutbox_loggedin_actions').html('Logged in as ' + json.username + '.');
					$('#shoutbox_loggedin_actions').show();
				}
				else {
					$('#shoutbox_user_actions').show();
				}
				$('#shoutbox_message').text(json.message).show();
			},
			'json'
		);
	});

	$('#shoutbox_logout').bind('click', function(e){
		e.preventDefault();
		$.getJSON(
			'/page/ys_logout',
			{},
			function(json){
				$('#ys-input-message').css('width', '200px');
				$('#ys-input-nickname').val('Name').removeClass('ys-after-focus').addClass('ys-before-focus').show();
				$('#shoutbox_loggedin_actions').hide();
				$('#shoutbox_user_actions').show();
				$('#shoutbox_message').text(json.message).show();
			}
		);
	});
	
	if (ys_user) {
		var ys_user_load = setInterval(function(){
			if ($('#ys-post-form').length) {
				$('#ys-input-nickname').val(ys_user.username).hide();
				$('#ys-input-message').css('width', '290px');
				$('#shoutbox_user_actions').hide();
				if (ys_user.username) $('span', '#shoutbox_loggedin_actions').html('Logged in as ' + ys_user.username + '.');
				$('#shoutbox_loggedin_actions').show();
				clearInterval(ys_user_load);
			}
		}, 100);
	}
});

