$(document).ready(function() {
	$('.opener').click(function(){
		$(this).parents('tr').toggleClass('open-row'); 
		$(this).parents().next('tr').toggle(); 
		return false;
	})
	
	//caption any images in the page-content that have alt texts
	addImageCaptions();
});


function addImageCaptions(){
	$("#page-content img[alt]").each(function(idx){
		var img = $(this);
		if(img.attr("alt").length){
			//only for those with non-empty alts
			var wrapper = $("<div class=\"thumbnail\"></div>");
			var parent = img.parent();
			var caption = $("<span>" + img.attr("alt") + "</span>");
			//Sort out the alignment : Use the align attr but remove it from the image
			var alignment = "left";
			if(img.attr("align") == "right") alignment = "right";
			img.removeAttr("align");
			//Steal the images padding and margin and alignment.
			wrapper.css(
					{
						"marginLeft":img.css("marginLeft"),
						"marginTop":img.css("marginTop"),
						"marginBottom":img.css("marginBottom"),
						"marginRight":img.css("marginRight"),
						"paddingLeft":img.css("paddingLeft"),
						"paddingRight":img.css("paddingRight"),
						"paddingTop":img.css("paddingTop"),
						"paddingBottom":img.css("paddingBottom"),
						"float":alignment
					});			
			//Reset the image to no padding and margin
			img.css({"margin":"0", "padding":"0", "border":"none"});
			//Set the wrapper to have the image width
			wrapper.width(img.width());
			//Check to see if IMG was enclosed in an A (with no siblings)
			if(parent[0].tagName == "A" && img.siblings().length == 0){
				//Has a link around it and is only child : Wrap it around the A instead
				parent.wrap(wrapper);
			}else{
				//Wrap it around the image
				img.wrap(wrapper);
			}
			//append the caption
			img.after(caption);			
		}
	});
}