(function(jQuery) {
  if (!window.getComputedStyle) {
    window.getComputedStyle = function(el, pseudo) {
      this.el = el;
      this.getPropertyValue = function(prop) {
        var re = /(\-([a-z]){1})/g;
        if (prop == 'float') prop = 'styleFloat';
        if (re.test(prop)) {
          prop = prop.replace(re, function () {
            return arguments[2].toUpperCase();
          });
        }
        return el.currentStyle[prop] ? el.currentStyle[prop] : null;
      }
      return this;
    }
  }

  function SimpleNodeOverflow(options, currentObject) {
    function CreateTestNode(currentObject) {
      var appliedStyle = {
        'font-size': $(currentObject).css('font-size'),
        'line-height': options.lineHeight + "px",
        'font-weight': $(currentObject).css('font-weight'),
        'visibility': 'hidden',
        'position': 'absolute',
        'display': 'block',
        'width': options.width +'px'
      }
      return $(document.createElement('span')).css(appliedStyle);
    }
    function fillTestNode(testNode, testText) {
      $('body').append(testNode);
      var maxHeight = options.lineHeight*options.line;
      var matchedWord = testText.split(/\b/);
      var cumulativeText = "";
      var currentWord = "";
      for (var i = 0; i < matchedWord.length; i++) {
        currentWord = matchedWord[i];
        if (i < matchedWord.length-1) {
          while (matchedWord[i+1].match(XRegExp("^\\p{L}+$"))) {
            i++;
            currentWord = currentWord + matchedWord[i];
            if (i == matchedWord.length) {
              break;
            }
          }
        }
        else {
          currentWord = matchedWord[i];
        }
        testNode.html(cumulativeText + currentWord + ' ...');
        if (maxHeight >= testNode[0].offsetHeight) {
          cumulativeText += currentWord;
        }
        else {
          break;
        }
      }
      return (i == matchedWord.length)?cumulativeText:cumulativeText + ' ...';
    }

    if (0 == $('.illustration',$(currentObject).parent()).length) {
      options.width = options.fullWidth;
    }
    else {
      options.width  = options.innerWidth;
    }
    $(currentObject).css({'width': options.width + 'px'});
    currentObject.html(fillTestNode(CreateTestNode(currentObject),currentObject.html()));
  }
  $.fn.SimpleNodeOverflow = function(options){
    options = $.extend($.fn.SimpleNodeOverflow.defaults, options);
    return this.each(function(i) {
      var CurrentCarousel = new SimpleNodeOverflow(options, $(this));
    });
  };
  $.fn.SimpleNodeOverflow.defaults = {
    'line': 2,
    'lineHeight': 14,
    'width': 171,
    'fullWidth': 231,
    'innerWidth': 171
  };
})(jQuery);
