<!-- SOME STATE VARIABLES -->
var most_recent_post = null;
var shift_down = false;
var is_logged_in = false;
<!-- STARTUP STUFFS -->
$(document).ready(function() {
	$("#register-div").hide();
	$("#help-div").hide();
	$("#input-div").hide();
	$("#settings-div").hide();
	verify_login();//check if already logged in (this is kinda haxish, but hey!)
	update(); //do a quick update to load initial posts	
	$("#loading").hide();
	setInterval("update()",2000);
});
<!-- AJAX UPDATING FUNCTIONS -->
function update()
{
	var url = "load_posts.php?salt="+Math.random();
	if(most_recent_post != null) url += "&id="+most_recent_post;
	$.getJSON(url,function(data) {
		if(data.posts.length == 0 && data.users.length == 0) {
			$("#whos-online").html(""); //yea.. this isnt always empty when this is going on :D
			return;
		}
		$.each(data.posts,function(i,item) {
			var count = $("#"+item.id).length;
			if(0 >= count) {
				most_recent_post = item.id;
				$("#left-column").prepend('<div class="post-body" id="'+item.id+'" style="background: #'+item.color+';"><b>'+item.name+'</b><hr>'+item.message+'<br /><div class="post-footer">Posted by: '+item.name+' @ '+item.time+' EST</div></div>');
				$("#"+item.id).hide().fadeIn("slow");
			}
		});
		$("#whos-online").html("");
		$.each(data.users,function(i,item) {
			$("#whos-online").append("<li>"+item.name+"</li>");
		});
	});
}
<!-- FIELD EVENT LISTENERS -->
$(function() {
	$(".text-field").focus(function() {
		$(this).addClass("textfield-focused");
	}).blur(function() {
		$(this).removeClass("textfield-focused");
	});
});
$(function() {
	$("#register-cpassword-field").bind("keyup keydown change",function(event) {
		var password = $("#register-password-field").val();
		var cpassword = $(this).val();
		if(password != cpassword)
			$(this).addClass("password-no-match");
		else
			$(this).removeClass("password-no-match");
	});
});
$(function()
{
	$("#message-field").keydown(function(event) {
		if(event.keyCode == 13) {
			if(!shift_down) {
				send_message();
				event.preventDefault();
			}
		}
		return true;
	});
});
<!-- LINK LISTENERS -->
$(function() {
	$("#help-link-show").click(function(event) {
		$("#help-div").show("slow");
		$(this).fadeOut("slow");
		event.preventDefault();
	});
	$("#help-link-hide").click(function(event) {
		$("#help-div").hide("slow");
		$("#help-link-show").fadeIn("slow");
		event.preventDefault();
	});
	$("#logout-link").click(function(event) {
		$.ajax({
			   type: "GET",
			   url: "login.php",
			   data: "logout",
			   success: function(msg) {
				   verify_login();
			   }
		});
		event.preventDefault();
	});
	$("#settings-link").click(function(event) {
		$("#settings-div").show("slow");
		event.preventDefault();
	});
});
<!-- BUTTON LISTENERS -->
$(function() {
	$("#register-button").click(function(event) {
		$("#login-div").hide("slow");
		$("#register-div").show("slow");
	});
	$("#register-cancel-button").click(function(event) {
		$("#login-div").show("slow");
		$("#register-div").hide("slow");
	});
	$("#settings-cancel").click(function(event) {
		$("#settings-div").hide("slow");
	});
});
<!-- FORM LISTENERS -->
$(function() {
	$("#register-form").submit(function(event) {
		register();
		event.preventDefault();
	});
});
$(function() {
	$("#login-form").submit(function(event) {
		login();
		event.preventDefault();
	});
});
$(function() {
	$("#save-color-form").submit(function(event) {
		var color = $("#change-color-field").val();
		$.ajax({
			   type: "POST",
			   url: "savecolor.php",
			   data: "color="+color+"&salt="+Math.random(),
			   success: function(msg) {
				   $("#settings-div").hide("slow");
				   most_recent_post = null;
				   $("#left-column").html("");
				   update();
			   }
		});
		event.preventDefault();
	});
});
<!-- MISC GLOBAL LISTENERS -->
$(function() 
{
    $(document).keydown(function(event) {
        if(event.keyCode == 16)
			shift_down = true;
    }).keyup(function(event) {
		if(event.keyCode == 16)
			shift_down = false;
	});
});
<!-- POST SENDING FUNCTION -->
function send_message()
{
	if(!verify_login()) return; //easy, no?
	var message = jQuery.trim($("#message-field").val());
	if(message == "") return;
	$("#message-field").attr("disabled","disabled").val("Posting...");
	$.ajax({
		   type: "POST",
		   url: "post_message.php",
		   data: "message="+message+"&salt="+Math.random(),
		   success: function(msg) {
			   if(msg == 1) {
				   update();
				   $("#message-field").removeAttr("disabled").val("");
			   }
			   else {//maybe the cookie expired? o_0
			   		verify_login();
				   $("#message-field").removeAttr("disabled").val(""); //heh. realized that if you dont do that they log back in and find themselves unable to post!
			   }
		   }
	});
}
<!-- REGISTER AND LOGIN FUNCTIONS -->
function register()
{
	//ok, so, if they managed to get to this point and didn't type their password in right? not my problem!
	var name = $("#register-name-field").val();
	var password = $("#register-password-field").val();
	$.ajax({
			   type: "POST",
			   url: "register.php",
			   data: "name="+name+"&password="+password+"&salt="+Math.random(),
			   success: function(msg) {
					if(msg == 0)
				   		$("#register-error").html("Invalid Name or Password");
					else if(msg == 1)
						$("#register-error").html("That Name is Already in Use");
					else if(msg == 2) {
						$("#register-div").html("<span style='color:#0F0;'>Successfully Registered!</span>").fadeOut(5000);
						$("#login-div").show("slow");
						$("#register-button").hide();
					}
					else
						$("#register-error").html("WTF? You hit an unhittable easter egg? The fuck?!?!?!?");
			   }
	});
}
function login()
{
	var name = $("#login-name-field").val();
	var password = $("#login-password-field").val();
	$.ajax({
		   type: "POST",
		   url: "login.php",
		   data: "name="+name+"&password="+password+"&salt="+Math.random(),
		   success: function(msg) {
				if(msg == 0)
					$("#login-error").html("Invalid Name or Password");
				else if(msg == 1)
					$("#login-error").html("Invalid Name/Password Combination");
				else if(msg == 2)
					verify_login();
				else
					$("#login-error").html(msg);
		   }
	});
}
function verify_login()
{
	$.ajax({
		   type: "GET",
		   url: "login.php",
		   data: "verify",
		   success: function(msg) {
			   if(msg == 1) {
					$("#login-div").hide();
					$("#login-error").hide();
					$("#input-div").show();
					is_logged_in = true;
			   }
			   else
			   {
				   $("#login-div").show();
				   $("#input-div").hide();
					is_logged_in = false;
			   }
		   }
	});
	return is_logged_in;
}
<!-- MISC SUPPORT FUNCTIONS -->