/*
    +--------------------------------------------------------------------------------------------+
    |                                                                                            |
    |                 HAVING PROBLEMS? NEED HELP? DOESN'T WORK? WANT TO SAY HELLO?               |
    |                                                                                            |
    |                              WRITE ME, I'M GLAD TO HELP                                    |
    |                                                                                            |
    |                                SVEN@FRANCODACOSTA.COM                                      |
    |                                                                                            |
    +--------------------------------------------------------------------------------------------+
    |   DISCLAIMER - LEGAL NOTICE -                                                              |
    +--------------------------------------------------------------------------------------------+
    |                                                                                            |
    |  This program is free for non comercial use, see the license terms available at            |
    |  http://www.francodacosta.com/licencing/ for more information                              |
    |                                                                                            |    
    |  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
    |  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
    |                                                                                            |
    |  USE IT AT YOUR OWN RISK                                                                   |
    |                                                                                            |
    |                                                                                            |
    +--------------------------------------------------------------------------------------------+
 */

Ext.namespace('Ext.fc');
/**
 * A nice way to present dates and time
 * 
 * @class   Ext.fc.autoHeightTextArea
 * @version    0.1
 * @author     Nuno Costa - sven@francodacosta.com
 * @copyright  Copyright (c) 2009
 * @license    http://www.francodacosta.com/licencing/
 * @link       http://francodacosta.com/extjs/auto-height-textarea
 * @since      2009-04-14
 */
Ext.fc.autoHeightTextArea = function(){
    
    var defaultConfig = function(){
        return {
            minHeight : 0
            ,maxHeight : 0
            ,growBy: 20
        }
    }
    
    var processOptions = function(config){
        var o = defaultConfig();
        var options = {};
        Ext.apply(options, config, o);
        
        return options ;
    }
    
    return {
        div : null
        ,applyTo: function(elementId, options){
        
            var el = Ext.get(elementId);
            var width = el.getWidth();
            var height = el.getHeight();
            
            var styles = el.getStyles('padding-top', 'padding-bottom', 'padding-left', 'padding-right', 'line-height', 'font-size', 'font-family', 'font-weight');
            styles.width = width +'px' ;

            if(! this.div){
                var options = processOptions(options);
                
                this.div = Ext.DomHelper.append(Ext.getBody() || document.body, {
                    'id':elementId + '-preview-div'
                    ,'tag' : 'div'
                    ,'background': 'red'
                    ,'style' : 'position: absolute; top: -100000px; left: -100000px;'
                }, true)
                Ext.DomHelper.applyStyles(this.div, styles);
                
                el.setStyle('overflow', 'hidden');
                el.on('keyup', function() {
                        this.applyTo(elementId, options);
                }, this);
                el.on('focus', function() {
                        this.applyTo(elementId, options);
                }, this);
                el.on('mouseup', function() {
                        this.applyTo(elementId, options);
                }, this);
                el.on('blur', function() {
                        this.applyTo(elementId, options);
                }, this);
            }
            
            //replace \n with <br>&nbsp; so that the enter key can trigger and height increase
            //but first remove all previous entries, so that the height mesurement can be as accurate as possible
            this.div.update( 
                    el.dom.value.replace(/<br \/>&nbsp;/, '<br />')
                                .replace(/<|>/g, ' ')
                                .replace(/&/g,"&amp;")
                                .replace(/\n/g, '<br />&nbsp;') 
                    );
            
            var textHeight = this.div.getHeight();
            
            if ( (textHeight > options.maxHeight ) && (options.maxHeight > 0) ){
                textHeight = options.maxHeight ;
                el.setStyle('overflow', 'auto');
            }
            if ( (textHeight < options.minHeight ) && (options.minHeight > 0) ) {
                textHeight = options.minHeight ;
                el.setStyle('overflow', 'auto');
            }
            
            el.setHeight(textHeight + options.growBy , true);
        }
    }
}