(function ($) {
    if (!window.MVD) {
        window.MVD = {};
    }


    var removeWordRegExp = /(.+)*[ ,.;](.+)/;

    function removeLastWord(text) {        
        var match = removeWordRegExp.exec(text);
        // console.log("text:",text, "match:",match);
        return match && match[1];
    }

    function heightOfLength(supcont, cont, text, len) {
        var t,s;
        if (len < text.length) {
            var x = text.slice(0, len-1);
            s = removeLastWord(x);
            t = s ? s + ' ...' : text;
        } else {
            t = text;
        }

        cont.html(t);
        return supcont.height();
    }

    function optimunHeight(supcont, cont, text, i, end, maxHeight) {
        if (i == end) {
            return i;
        }
        if ((i+1) == end) {
            return (heightOfLength(supcont, cont, text, end) < maxHeight) ? end : i;
        }
        var m = Math.floor((i + end) / 2);
        if (heightOfLength(supcont, cont, text, m) < maxHeight) {
            return optimunHeight(supcont, cont, text, m, end, maxHeight);
        } else {
            return optimunHeight(supcont, cont, text, i, m, maxHeight);
        }
    }

    MVD.fixHeight = function (supcont, maxHeight) {
        return;
        var cont = $('.text', supcont);
        var text = cont.html();
        cont.css('visibility', 'hidden');
        setTimeout(function () {
            var fullH = cont.height();
            if (fullH > maxHeight) {                
                heightOfLength(supcont, cont, text, optimunHeight(supcont, cont, text, 0, text.length, maxHeight));
                var contractedH = cont.height();
                var btn = $('.btnExpand', supcont);
                if (btn) {
                    btn.show().click(function () {
                        btn.hide(); // .blur("click");
                        cont.height(contractedH).html(text).animate({height: fullH}, (fullH - contractedH) , "swing");
                        return false;
                    });
                }
            }
            cont.css('visibility', 'visible');
        }, 10);
    };

}) (jQuery);