
function wrap(quem){
	var larg_total,larg_carac,quant_quebra,pos_quebra, over_orig;
	var elementos,quem, pai, caracs, texto, pai_texto, display_orig, wid_orig;
    if(quem.nodeType==3){ if(quem.nodeValue.replace('\n','').replace('\t','').trim()=='') { return true; }
		pai = quem.parentNode; texto = quem.nodeValue; display_orig = pai.style.display; over_orig = pai.style.overflow; wid_orig = pai.style.width;
		pai.style.display="block"; pai.style.overflow="hidden"; larg_oficial = pai.offsetWidth; pai.style.display="table"; pai.style.width = "auto";
		pai.style.overflow = "visible"; larg_total = pai.offsetWidth; pai.style.overflow = over_orig;
		
		if(larg_total>larg_oficial){ pos_quebra = 0; caracs = pai.textContent.length; quem.nodeValue = pai.textContent.replace(/ /g,"Ø") + " ";
			larg_total = pai.offsetWidth; pai.style.display = display_orig;			
			larg_carac = larg_total / caracs ; quant_quebra = parseInt(larg_oficial/larg_carac) - 2; quant_quebra = quant_quebra>0 ? quant_quebra : 1 ;
			quem.nodeValue = '';
			while(pos_quebra<=caracs){ quem.nodeValue += texto.substring(pos_quebra, pos_quebra + quant_quebra) + " "; pos_quebra = pos_quebra + quant_quebra; }			
		}
		pai.style.display = display_orig; pai.style.display = over_orig; pai.style.width = wid_orig;
	}
	else if (quem.childNodes.length == 1 && quem.childNodes[0].nodeType == 3) {
		texto = String(quem.innerHTML); display_orig = quem.style.display; over_orig = quem.style.overflow; wid_orig = quem.style.width;
		quem.style.display = "block"; quem.style.overflow = "hidden"; larg_oficial = quem.offsetWidth; quem.style.display = "table";
		quem.style.width = "auto"; quem.style.overflow = "visible"; larg_total = quem.offsetWidth; quem.style.overflow = over_orig;
		if(larg_total>larg_oficial){
			pos_quebra = 0; caracs = texto.length; quem.innerHTML = quem.innerHTML.replace(/ /g,"Ø"); larg_total = quem.offsetWidth; larg_carac = larg_total / caracs ;
			quant_quebra = parseInt(larg_oficial/larg_carac) - 2;  quant_quebra = quant_quebra>0 ? quant_quebra : 1 ; quem.innerHTML = ""
			while(pos_quebra<=caracs){ quem.innerHTML += texto.substring(pos_quebra, pos_quebra + quant_quebra) + " "; pos_quebra = pos_quebra + quant_quebra; }
		}	
		quem.style.display = display_orig; quem.style.display = over_orig; quem.style.width = wid_orig;
	}
	else if(quem.childNodes.length>0){ for(var i=0;i<quem.childNodes.length;i++){ wrap(quem.childNodes[i]); } }
}

function wordWrap() {var elementos = document.body.getElementsByTagName("*"); if(navigator.appName.indexOf("Internet Explorer")>-1){ for(var i=0; i<elementos.length;i++){ if(elementos[i].className.indexOf("word-wrap")>-1){ elementos[i].style.wordWrap = "break-word"; } } } else { for(var i=0; i<elementos.length;i++){ if(elementos[i].className.indexOf("word-wrap")>-1){ wrap(elementos[i]); } } } }
String.prototype.trim = function() { return this.replace(/^[ ]+|[ ]+$/g,""); }
function bodyOnReady(func) { if(!(document.body==null)){ func(); } else { var func_rep = func; setTimeout(function(){ bodyOnReady(func_rep) },100); } }
bodyOnReady(wordWrap);
