(function($) {

    /*
     * Auto-growing textareas; technique ripped from Facebook
     */
    $.fn.autogrow = function(options) {
        this.filter('textarea').each(function() {          
            //temp_minHeight = temp_minHeight.replace('px','');
            var $this = $(this),           
            minHeight = 15,
            lineHeight  = $this.css('lineHeight');
            if(options.minHeight)
            {
                minHeight = parseInt((options.minHeight).replace('px',''));
            }
            else if($this.height())
            {
                minHeight = $this.height();
            }

            var shadow = $('<div></div>').css({
                position:   'absolute',
                top:        -10000,
                left:       -10000,
                width:      (typeof(options)=="undefined")?0:options.width,
                //width:    $(this).width(), - this does not seem to work because due to creating the textarea through jquery, its css appears to be not be initialised when this function is called.
                fontSize:   (typeof(options)=="undefined")?$this.css('fontSize'):options.fontSize,
                //fontFamily: $this.css('fontFamily'), - returns wrong font-family in IE
				fontFamily: "Arial,Verdana,Geneva,sans-serif",
                lineHeight: (typeof(options)=="undefined")?$this.css('lineHeight'):options.lineHeight,
                resize:     'none'
            }).appendTo(document.body);
            
            var update = function() {
                

                var val = this.value.replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/&/g, '&amp;')
                .replace(/\n/g, '<br/>K');

                shadow.html(val);
                $(this).css('height', Math.max(shadow.height(), minHeight));
            }
            
            $(this).change(update).keyup(update).keydown(update);
            
            update.apply(this);
            
        });
        
        return this;
        
    }
    
})(jQuery);
