function Trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function GetNumberFromSize(str) {
    return Number(str.replace(/px*/, ''));
}


function GetElementWidth(elem) {
    if (elem.currentStyle) { // sort of, but not really, works in IE
        return GetNumberFromSize(elem.currentStyle["width"]);
    } else if (document.defaultView) { // works in Opera and FF
    return GetNumberFromSize(document.defaultView.getComputedStyle(elem, null).getPropertyValue("width"));
    } else {
        return 0;
    }
}

function FontManager() {
    this.GetFontFromElement = function(elem) {
        var fontFamily = this.GetFontFamily(elem);
        var fontSize = this.GetFontSize(elem);
        var fontWeight = this.GetFontWeight(elem);
        return new FontObject(fontFamily, fontSize, fontWeight);
    }
    this.GetFontSize = function(elem) {
        if (elem.currentStyle) { // sort of, but not really, works in IE
            return elem.currentStyle["fontSize"];
        } else if (document.defaultView) { // works in Opera and FF
            return document.defaultView.getComputedStyle(elem, null).getPropertyValue("font-size");
        } else {
            return null;
        }
    }
    this.GetFontFamily = function(elem) {
        if (elem.currentStyle) { // sort of, but not really, works in IE
            return elem.currentStyle["fontFamily"];
        } else if (document.defaultView) { // works in Opera and FF
            return document.defaultView.getComputedStyle(elem, null).getPropertyValue("font-family");
        } else {
            return null;
        }
    }
    this.GetFontWeight = function(elem) {
        if (elem.currentStyle) { // sort of, but not really, works in IE
            return elem.currentStyle["fontWeight"];
        } else if (document.defaultView) { // works in Opera and FF
            return document.defaultView.getComputedStyle(elem, null).getPropertyValue("font-weight");
        } else {
            return null;
        }
    }
}

function FontObject(fontFamily, fontSize, fontWeight) {
    //Private Fields
    this._FontType = null;
    this._FontDelta = null;
    //Properties
    this.FontFamily = fontFamily;
    this.FontSize = fontSize;
    this.FontWeight = fontWeight;
    this.LightFontType = 0;
    this.NormalFontType = 1;
    this.BoldFontType = 2;
    //Methods
    this.GetNumberFromSize = function(size) {
        return Number(size.replace(/px/, ''));
    }
    this.FontDelta = function() {
    if (this._FontDelta == null) {
        if ((this.FontFamily.toString().indexOf('verdana') != -1) || (this.FontFamily.toString().indexOf('Verdana') != -1)) {
                if (this.FontType() == this.NormalFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.55;
                }
                else if (this.FontType() == this.LightFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.51;
                }
                else if (this.FontType() == this.BoldFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.65;
                }
            }
            else if ((this.FontFamily.toString().indexOf('arial') != -1) || (this.FontFamily.toString().indexOf('Arial') != -1)) {
                if (this.FontType() == this.NormalFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.6;
                }
                else if (this.FontType() == this.LightFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.55;
                }
                else if (this.FontType() == this.BoldFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.7;
                }
            }
            else {
                if (this.FontType() == this.NormalFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.72;
                }
                else if (this.FontType() == this.LightFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.68;
                }
                else if (this.FontType() == this.BoldFontType) {
                    this._FontDelta = this.GetNumberFromSize(this.FontSize) * 0.82;
                }
            }
        }
        return this._FontDelta;
    }
    this.FontType = function() {
        if (this._FontType == null) {
            if (isNaN(this.FontWeight)) {
                switch (this.FontWeight) {
                    case 'lighter':
                        this._FontType = this.LigthFontType;
                        break;
                    case 'bolder':
                        this._FontType = this.BoldFontType;
                        break;
                    case 'bold':
                        this._FontType = this.BoldFontType;
                        break;
                    default:
                        this._FontType = this.NormalFontType;
                        break;
                }
            }
            else {
                var nFontWeight = Number(this.FontWeight);
                if (nFontWeight < 300) {
                    this._FontType = this.LigthFontType;
                }
                else if (nFontWeight >= 300 && nFontWeight <= 500) {
                    this._FontType = this.NormalFontType;
                }
                else if (nFontWeight > 500) {
                    this._FontType = this.BoldFontType;
                }
            }
        }
        return this._FontType;
    }
    this.GetTextWidth = function(strText) {
        var delta = this.FontDelta();
        return strText.length * delta;
    }
    this.CropText = function(strText, avaibleWidth, lines, cropEnd) {
        var delta = this.FontDelta();
        if (cropEnd == null) {
            cropEnd = '...';
        }
        if (lines == null) {
            lines = 1;
        }
        avaibleWidthLast = avaibleWidth - (this.GetTextWidth(cropEnd)); //Lunghezza imposta - lunghezza stringa di crop
        var result = '';
        var currentLetter = 0;
        var letterCount = 0;
        var currAvaibleWidth;
        var slicer;
        for (var i = 1; i <= lines; i++) {
            if (i == lines) {
                currAvaibleWidth = avaibleWidthLast;
            }
            else {
                currAvaibleWidth = avaibleWidth;
            }
            letterCount = Math.ceil(currAvaibleWidth / delta);
            slicer = strText.slice(currentLetter, currentLetter + letterCount);
            if (i != lines && slicer.length == letterCount) {
                if (slicer.substr(letterCount - 1, 1) != ' ') {
                    letterCount = slicer.lastIndexOf(' ');
                    slicer = slicer.substr(0, letterCount);
                    letterCount++;
                }
            }
            result += slicer;
            currentLetter += letterCount;
            if (i == lines && currentLetter < strText.length) 
            {
                result += cropEnd;
            }
            else if (currentLetter < strText.length)
            {
                result += '<br />';
            }
        }
        return result;
    }
}

function CropTextDiv(containerId, textId, lines) {
    var containerObject = document.getElementById(containerId);
    var textObject = document.getElementById(textId);
    var strText = new String(Trim(textObject.innerHTML));
    strText = strText.replace(/<BR>/gi, " ");
    strText = strText.replace(/<BR\/>/gi, " ");
    var avaibleWidth = GetElementWidth(containerObject);
    var fontMgr = new FontManager();
    var font = fontMgr.GetFontFromElement(textObject);
    if (font.GetTextWidth(strText) > avaibleWidth) {
        textObject.innerHTML = font.CropText(strText, avaibleWidth, lines)
    }
}
