$(document).ready(function() {

	$(window).scroll(function(){
		if($(window).scrollTop() > 700)
		{
			$('.up-button').show();
		}
		else
		{
			$('.up-button').hide();
		}
	});

	$('.up-button').click(function(){
		$("html, body").animate({ scrollTop: 0 }, "slow");
	});

	$("a.toggle_bottom").click(function() {
		var a = $(this);
		$("html, body").animate({ scrollTop: $(a.attr('href')).position().top - 50 }, "slow");
		return false;
	});

	$('a#load-post').click(function() {

		var button = $(this), loading = $('.loading-post'), offset = button.data('offset'), tag = button.data('tag');

		button.hide();
		loading.show();

		$.ajax({
			method: 'POST',
			url: "/blog/post/load",
			data: {
				offset: offset,
				tag: tag
			},
			success: function(response) 
			{
				$('.loaded').append(response.posts);
				button.data('offset', response.offset);

				if(response.count <= response.offset)
				{
					button.hide();
				}
				else
				{
					button.show();
				}

				loading.hide();
			}
		});

		return false;

	});

	$.ajax({
		url: "/sessions/default/add"
	});

	setInterval(function(){
		$.ajax({
			url: "/sessions/default/update"
		});
	}, 1000 * 60 * 5);
	

	$('form#error-testing-form').on('beforeSubmit', function(e) {
	    $.ajax({
	        url: $('form#error-testing-form').attr('action'),
	        method: 'POST',
	        dataType: 'json',
	        data: {
	            message: $('[name="message"]').val()
	        },
	        success: function(response){
	            if(response.success)
	            {
	                $(".error_fedback").html('<div class="row"><div class="col-sm-8" style="margin:0 auto;float:none;"><div class="gray_box"><p>' + response.message + '</p></div></div></div>');
	            }
	        }
	    });
	}).on('submit', function(e){
	    return false;
	});
});