function adjustHeight(event) {
	Event.stop(event);
	var textarea = Event.element(event);
	var dif = textarea.scrollHeight - textarea.clientHeight; 
	if (dif) {  
		if (isNaN(parseInt(textarea.style.height))) { 
			textarea.style.height = textarea.scrollHeight + "px" 
		} else { 
			textarea.style.height = parseInt(textarea.style.height) + dif + "px" 
		} 
	} 
}

ResizableTextArea = Class.create({
	min_height: 4,
	margin: 1.1,
	unit: "em",
	
	initialize: function(field) {
		this.element = $(field);
		this.element.setStyle({
			height: this.min_height + this.unit,
			overflow: "hidden"
		});
		this._resize();
		this.element.observe('keyup', this._resize.bind(this));
		this.element.observe('mouseup', this._resize.bind(this));
	},

	_resize: function() {
		this.rows = this._calcRows();
		this.height = (this.rows*this.margin) + this.margin;
		if (this.height <this.min_height) { this.height = this.min_height; }
		//this.element.morph({height: (this.height + this.unit)}, {duration: .2});
		this.element.setStyle({height: (this.height + this.unit)});
	},

	_calcRows: function() {
		this.cols = this.element.getWidth() / 8;
		this.rows = Math.floor(this.element.value.length / this.cols) + this.element.value.split("\n").length;
		return this.rows
	}
});

document.observe('dom:loaded', function() {
	$$('.form textarea').each(function(el) {
		new ResizableTextArea(el);
		/*['change', 'keyup', 'mouseup'].each(function(action) {
			Event.observe(el, action, adjustHeight);
		});*/     
	});
});
