﻿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); 
		
		while (texto.search(/\s\s/g) > -1) {
			texto = texto.replace(/\s\s/, ' ');
		}
		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,"Ø");
			quem.innerHTML = texto.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 wordwrapByElementId(eleId){
	if (document.createEvent && document.getElementById(eleId) != null) {
		var main = document.getElementById(eleId);
	    main.addEventListener('overflow', function(e){
	    	document.getElementById(eleId).className="wordwrap" 
	    }, false);
	}
}
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 substringReplace(strText, startChar, maxChar, addEndStr) {
	var tempStr = strText;
	if(strText.length > 0 && strText.length > maxChar) {
		var temp = strText.substring(startChar, maxChar);
		var lastSpaceIndex = temp.lastIndexOf(" ", temp);
		var lastltIndex = temp.lastIndexOf("<", temp);
		var lastgtIndex = temp.lastIndexOf(">", temp);
		
		if(lastltIndex > lastgtIndex || lastgtIndex == maxChar) {
			tempStr = strText.substring(startChar, lastltIndex) + addEndStr;
		}else if(lastSpaceIndex > 0) {
			tempStr = strText.substring(startChar, lastSpaceIndex) + addEndStr;		
		}else {
			tempStr = strText.substring(startChar, maxChar) + addEndStr;		
		}	
	}
	return tempStr;
}