I've updated your code a little bit and now it works as expected. It has the advantag that it fires everything only once if needed and not constantly. I also stored the selector in a variable to avoid a lot of re-querying.
JavaScript
var nav = $('.nav');
var scrolled = false;
$(window).scroll(function () {
if (500 < $(window).scrollTop() && !scrolled) {
nav.addClass('visible').animate({ top: '0px' });
scrolled = true;
}
if (500 > $(window).scrollTop() && scrolled) {
nav.removeClass('visible').css('top', '-30px');
scrolled = false;
}
});
Demo