function checkClosed(){
	$.each($(".content_block").filter(".open"), function(i, val){
		w -= 200;
		changeLayout(w);
		
		$(val).removeClass("open").animate({width: "200px"}, d)
				.find(".content_bottom").animate({width: "164px"}, d )
				.find(".more").removeClass("open").html("MORE");
	});
}

function addMore(){
	$(".block_more").click(function(e){
		
		checkClosed();
		
		var tmpThis = $(this);
		var tmpTarget = $(this).parent();
		var tmpCat = $(this).find("span.category").text();
		var tmpOffset = $(this).find("span.offset").text();
		
		if( tmpCat == ""){ tmpCat = 2; }
		if( tmpOffset == ""){ tmpOffset = 25; }

		$.ajax({ url: "/ajax/entries-offset/" + tmpCat + "/" + tmpOffset, success: function(data){
			
			$("a.fancy, a.video, a.audio").colorbox();
		
			if( data.length > 0 )
			{
				tmpThis.remove();
				tmpTarget.append(data);
			
				w += (200 * ($(data).find("span.count").text() ));
			
				if( tmpOffset == 25 )
				{
					w += 200;
				}
				controlWrappersLayout(w);
			
				addMore();
				checkMore();
			}
		}});
		
	});
}

function checkMore(){
	$(".content_block").not(".title").unbind('click');
	$(".content_block").not(".title").click(function(e){
		
		if(!$(e.target).hasClass("ext")){
			
			$(".content_block, .content_bottom, #top_line, #bottom_line, #middle_line, #content_wrapper").stop(true, true);
			
			if(!$(this).hasClass("open")){

				// Close all open boxes
				checkClosed();
				
				// Open the clicked one
				w += 200;
				controlWrappersLayout(w);
				
				var tmpTarget = $(this).find(".content_right");
				
				$.ajax({ url: "/ajax/client-detail/" + $(this).find(".content_right").attr("id"), success: function(data){
					tmpTarget.find("ul").remove();
					tmpTarget.append(data);
					$("a.fancy, a.video, a.audio").colorbox();
				}});
				
				$(this).addClass("open").animate({width: "400px"}, d )
					   .find(".content_bottom")
					   .animate({width: "364px"}, d )
					   .find(".more").addClass("open").html("LESS");
			}
			else
			{
				w -= 200;
				
				$(this).removeClass("open").animate({width: "200px"}, d )
					.find(".content_bottom")
					.animate({width: "164px"}, d )
						.find(".more").removeClass("open").html("MORE");
				
				controlWrappersLayout(w);
			}
		}
		
	});
	
}

$(document).ready(function(){
	var count = $("#content .content_block").length;
	w = 16 + (202 * count) + 50;
	$("#content_wrapper").width(w);
	changeLayout(w);
	
	$(window).resize(function(){
		changeLayout(w);
	});
	
	$("a.print").click(function(){
		var project = $(this)
		var title = project.attr("title");
		var url = project.attr("href");
		var print_window = window.open(url, "Print", "width=860,height=580,scrollbars=yes");
		print_window.onload = function(){print_window.print();};
		return false;
	});
	
	$("a.mail").click(function(){
		var email = window.prompt("Give up the emailaddress to send this to, please", "");
		var project = $(this);
		var title = project.attr("title");
		var url = project.attr("href");
		window.open("mailto:"+email+"?subject=LDV website - "+title+"&body="+url,"_self");
		return false;
	});
	
	$("#content .content_inner")
	.filter(function(index){
		return !$(this).parent().hasClass(".title");
	})
	.hover(
	  function () {
		$(this).addClass("active").find(".star").addClass("star_hover").removeClass("star");
	  }, 
	  function () {
		$(this).removeClass("active").find(".star_hover").addClass("star").removeClass("star_hover");
	  }
	);
	
	addMore();
	
	checkMore();
	
	var hash = location.hash;
	if(hash != ""){
		$(hash).trigger("click");
	}
	
});

