; (function($) {
    /**
    * Autocut text
    * @author Renaud LITTOLFF rlittolff@gmail.com
    * @version 1.1
    * @param {Object} options {maxWidth, maxHeight, end}
    * @return All outer elements processed
    */
    $.fn.autoCutText = function(options) {
        var o = $.extend(
            {
                maxWidth:  null,
                maxHeight: null,
                end:        '...'
            },
            options
        );

        return this.each(
            function() {
                $this = $(this);
                var text = $.trim($this.text());

                var maxHeight = null;
                var maxWidth  = o.maxWidth;

                if(typeof o.maxHeight == 'string'
                && o.maxHeight.substr(o.maxHeight.length - 1, 1) == 'l') {
                    // détection de la hauteur d'une ligne
                    $this.text('.');
                    maxHeight = Math.floor(parseFloat(o.maxHeight) * $this.height());

                    // détection de la largeur d'une ligne
                    $this.text('');
                    if(o.maxWidth === null) {
                        $current = $this;
                        
                        while($current.length > 0 && $current.width() == 0) {
                            $current = $current.parent();
                        }

                        if($current.length > 0) {
                            maxWidth = $current.width();
                        }
                    }

                    // réinitialisation du texte
                    $this.text(text);
                }
                else if(o.maxHeight !== null) {
                    maxHeight = Math.floor(parseFloat(o.maxHeight));
                }

                while(
                    (
                            (maxHeight !== null && $this.height() > maxHeight)
                        ||  (maxWidth  !== null && $this.width()  > maxWidth)
                    )
                    && text.length > 0
                ) {
                    text = $.trim(text.substr(0, text.length - 1));
                    $this.text(text + o.end);
                }
            }
        );
    };
})(jQuery);