$(document).ready(function()
{
	$(".navigation ul li:first").addClass("first");
	
	$(".fixResults .block-bordered, .fixResults .block-bordered-small").each(function()
	{
		if ($(this).text().length <= 10)
			$(this).remove();
	});
	
	if ($(".masonry .box").length > 0)
	{
		$(".content").addClass("loading");
	}
	
	$(".press-line").each(function()
	{
		var thisLine = $(this);
		thisLine.find(".preview_link").click(function()
		{
			$(".popup").hide();
			$(".layer").remove();
			$("body").prepend('<div class="layer" />');
			$(".layer").width($(window).width()).height($(document).height());
			$(".layer").fadeIn(500);
			
			var prevLine = thisLine.prev();
			var nextLine = thisLine.next();
			if (prevLine.find(".popup").length == 0) thisLine.find(".previous_article").hide(); else thisLine.find(".previous_article").show();
			if (nextLine.find(".popup").length == 0) thisLine.find(".next_article").hide(); else thisLine.find(".next_article").show();
			
			if (thisLine.find(".previous_article").is(":visible") && thisLine.find(".next_article").is(":visible"))
				thisLine.find(".separator").show();
			else
				thisLine.find(".separator").hide();
			
			thisLine.find(".popup").fadeIn(500);
			
			return false;
		});
		
		var selectedSliderItem = thisLine.find(".popup .slider li.selected");
		if (!selectedSliderItem.length > 0) selectedSliderItem = thisLine.find(".popup .slider li:first");
		selectedSliderItem.addClass("selected");
		thisLine.find(".full-image").html('<img src="'+selectedSliderItem.find("a").attr("href")+'" alt="" />');
		
		thisLine.find(".popup .slider li a").click(function()
		{
			thisLine.find(".popup .slider li.selected").removeClass("selected");
			$(this).parent().addClass("selected");
			
			thisLine.find(".full-image").html('<img src="'+$(this).attr("href")+'" alt="" />');
			
			return false;
		});
		
		thisLine.find(".previous_article").click(function()
		{
			var nextLine = thisLine.prev();
			if (nextLine.find(".popup").length == 0) return false;
			
			$(".popup").hide();
			
			var prevLine2 = nextLine.prev();
			var nextLine2 = nextLine.next();
			if (prevLine2.find(".popup").length == 0) nextLine.find(".previous_article").hide(); else nextLine.find(".previous_article").show();
			if (nextLine2.find(".popup").length == 0) nextLine.find(".next_article").hide(); else nextLine.find(".next_article").show();
			
			if (nextLine.find(".previous_article").is(":visible") && nextLine.find(".next_article").is(":visible"))
				nextLine.find(".separator").show();
			else
				nextLine.find(".separator").hide();
			
			nextLine.find(".popup").show();
			
			return false;
		});
		
		thisLine.find(".next_article").click(function()
		{
			var nextLine = thisLine.next();
			if (nextLine.find(".popup").length == 0) return false;
			
			$(".popup").hide();
			
			var prevLine2 = nextLine.prev();
			var nextLine2 = nextLine.next();
			if (prevLine2.find(".popup").length == 0) nextLine.find(".previous_article").hide(); else nextLine.find(".previous_article").show();
			if (nextLine2.find(".popup").length == 0) nextLine.find(".next_article").hide(); else nextLine.find(".next_article").show();
			
			if (nextLine.find(".previous_article").is(":visible") && nextLine.find(".next_article").is(":visible"))
				nextLine.find(".separator").show();
			else
				nextLine.find(".separator").hide();
			
			nextLine.find(".popup").show();
			
			return false;
		});
	});
	
	$(".popup .close").click(function()
	{
		$(".popup").fadeOut(500, function(){ $(".layer").fadeOut(500, function() { $(".layer").remove(); }); });
		
		return false;
	});
	
	if ($(".legal-popup").length > 0 && $.cookie('legal_age') != "yes")
	{
		$(".layer").remove();
		$("body").prepend('<div class="layer" />');
		$(".layer").width($(window).width()).height($(document).height());
		$(".layer").show();
		$(".legal-popup").show();
		
		$(".legal-popup .answer-yes").click(function()
		{
			$(".layer").fadeOut(500);
			$(".legal-popup").fadeOut(500);
			$.cookie('legal_age', 'yes', { expires: 1 });
			
			return false;
		});
	}
});

$(window).load(function()
{
	$(".actions:not(.press-line .actions)").html($(".articles").html());
	$(".articles").remove();
	
	$('.masonry').masonry({
		columnWidth: 261,
		itemSelector: '.box:visible'
	});
	
	if ($(".sidebar").height() > $(".content").height())
	{
		$(".content").height($(".sidebar").height() - 20);
	}
	
	$(".block-place").each(function()
	{
		var thisBlock = $(this);
		thisBlock.find(".button, .region").click(function()
		{
			thisBlock.toggleClass("block-closed").toggleClass("block-opened");			
			thisBlock.find(".block-content").slideToggle('slow');
			
			return false;
		});
	});
});
