/*
Textarea Autoresizer 0.2b
Updated to a specific need

Textarea Autoresizer 0.2a
Automatically resizes textareas on loading and editing.
Update by Walter Rafelsberger - http://www.rafelsberger.at

Derived from:
Textarea Resizer 0.2
Allows the user to expand and shrink the height of HTML textareas
by Chris Barr of Eject Media - http://ejectmedia.net
Updated and extended to 1.2 by Lennart Pilon - http://ljpilon.nl

Based on Mootools 1.2 - http://mootools.net
*/

var textareaSizer = {};

textareaSizer = new Class({

	Implements: Options,	
	options: {
		tbResizeDuration:400,				//how long each resize takes
		tbResizeTransition: Fx.Transitions.Expo.easeOut, //the transition :P
		tbMinSize: 20, 				//always leave a min size or it looks strange
		autoSize: true				// if you want to expand a textarea which already has content
	},
	initialize: function(options){
		
		options = $merge(this.options, options);
		this.setOptions(options);
		this.initTextareas(options);
	},
	initTextareas: function(options) {
		options = $merge(this.options, options);
		//select each textarea without the class to skip
		$$('textarea.grow').each(function(textbox,i){				
			// disable srollbars
			textbox.setStyle('overflow', 'hidden');
			
			// code to autoresize textarea on edit
			// via http://fragged.org/dev/textarea.php
			textbox.addEvents({
				focus: function() {
					this.focused = true;
				},
				blur: function() {
					this.focused = false;
				}
			});
			
			// attach a key listener to autoresize when editing
			window.addEvent("keyup", function(e) {
				if (textbox.focused) {
					if (options.autoSize) {
						if ( textbox.getScrollSize()['y'] > options.tbMinSize) {
							new Fx.Morph(textbox, {duration: 0 ,transition: options.tbResizeTransition}).start({'height': textbox.getScrollSize()['y']});
						}
					}
				}
			});

			// initial resize
			if (options.autoSize) {
				if ( textbox.getScrollSize()['y'] > options.tbMinSize) {
					new Fx.Morph(textbox, {duration: options.tbResizeDuration ,transition: options.tbResizeTransition}).start({'height': textbox.getScrollSize()['y'] + options.tbMinSize});
				}
			}
		});
	}
});
