/* jQuery truncateText Plugin
   Intelligently truncates the content of the passed element.
   By Livingston Samuel - http://livingstonsamuel.com */

;(function($) {
  $.fn.truncateText = function(options) {
    var defaults = {
      length: 10,
      ellipsisText: "...", // can be string ot html element, change contentType accordingly
      contentType: 'text' // text or html
    };
    var options = $.extend(defaults, options);
    return this.each(function() {
      var me = $(this);
      var textContent = ( options.contentType == "text" )? me.text() : me.html();
      if (textContent.length > options.length) {
        var splitPosition = textContent.indexOf(' ', options.length);
        if (splitPosition != "-1") {
          var truncatedText = textContent.substring(0, splitPosition);
          if (options.contentType == "text") {
            truncatedText += options.ellipsisText;
            me.text(truncatedText);
            me.attr({title : textContent});
          } else if (options.contentType == "html") {
            me.html(truncatedText);
            me.append(options.ellipsisText);
          }
        }
      }
    });
  }
})(jQuery);