/**
 * Teleuco Web Framework
 * /home/js/components/autogrow.js
 * 
 * @copyright 	Copyright (C) 2010 Teleuco s.r.l. (http://teleuco.com)
 * @license 	Commercial
 * @version	1.0.0
 * @package	twaf
 * 
 */

jQuery.fn.autogrow = function(settings) {
	return this.each(function(){
		var __e = $(this);
		var options = $.extend({
            css:{
                'overflow-y': 'hidden',
                'overflow-x': 'hidden',
                'outline': 'none',
                'border': "solid 1px #cccccc",
                'font-family': "Trebuchet, Verdana, Arial, Helvetica, sans-serif",
                'display':'block',
                'font-size': "12px",
                'padding': "2px",
                'line-height': '14px'
            }
        }, settings);
        if(__e.get(0).tagName.toLowerCase()=='textarea'){
        	__e.css(options.css);
            var $meter = $(document.createElement('div')).css({
                'background': 'yellow',
                'font-family': __e.css('font-family'),
                'font-size': __e.css('font-size'),
                'overflow-y': 'visible',
                'overflow-x': 'visible',
                'height': 'auto',
                'display': 'block',
                'padding': __e.css('padding'),
                'line-height': __e.css('line-height')
            }).hide().appendTo($('body'));
            _resize();

            __e.keyup(_resize).focus(_resize).ready(_resize);
        }

        function _resize(){
            $meter.html(String(__e.val()).replace(/\n/g, '<br>&nbsp;')).width(__e.width());       
            if(Number(__e.outerHeight()) != Number($meter.height()+28))
                {__e.animate({height: Number($meter.height()+28)+"px"}, 'fast');}
        }
	});
};
