(function($){
    
  $.fn.description = function(settings) {
    
    var settings = $.extend({
      foldText: 'свернуть',
      unfoldText: 'развернуть',
      lines: 2
    }, settings || {});
  
    return this.each(function() {
      var description = jQuery(this);
      var el = description;
      while ( isNaN(parseInt(el.css('lineHeight'))) )
        el = el.parent();
      var foldedHeight = parseInt(el.css('lineHeight')) * settings.lines;
      el = description;
      var width = 0;
      while ( !(el.css('width')) || el.css('width') == 'auto' )
      {
        width -= parseInt(el.css('paddingLeft'));
        width -= parseInt(el.css('paddingRight'));
        if ( !isNaN(parseInt(el.css('borderRightWidth'))) )
          width -= parseInt(el.css('borderRightWidth'));
        if ( !isNaN(parseInt(el.css('borderLeftWidth'))) )
          width -= parseInt(el.css('borderLeftWidth'));
        el = el.parent();
      }
      width += parseInt(el.css('width'));
      //alert(width);
      
      var tmp = description.clone().css({overflow: 'hidden', 'width': width, lineHeight: el.css('lineHeight')});
      jQuery(document.body).prepend(tmp);
      var fullHeight = tmp.height();
      if ( foldedHeight < fullHeight )
      {
        var button = jQuery('<span> <span>&hellip;</span> <a href="#">' + settings.unfoldText + '</a></span>').children('a');
        var fullButton = button.parent().contents();
        tmp.append(button.parent());
        fullHeight = tmp.height();
        var textHandler = tmp.children('.value');
        var fullText = textHandler.html();
        var text = fullText.split(/[\s]+/m);
        var smallText = [];
        do
        {
          smallText.push(text.shift());
          textHandler.html(smallText.join(' '));
        }
        while ( (tmp.height() <= foldedHeight) && text.length);
        smallText.pop();
        delete text;
        var smallText = smallText.join(' ');
        textHandler.html(smallText);
        tmp.height(foldedHeight);
        var showHandler = function () {
          jQuery(this).unbind('click').html('').parent().children('span').hide();
          textHandler.html(fullText);
          tmp.animate({height: fullHeight},'fast').queue(function(){jQuery(this).stop();});
          jQuery(this).html(settings.foldText).click(hideHandler);
          return false;
        }
        var hideHandler = function () {
          jQuery(this).unbind('click').html('').parent().children('span').show();
          tmp.animate({height: foldedHeight},'fast').queue(function(){textHandler.html(smallText);jQuery(this).stop();});
          jQuery(this).html(settings.unfoldText).click(showHandler);         
          return false;
        }
        button.click(showHandler);
        description.replaceWith(tmp);
      }
      else
      {
        tmp.remove();
      }
    });
    
  }

})(jQuery);