$(document).ready(function(){
	
	$('input[name="news_categories[]"]').change(function() {
		var data = $("#categories").serialize()
		
		$('input[name="news_categories[]"]').attr('disabled', 'disabled');
		
		$.post("/lib/requests/get_news.php", data, function(response) {
			$('#news-content').fadeOut('slow', function() {
				$("#news-content").html(response);
				$('#news-content').fadeIn();
				$('img.jail').asynchImageLoader({
					offset:240
				}).asynchImageLoader({
					event: "focus"
				});
				$('input[name="news_categories[]"]').removeAttr('disabled');
			});
		});

	});
	
	$('input[name="news_categories[]"]').click(function() {
		
		var n = $('input[name="news_categories[]"]:checked').length;
		
		if(n > 0) {
			$('#show_all').attr('checked', false);
		} else {
			$('#show_all').attr('checked', true);
		}

	});

	$('#show_all').click(function(){

		$('input[name="news_categories[]"]').attr('checked', false);
		
		$.post("/lib/requests/get_news.php", function(response) {
			$('#news-content').fadeOut('slow', function() {
				$("#news-content").html(response);
				$('#news-content').fadeIn();
				$('img.jail').asynchImageLoader({
					offset:240
				}).asynchImageLoader({
					event: "focus"
				});
			});
		});
		$('#show_all').attr('checked', true);
	});
	
	$("img").load(function() {
	
		$("img").each(function(i) {
			if($(this).height() == 1 || $(this).width() == 1) {
				$(this).remove();
			}
		});
	
		//function to resize large images in blog display
		$("img.large_preview").each(function(i) {
					
			var max_width = 645;
					
			if($(this).width() > max_width) {
				var w = max_width;
				var h = Math.ceil($(this).height() / $(this).width() * max_width);
				$(this).css({ height: h, width: w });
			}
		});
	
		//function to resize large images in blog display
		$("img.small_preview").each(function(i) {
					
			var max_width = 250;

			if($(this).width() > max_width) {
				var w = max_width;
				var h = Math.ceil($(this).height() / $(this).width() * max_width);
				$(this).css({ height: h, width: w });
			}
		});
	});

	$('#follow_blog').bind('click', function() {
		
		if(loggedin_base) {

			$.get("/lib/requests/get_blog_subscription.php?bid="+$('#active_blog').val(), function(status) {
				
				location.reload(); //nasty fix
				
				/*
				if(status == 'subscribed') {
					$('#follow_blog').val('Følg ikke!').removeClass('green').addClass('yellow');
					
				} else {
					$('#follow_blog').val('Følg').removeClass('yellow').addClass('green');
				}*/
			});
					
		} else {

			$("#login_required_trigger").trigger('click');

		}
	});
	
});

