$(function() { 

	$(".post").each(function() {
		var postID = $(this).attr("id");
		$(this).children(".header").children(".header-under").before('<div class="header-over"></div>');
		$(this).children(".header").children(".header-over").html($(this).children(".header").children(".header-under").html());
		$(this).children(".header").children(".header-over").css("opacity","0");
		attachHoverEvents($(this).children(".header").children(".header-over"));
	});
	
	$('.content').css('display','none');
	
	/*setTimeout ( function(){
		$(".post:first").children(".content").slideDown('slow');
		removeHoverEvents($(".post:first").children(".header").children(".header-over"));
		$(".post:first").children(".header").children(".header-over").css("opacity","1");
		$(".post:first").children(".header").addClass('open');
	}, 500 );*/
	
	$("div.header").click(function(){
		if ($(this).hasClass('open')) { 
			$(this).removeClass('open');
			$(this).addClass('closed');
			
			var HeaderOn = $(this);
			$(this).siblings(".content").slideUp('slow',function(){
				HeaderOn.children(".header-over").stop().animate({opacity:0},500);
				attachHoverEvents(HeaderOn.children(".header-over"));
			});
			
		} else{
			
			removeHoverEvents($(this).children(".header-over"));
			$(this).children(".header-over").css("opacity","1");
			
			$(this).removeClass('closed');
			$(this).addClass('open');

			$(this).siblings(".content").slideDown('slow');
			
		}
	});
	
	
});

function attachHoverEvents(header) {
    header.mouseover(function() {
		$(this).stop().animate({opacity:1.0},50);
    }).mouseout(function() {
		$(this).stop().animate({opacity:0},1000);
    });
}

function removeHoverEvents(header){
	header.unbind('mouseout');
}