(function(jQuery) {
	var self = null;
	jQuery.fn.autogrow = function(o) { return this.each(function() { new jQuery.autogrow(this, o); }); };
	jQuery.autogrow = function (e, o) {
		this.options = o || {};
		this.dummy = null;
		this.interval = null;
		this.line_height = this.options.lineHeight || parseFloat(jQuery(e).css('line-height')) || 16;
		this.min_height = this.options.minHeight || parseInt(jQuery(e).css('min-height')) || parseInt(jQuery(e).innerHeight());
		this.max_height = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));
		this.textarea = jQuery(e);
		this.init();
	};
	
	jQuery.autogrow.fn = jQuery.autogrow.prototype = { autogrow: '1.2.2' };
 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
	jQuery.autogrow.fn.extend({
		init: function() {			
			var self = this;			
			this.textarea.css({display: 'block', 'overflow': 'hidden', 'height': this.textarea.height()});
			this.textarea.keyup(function() { self.checkExpand(); });
			this.checkExpand();	
		},
		checkExpand: function() {
			if (this.dummy == null) {
				this.dummy = jQuery('<div></div>');
				this.dummy.css({
					'font-size'  : this.textarea.css('font-size'),
					'font-family': this.textarea.css('font-family'),
					'width'      : (parseInt(this.textarea.width())-6) + 'px',
					'padding'    : '2px',
					'line-height': this.line_height + 'px',
					'position'   : 'absolute',
					'top'        : 0,
					'left'		 : -9999
					}).appendTo(this.textarea.get(0).parentNode);
			}
			var html = this.textarea.val() + '.';
			if ($.browser.msie) html = html.replace(/\n/g, '<BR>');
			else html = html.replace(/\n/g, '<br />');
			if (this.dummy.html() != html) {
				this.dummy.html(html);
				if (this.max_height && this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)) { this.textarea.css('overflow-y', 'auto'); }
				else {
					this.textarea.css('overflow-y', 'hidden');
					var dummy = parseInt(this.dummy.height() + this.line_height);
					if(dummy >= this.min_height && Math.abs(this.textarea.height() - dummy) > this.line_height) {
						if(dummy > this.textarea.height()) this.textarea.animate({height: dummy + 'px'}, 150);
						if(dummy < this.textarea.height()) this.textarea.animate({height: dummy + 'px'}, 150);
					} else { if(Math.abs(this.textarea.height() - this.min_height) > this.line_height && Math.abs(this.textarea.height() - dummy) > this.line_height) this.textarea.animate({height: this.min_height + 'px'}, 200); }
				}
			}
		}
	 });
})(jQuery);