/* Autogrow plugin
------------------------------------------------------------------------------------------------ */

(function($) {
    $.fn.autogrow = function(options) {

        if (!this.length) return;

        var settings = $.extend({
            'minheight' : 155,      // Min height of the textarea
            'debug'     : false     // Debug mode
        }, options);

        return this.each(function() {

            var textarea    = this,
                lineheight  = parseInt($(textarea).css('line-height'), 10) || parseInt($(textarea).css('font-size'), 10);

            // Prepair textarea

             $(textarea).css('overflow', 'hidden');

            // Create twin div with textarea style

            $(textarea).parent().append(
                twin = $('<div />').css({
                    'display'           : 'none',
                    'font-size'         : $(textarea).css('font-size'),
                    'font-family'       : $(textarea).css('font-family'),
                    'font-weight'       : $(textarea).css('font-weight'),
                    'line-height'       : $(textarea).css('line-height'),
                    'padding-top'       : $(textarea).css('padding-top'),
                    'padding-right'     : $(textarea).css('padding-right'),
                    'padding-bottom'    : $(textarea).css('padding-bottom'),
                    'padding-left'      : $(textarea).css('padding-left'),
                    'position'          : 'absolute',
                    'width'             : $(textarea).css('width'),
                    'word-wrap'         : 'break-word'
                })
            );

            // Update textarea on keyup

            $(textarea).keyup(function(){
                $.fn.autogrow.update(textarea, lineheight, settings);
            });

            // Update textarea on browser (re)load

            $.fn.autogrow.update(textarea, lineheight, settings);

        });
    }

    // Function: update textarea

    $.fn.autogrow.update = function(textarea, lineheight, settings) {

        var textarea_content    = $(textarea).val().replace('&','&amp;').replace(/  /g, '&nbsp;').replace('>', '&gt;').replace('<', '&lt;').replace(/\n/g, '<br />'),
            twin_content        = $(twin).html();

        if (textarea_content + '&nbsp;' != twin_content) {
            $(twin).html(textarea_content + '&nbsp;');

            if (Math.abs($(twin).height() + lineheight - $(textarea).height()) > 3) {

                var goal_height = $(twin).height() + lineheight;

                if (goal_height <= settings.minheight) {
                    $.fn.autogrow.setHeight(textarea, settings.minheight)
                }
                else {
                    $.fn.autogrow.setHeight(textarea, goal_height)
                }
            }
        }

    }

    // Function: set textarea height

    $.fn.autogrow.setHeight = function(textarea, height) {

        if ($(textarea).height() != height) {

            var old_textarea_height = $(textarea).height();

            $(textarea).css({
                'height'    : height,
                'overflow'  : 'hidden'
            });
        }
    }

})(jQuery);

