function bindLogin(){
	/* attach a submit handler to the form */
	jQuery('#login-small').submit(function(event){
		/* stop form from submitting normally */
		event.preventDefault();
		$('.ajax-load-login').show();
		/* get some values from elements on the page */
		var $form = $(this);
		var url = $form.attr('action');
		var username = $form.find('input[name="username"]').val();
		var password = $form.find('input[name="password"]').val();
		var remember = $form.find('input[name="remember"]:checked').val();
		var redirect_to = $form.find('input[name="redirect_to"]').val();
		/* send the data using post */
		jQuery.post(url, { username: username, password: password, remember: remember, redirect_to:redirect_to, btnLogin: 'Login' }, 'html')
		.success(function(data) {
			var errorBox = $(data).find('.error-box');
			var usermeta = $(data).find('#user-outer');
		  $.ajax({
			  url: window.location.href,
			  success: function(data){
					var register = $(data).find('.ajax_register');
					var comments = $(data).find('#comments_container');
					var rating = $(data).find('.game-rating');
					var discussions = $(data).find('ul#discussions_list');
					var discussion = $(data).find('#single_discussion_view');
					$('.error-box').remove();					
					$('.ajax_register').replaceWith(register);
					$('.ajax-load-login').hide();
					$("#user-outer").empty().replaceWith(usermeta);
					$("#login-small").prepend(errorBox);
					$(".closable").show();
					// Add the close link to all boxes with the closable class
					$('.closable').append('<a href="#" class="close">close</a>');
					if ($('#comments_container').hasClass('comments')) {
						$('#comments_container').empty().replaceWith(comments);
					}
					$('#single_discussion_view').empty().replaceWith(discussion);
					$('ul#discussions_list').empty().replaceWith(discussions);
					if ($('.game-rating').hasClass('game-rating')) {
						$('.game-rating').empty().replaceWith(rating);
						$('.star-radio').rating({
							required: true,
							callback: function(value, link) {
								$.ajax({
									type: "post",
									url: $('#rating_url').val(),
									dataType: "json",
									data: "&game_id=" + $('#game_id').val() + "&rate_val=" + value,
									success: function(e) {
										//$('#form_rating').append(e.code + '<br />' + e.msg);
									}
								});
							}
						});
					}
					bindLogin();
					bindRegister();
			  }
			});
		  bindLogin();
		  bindRegister();
		})
	});
}

function bindRegister(){
	/* attach a submit handler to the form */
	jQuery('#register').submit(function(event){
		/* stop form from submitting normally */
		event.preventDefault();
		$('.ajax-load-register').show();
		/* get some values from elements on the page */
		var $form = $(this);
		var url = $form.attr('action');
		var d0ntf1llth1s1n = $form.find('input[name="d0ntf1llth1s1n"]').val();
		var username = $form.find('input[name="username"]').val();
		var password = $form.find('input[name="password"]').val();
		var confirm_password = $form.find('input[name="confirm_password"]').val();
		var email = $form.find('input[name="email"]').val();
		var email_permission = $form.find('input[name="email_permission"]:checked').val();
		var accept_terms = $form.find('input[name="accept_terms"]:checked').val();
		/* send the data using post */
		jQuery.post(url, { username: username, password: password, d0ntf1llth1s1n: d0ntf1llth1s1n, confirm_password: confirm_password, email: email, email_permission: email_permission, accept_terms: accept_terms }, 'html')
		.success(function(data) {
			var errorBox = $(data).find('.error-box');
			$.ajax({
				url: window.location.href,
				success: function(data){
					var register = $(data).find('.ajax_register');
					var comments = $(data).find('#comments_container');
					var rating = $(data).find('.game-rating');
					var usermeta = $(data).find('#user-outer');
					$('.error-box').remove();
					$('.ajax_register').replaceWith(register);
					$('.ajax-load-register').hide();
					$("#ajax_register").prepend(errorBox);
					$('input[name="username"]').val(username);
					$('input[name="email"]').val(email);
					$("#user-outer").empty().replaceWith(usermeta);
					if ($('#comments_container').hasClass('comments')) {
						$('#comments_container').empty().replaceWith(comments);
					}
					if ($('.game-rating').hasClass('game-rating')) {
						$('.game-rating').empty().replaceWith(rating);
						$('.star-radio').rating({
							required: true,
							callback: function(value, link) {
								$.ajax({
									type: "post",
									url: $('#rating_url').val(),
									dataType: "json",
									data: "&game_id=" + $('#game_id').val() + "&rate_val=" + value,
									success: function(e) {
										//$('#form_rating').append(e.code + '<br />' + e.msg);
									}
								});
							}
						});
					}
					bindLogin();
					bindRegister();
				}
			});
			bindLogin();
			bindRegister();
		});
	});
}

$(document).ready(function()
{
	bindLogin();
	bindRegister();
	
	// Add the close link to all boxes with the closable class
	$('.closable').append('<a href="#" class="close">close</a>');
	
	// Close the notifications when the close link is clicked
	$('a.close').live('click', function(e)
	{
		e.preventDefault();
		$(this).fadeTo(200, 0); // This is a hack so that the close link fades out in IE
		$(this).parent().fadeTo(200, 0);
		$(this).parent().slideUp(400, function()
		{
			$(this).remove();
		});
	});
});
