Event.onReady(function() {

//  $$('textarea').each(function (inputElement) { adjustTextarea(inputElement) });
	$$('textarea').each(function (inputElement) { new TextAreaResize( inputElement) });
  

  });
  
  function adjustTextarea(inputElement) {
    var textarea = inputElement;
    var initialHeight = textarea.getHeight();
    var currentHeight = -1;
    var currentTimer = false;


	div = $(document.createElement('div'));
	div.id = textarea.id + '_hidden';

    div.setStyle({'display'       : 'none',
                  'width'         : textarea.getWidth() ?
                                      (textarea.getWidth() + "px") :
                                      textarea.getStyle('width'),
                 // 'whiteSpace'    : 'pre-wrap',
                  'fontFamily'    : textarea.getStyle('fontFamily'),
                  'fontSize'      : textarea.getStyle('fontSize'),
                  'lineHeight'    : textarea.getStyle('lineHeight'),
                  'paddingTop'    : textarea.getStyle('paddingTop'),
                  'paddingLeft'   : textarea.getStyle('paddingLeft'),
                  'paddingRight'  : textarea.getStyle('paddingRight'),
                  'paddingBottom' : textarea.getStyle('paddingBottom'),
                  'marginTop'     : textarea.getStyle('marginTop'),
                  'marginLeft'    : textarea.getStyle('marginLeft'),
                  'marginRight'   : textarea.getStyle('marginRight'),
                  'marginBottom'  : textarea.getStyle('marginBottom'),
                  'borderTop'     : textarea.getStyle('borderTop'),
                  'borderLeft'    : textarea.getStyle('borderLeft'),
                  'borderRight'   : textarea.getStyle('borderRight'),
                  'borderBottom'  : textarea.getStyle('borderBottom')
                 });

	document.body.appendChild(div);
    var timerHandler = function() {
      currentTimer = false;
      if(initialHeight == 0) {
        initialHeight = textarea.getHeight();
      }
      if($F(textarea) == "") {
      	initialHeight = 100;
      }
      div.innerHTML = $F(textarea).replace(/&/g, '&amp;')
                                  .replace(/</g, '&lt;')
                                  .replace(/\n/g, '<br />') +
                      '<br />z';
      var newHeight = Math.max(initialHeight, div.getHeight()) + 10;

      if(newHeight != currentHeight && newHeight != 0) {
        textarea.setStyle({ 'height': newHeight + 'px' });
        currentHeight = newHeight;
      }
    }
    var eventHandler = function(ev) {
      if(!currentTimer) {
        setTimeout(timerHandler, 250);
      }
    }
    textarea.observe('change', eventHandler);
    textarea.observe('keyup', eventHandler);
    timerHandler();
  };



var TextAreaResize = Class.create();
TextAreaResize.prototype = {
  initialize: function( element, options ) {
    //element = $( element );
    this.element = element;

    this.options = Object.extend(
      {},
      options || {} );

    Event.observe( this.element, 'keyup',
      this.onKeyUp.bindAsEventListener( this ) );
    this.onKeyUp();
  },

  onKeyUp: function() {
    // We need this variable because "this" changes in the scope of the
    // function below.
    var cols = this.element.cols;

    var linecount = 0;
    $A( this.element.value.split( "\n" ) ).each( function( l ) {
      // We take long lines into account via the cols divide.
      linecount += 1 + Math.floor( l.length / cols );
    } )
	if(linecount < 5){ linecount = 4;}

    this.element.rows = linecount + 1;
  }
}
