/**
 * Growing Textareas
 * jQuery Plugin
 * 
 * (c) EZdesign.de
 *
 * Author:   Timo Besenreuther
 * Created:  2009-04-20
 * Modified: 2009-04-20
 */


$.fn.growingTextarea = function() {
	$(this).each(function() {
		var el = $(this);
		var rows = parseInt(el.attr('rows'));
		var minHeight;
		if (rows > 0) {
			var lineHeight = parseInt(el.css('line-height'));
			if (!(lineHeight > 0)) {
				lineHeight = 16;
			}
			minHeight =  lineHeight * rows;
		}
		$(this).keyup(function() {
			// create dummy container
			if ($.fn.growingTextarea.dummy == null) {
				$.fn.growingTextarea.dummy = $('<div></div>');
				$.fn.growingTextarea.dummy.css({
					'font-size'  : el.css('font-size'),
					'font-family': el.css('font-family'),
					'width'      : el.innerWidth(),
					'padding-top': el.css('padding-top'),
					'padding-right': el.css('padding-right'),
					'padding-bottom': el.css('padding-bottom'),
					'padding-left': el.css('padding-left'),
					'line-height': el.css('line-height'),
					'overflow-x' : 'hidden',
					'position'   : 'absolute',
					'top'        : '0',
					'left'		 : '-9999px'
				}).appendTo('body');
			}
			// check height of current textarea content
			var html = el.val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />');
			$.fn.growingTextarea.dummy.html(html);
			var desiredElHeight = $.fn.growingTextarea.dummy.height() + 20;
			if (desiredElHeight < minHeight) {
				desiredElHeight = minHeight;
			}
			if (el.height() != desiredElHeight) {
				el.css({height: desiredElHeight+'px'});
			}
		}).keyup();
	});
};

$.fn.growingTextarea.dummy = null;
