// Addons for display CMS HTML content 

$(document).ready(function(){
	$(".content-body > p").each(function(){
		var text = $(this).html();
		if( text == "" || text == "&#160;" || text == "&nbsp;"){
			$(this).remove();
		}
	});
	/*$(".content-body > p > img").each(function(){
		var text= $(this).attr( "alt");
		if(text){
			var span = $(this).wrap("<div />");
			$(this).attr("align", "bottom");
			$(span).after("<div style=\"width: " + $(this).width()+"px; padding-bottom: 12px; margin: 5px;\">" + text + "</div>");
		}
	});*/
	var rfunc = function(){
		var tit = $(this).attr( "title");
		var alt = $(this).attr( "alt");
		if( tit || alt){
			var text = "";
			if(tit){
				text = "<div class=\"imtitle-text\">" + tit + "</div>";
			}
			if(alt){
				text += "<div class=\"imtitle-author\">Фото: " + alt + "</div>";
			}
			var hSpaceAdd = 0;
			var vSpaceAdd = 0;
			if( $(this).attr("hspace")) hSpaceAdd = $(this).attr("hspace");
			if( $(this).attr("vspace")) vSpaceAdd = $(this).attr("vspace");
			var space = 5;
			var width = $(this).width() - 2 * space;
			var div = $("<div class=\"imtitle\">" + text + "</div>");			
			
			var span = $(this).wrap("<div style=\"overflow: hidden; width: " + ($(this).width() + 2 + 2 * hSpaceAdd) + "px; height: " + ($(this).height() + 2 + 2 * vSpaceAdd) + "px;\" />");
			var externalDiv = $("<div style=\"width: " + width + "px; padding: " + space + "px;\" class=\"imtitle-container\" />");
			$(span).after(externalDiv);
			externalDiv.append(div);			
			var height = div.height();
			externalDiv.css( "height", height + "px");
			externalDiv.css( "position", "relative");
			externalDiv.css( "top", "-" + (height + 4 * space) + "px");
			//div.css( "background-color", "#aaa");
			externalDiv.css( "margin", space + "px");
			//externalDiv.css( "border-radius", "4px 4px 4px 4px");
			//div.css( "opacity", "0.5");			
			//div.addClass( "imtitle");
			
			//$(this).attr("align", "bottom");
			//$(span).after("<div style=\"position: relative; top: -" + (height + 2 * space) +"px; width: " + width + "px; height: " + height + "px; background-color: red; margin: " + space + "px;\">" + text + "</div>");
		}
	}
	$(".content-body > p > img").each( rfunc);	
	$(".imalt > img").each( rfunc);	
});
