var collapsed = "false";

function showLatest() {
   $("div.recentposts").find("a:eq(0)").each(function(i){
//     $(this).parent().parent().parent().prev().append(
//         $(this).clone().html("Last: " +this.title  + " &raquo;").addClass("lastpost").attr('title','by '+ $(this).children('strong').html())
//      );
     $(this).parent().parent().parent().parent().addClass("collapsed");
   });
 $("span.topicMeta").each(function(i){
	$(this).click(function() {
	thisParent = $(this).parent().parent();
	if (thisParent.attr("class").indexOf("collapsed") != -1) {
		thisParent.children("div.recentposts").animate({
			height: 'show',
                }, 'slow');
		thisParent.parent().toggleClass("collapsed");
		//alert(thisParent.parent().attr("class"));
	}	
	});
   });
//   $(".topicList dd").each(function(j) {
//	$(this).mouseout(function() {
//	if ($(this).attr("class").indexOf("collapsed") == -1) {
	//	$(this).children("div.recentposts").animate({
        //                 height: 'hide',
        //       }, 'slow');
        //        thisParent.parent().toggleClass("collapsed");

//	}
//	});
//   });
}	

// called if user preference is for collapsed:
function collapse() {
   $("div.recentposts").hide();
   $("#collapser").html("Expand All topics");
   showLatest();
   collapsed = "true";
}

//when dcoument ready:
$(function() {
if (collapsed == "false") {
   var topicMetaWidth = 
   $("#collapser").toggle(function(e) {
                e.preventDefault();
		$("div.recentposts").animate({
			height: 'hide',
			//opacity: 'hide'
		}, 'slow');
               
               $(this).html("Expand All topics");
                showLatest();

              
	}, function() {
		$("div.recentposts").animate({
			height: 'show',
			//opacity: 'show'
		}, 'slow');
                $(".collapsed").removeClass("collapsed");
		$(this).html("Collapse All topics");
               $("a.lastpost").remove();	
	});
} else {
   $("#collapser").toggle(function(e) {
                e.preventDefault();
		$("div.recentposts").animate({
			height: 'show',
			//opacity: 'show'
		}, 'slow');
                $(".collapsed").removeClass("collapsed");

                  $(this).html("Collapse All topics");
               $("a.lastpost").remove();             

              
	}, function() {
		$("div.recentposts").animate({
			height: 'hide',
			//opacity: 'hide'
		}, 'slow');
            $(this).html("Expand All topics");
                showLatest();

	});
}

});

