﻿function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}


function tipoBrowser() {
    var tb = null;
    if (navigator.appName == 'Microsoft Internet Explorer') {
        tb = 'ie';
    }
    else {
        tb = 'mz';
    }

    return tb;
}

function fitStringToWidth(str, width, className, insereReticencias) {
    // str    A string where html-entities are allowed but no tags.  
    // width  The maximum allowed width in pixels  
    // className  A CSS class name with the desired font-name and font-size. (optional)  
    // ----  
    // _escTag is a helper to escape 'less than' and 'greater than'
    function _escTag(s) { return s.replace("<", "&lt;").replace(">", "&gt;"); }

    //Create a span element that will be used to get the width  
    var span = document.createElement("span");
    //Allow a classname to be set to get the right font-size.
    if (className) span.className = className;
    span.style.display = 'inline';
    span.style.visibility = 'hidden';
    span.style.padding = '0px';
    document.body.appendChild(span);
    var result = _escTag(str);

    // default to the whole string
    if (tipoBrowser() == 'ie') {
        span.innerText = result;
    }
    else {
        span.innerHTML = result;
    }

    // Check if the string will fit in the allowed width. NOTE: if the width  
    // can't be determinated (offsetWidth==0) the whole string will be returned.  
    if (span.offsetWidth > width) {
        var posStart = 0, posMid, posEnd = str.length, posLength;
        // Calculate (posEnd - posStart) integer division by 2 and    
        // assign it to posLength. Repeat until posLength is zero.    
        while (posLength = (posEnd - posStart) >> 1) {
            posMid = posStart + posLength;
            //Get the string from the begining up to posMid;

            if (insereReticencias) {
                if (tipoBrowser() == 'ie') {
                    span.innerText = _escTag(str.substring(0, posMid)) + '...';
                } else {
                    span.innerHTML = _escTag(str.substring(0, posMid)) + '&hellip;';
                }
            }
            else {
                if (tipoBrowser() == 'ie') {
                    span.innerText = _escTag(str.substring(0, posMid));
                }
                else {
                    span.innerHTML = _escTag(str.substring(0, posMid));
                }
            }

            // Check if the current width is too wide (set new end)
            // or too narrow (set new start)      
            if (span.offsetWidth > width) posEnd = posMid; else posStart = posMid;
        }

        if (insereReticencias) {
            if (tipoBrowser() == 'ie') {
                result = _escTag(str.substring(0, posStart)) + '...';
            } else {
                result = _escTag(str.substring(0, posStart)) + '&hellip;';
            }
        }
        else {
            result = _escTag(str.substring(0, posStart));
        }
    }

    document.body.removeChild(span);

    return result;
}

function LimitarTexto(textoPuro, quantidadeLinhas, larguraDiv, classeCSS) {
    var texto = null;
    var textoOriginal = null;
    var qtdLinhas = quantidadeLinhas;
    var textoTemp = '';
    var proximoCaractere = null;

    texto = '';

    if (tipoBrowser() == 'ie') {
        textoOriginal = replaceAll(textoPuro, '&nbsp;', ' ');
    } else {
        textoOriginal = textoPuro;
    }

    for (var i = 0; i < qtdLinhas; i++) {
        if ((i + 1) == qtdLinhas) {
            textoTemp = fitStringToWidth(textoOriginal, larguraDiv, classeCSS, 0);

            textoTemp = ltrim(textoTemp);

            if (textoOriginal == textoTemp) {
                texto += textoTemp;
            }
            else {
                if (tipoBrowser() == 'ie') {
                    texto += textoTemp.substring(0, textoTemp.length - 4) + '...';
                } else {
                    texto += textoTemp.substring(0, textoTemp.length - 4) + '&hellip;';
                }
            }
        }
        else {
            textoTemp = fitStringToWidth(textoOriginal, larguraDiv, classeCSS, 0);

            proximoCaractere = textoOriginal.substr(textoTemp.length, 1);

            if (proximoCaractere == ' ') {
                textoOriginal = replaceAll(textoOriginal, textoTemp, '');

                texto += textoTemp;
            }
            else {
                textoTemp = textoTemp.substring(0, textoTemp.lastIndexOf(' ')).trim();

                textoOriginal = replaceAll(textoOriginal, textoTemp, '');

                texto += textoTemp;
            }
        }

        texto = ltrim(texto);

        if (tipoBrowser() == 'ie') {
            texto += '\n';
        }
        else {
            texto += '<br />';
        }
    }

    return texto;
}

function LimiteTexto() {
    var divs = null;
    var texto = null;
    var idDiv = null;

    divs = document.getElementsByTagName('div');

    for (var i = 0; i < divs.length; i++) {
		if (divs[i].id.indexOf('litConteudoNoticia') > 0)
			divs[i].style.display='none';
        /*if (divs[i].id.indexOf('litConteudoNoticia') > 0) {
            texto = divs[i].innerHTML;

            idDiv = divs[i].id;

            texto = LimitarTexto(texto, 2, 240, 'noticiaConteudo');

            if (tipoBrowser() == 'ie') {
                divs[i].innerText = texto;
            }
            else {
                divs[i].innerHTML = texto;
            }
        }

        if (divs[i].id.indexOf('litTituloNoticia') > 0) {
            texto = divs[i].innerHTML;

            idDiv = divs[i].id;

            //texto = LimitarTexto(texto, 1, 241, 'noticiaTitulo');
            texto = LimitarTexto(texto, 2, 100, 'noticiaTitulo');

            if (tipoBrowser() == 'ie') {
                divs[i].innerText = texto;
            }
            else {
                divs[i].innerHTML = texto;
            }
        }*/
    }
}