$(document).ready(function () {
	// Thanks to jqueryfordesigners.com/fixed-floating-elements for this code.
	var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0)),
		left = $('#sidebar').offset().left;
	
	// Since the sidebar is floating right instead of positioned absolutely, we have to
	// grab its offset and add it to the fixed position to make sure this works.
	$('<style> .fixedPosition { left: ' + left + 'px; } </style>').appendTo('head');

	$(window).scroll(function () {
		var y = $(this).scrollTop();

		if (y >= top) {
			$('#sidebar').addClass('fixed');
			$('#sidebar').addClass('fixedPosition');
		}
		else {
			$('#sidebar').removeClass('fixed');
			$('#sidebar').removeClass('fixedPosition');
		}
	});

	// MEGA thanks to github.com/bigspotteddog for this code, which plays nice with the footer!
	// We check to see if media queries have adapted for mobile. If so, we don't want it fixed.
	/*if ($('#sidebar').css('top') === 'auto') {
		$('#sidebar').scrollToFixed({
			limit: $('footer').offset().top - $('#sidebar').outerHeight() - 10
		});
	}*/

	// Expanding code for new posts, threads, and messages.
	$('.expandy').keyup(function (event) {
		if ($(this).val() != '') {
			$('.expander').slideDown(100);
		}
		else if ($(this).val() == '' && $('.expander textarea').val() == "") {
			$('.expander').slideUp(100);
		}
	});
});
