/*  ResizeableTextarea for the Prototype JavaScript framework, version 0.2
 *  (c) 2006 Bermi Ferrer <info -a-t bermi org>
 *
 *  ResizeableTextarea is freely distributable under the terms of an MIT-style license.
 *
 *  Requirements: Prototype JS framework http://prototypejs.org/
 *  Ussage: Add this attribute to the textarea you want to resize 
 *
 *    onfocus="new ResizeableTextarea(this);"
 *
/*--------------------------------------------------------------------------*/

var textTools = {
  insertAtCursor: function (myField, myValue) {
    //IE support
    if (document.selection) {
      myField.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos)
      + myValue
      + myField.value.substring(endPos, myField.value.length);
    } else {
      myField.value += myValue;
    }
  }
}

ResizeableTextarea = Class.create();
ResizeableTextarea.prototype = {
    charCount: 0,   // Represents the number of characters on the page when we last grew the page
    initialize: function(element, options) {
        this.element = $(element);
        this.size = parseFloat(this.element.getStyle('height') || '100');
        this.options = Object.extend({
            inScreen: false,
            resizeStep: 10,
            minHeight: this.size,
            fixBottomPosition: true             // If true, we scroll the window so that the bottom of the window stays fixed
        }, options || {});
        Event.observe(this.element, "keyup", this.resize.bindAsEventListener(this));
        if ( !this.options.inScreen ) {
          // Had to comment this out because it makes the FF caret disappear
            // this.element.style.overflow = 'hidden';
        }
        this.element.setAttribute("wrap","virtual");
        this.resize();
    },
    resize : function(){
        this.shrink();
        this.grow();
    },
    shrink : function(){
        // console.log("Shrinking")
        if ( this.size <= this.options.minHeight || this.element.value.length >= this.charCount ){
            return;
        }
        // console.log("  shrink scrollHeight="+this.element.scrollHeight+",clientHeight="+this.element.clientHeight);
        // console.log("  shrink offsetTop="+this.element.offsetTop+", body.clientHeight="+document.body.clientHeight);
        if ( this.element.scrollHeight <= this.element.clientHeight) {
            this.size -= 2*this.options.resizeStep;
            this.element.style.height = this.size+'px';
            // console.log("  Set height to "+this.size);
            if ( this.options.fixBottomPosition) window.scrollBy(0,-this.options.resizeStep);
            this.shrink();
        }
    },
    grow : function(){
      // console.log("growing")
        if ( this.element.scrollHeight > this.element.clientHeight ) {
          // console.log("  grow scrollHeight="+this.element.scrollHeight+",clientHeight="+this.element.clientHeight);
          // console.log("  grow offsetTop="+this.element.offsetTop+", body.clientHeight="+document.body.clientHeight);
            if ( this.options.inScreen && (20 + this.element.offsetTop + this.element.clientHeight) > document.body.clientHeight ) {
                return;
            }
            var step = (this.element.scrollHeight - this.element.clientHeight) + this.options.resizeStep;
            this.size += step
            this.element.style.height = this.size+'px';
            // console.log("  Set height to "+this.size);
            this.charCount = this.element.value.length;
            if ( this.options.fixBottomPosition) window.scrollBy(0,step);
            this.grow();
        }
    }
}  
