$(document).ready(function()
{
	var Username = '';
	var Password = '';
	var login = false;
	var latest_message;
	
	// Scrolling variables
	var bottom_scroll = false;
	var content_text;
	
	function clean_spaces(clean_string)
	{
		while (clean_string.search('  ') != -1)
		{
			clean_string = clean_string.replace('  ', ' ');
		}
		
		return clean_string;
	}
	
	function run_command(command)
	{
		// Clean from unwanted spaces in the beginning of the string
		while (command.search(' ') == 0)
		{
			command = command.substr(1);
		}
		
		command = clean_spaces(command);
		
		if(command.search('/login ') == 0 || command.search('/register ') == 0)
		{
			if(command.search('/login ') == 0)
			{
				command = command.substr(7);
				login = true;
			}
			else
			{
				command = command.substr(10);
				login = false;
			}
			
			if (command.search(' ') != -1)
			{
				Username = command.substr(0, command.search(" "));
				Password = MD5('salt' + command.substr(command.search(' ') + 1) + 'pepper');
				
				if(login)
				{
					$.post('scripts/command.php', {c : '/login ' + Username + ' ' + Password}, function(data)
					{
						if(data == 'true')
						{
							$('#content').append('<p><span style="color: green">You are logged in.</span></p>');
							latest_message = setInterval(function(){
								run_command("/listen");
							}, 3000);
						}
						else
						{
							$('#content').append('<p><span style="color: red">Login is not correct.</span></p>');
						}
					});
				}
				else
				{
					$.post('scripts/command.php', {c : '/register ' + Username + ' ' + Password}, function(data)
					{
						$('#content').append(data);
					});
				}
			}
			else
			{
				alert('Function was used incorrectly. Use it this way: /function %username% %password%');
			}
		}
		else if(command.search('/logout') == 0)
		{
			clearInterval(latest_message);
			login = false;
			Username = ' ';
			Password = ' ';
		}
		else if(command.search('/') == 0)
		{
			command += ' ';
			command = command.substr(0,command.search(' ')) + ' ' + Username + ' ' + Password + command.substr(command.search(' '));
			$.post('scripts/command.php', {c : command}, function(data)
			{
				$('#content').append(data);
			});
		}
		else
		{
			$.post('scripts/command.php', {c : '/say ' + Username + ' ' + Password + ' ' + command}, function(data)
			{
				$('#content').append(data);
			});
		}
	}
	
	// Enter is pressed in the main text field
	$('#command_input').keyup(function(e)
	{
		if(e.keyCode == 13)
		{
			run_command($('#command_input').val());
			$('#command_input').val('');
		}
	});
	
	setInterval(function()
	{
		if(content_text != MD5($("#content").html()))
		{
			if (bottom_scroll)
			{
				window.scroll(0, $(document).height());
			}
			content_text = MD5($("#content").html());
		}
	}, 1000);
	
	$(window).scroll(function()
	{
		if (($(document).scrollTop() + $(window).height()) == $(document).height())
		{
			bottom_scroll = true;
		}
		else
		{
			bottom_scroll = false;
		}
	});
});
