/* 
 * Auto Expanding Text Area (1.2.1)
 * by Chrys Bader (www.chrysbader.com)
 * chrysb@gmail.com
 *
 * Special thanks to:
 * Jake Chapa - jake@hybridstudio.com
 * John Resig - jeresig@gmail.com
 *
 * Copyright (c) 2008 Chrys Bader (www.chrysbader.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses. 
 *
 *
 * NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
 *
 */
 
(function(jQuery) {
		  
	var self = null;
 
	jQuery.fn.autogrow = function(o)
	{	
		return this.each(function() {
			new jQuery.autogrow(this, o);
		});
	};
	

    /**
     * The autogrow object.
     *
     * @constructor
     * @name jQuery.autogrow
     * @param Object e The textarea to create the autogrow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/autogrow
        accept parames {minHeight:40,maxHeight:80,line_height:18}
     */
	
	jQuery.autogrow = function (e, options)
	{
	    options = options || {};
		this.dummy			  = null;
		this.interval	 	  = null;
		this.line_height	  = options.lineHeight || '11';
		jQuery(e).css("line-height",this.line_height + "px");
		options.minHeight = options.minHeight || 0;
		this.minHeight		  = options.minHeight < parseInt(jQuery(e).css('height'),10)?parseInt(jQuery(e).css('height'),10):options.minHeight;
		if(options.maxHeight){
		    this.maxHeight = options.maxHeight < parseInt(jQuery(e).css('height'),10)?parseInt(jQuery(e).css('height'),10):options.maxHeight;
		}
		this.options		  = options;
		this.textarea		  = jQuery(e);
		
		if(this.line_height == NaN)
		  this.line_height = 0;
		
		// Only one textarea activated at a time, the one being used
		this.init();
	};
	
	jQuery.autogrow.fn = jQuery.autogrow.prototype = {
    autogrow: '1.2.1'
  };
	
 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
	
	jQuery.autogrow.fn.extend({
						 
		init: function() {			
			var self = this;			
			//this.textarea.css({overflow: 'hidden'});
			this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand });
			this.checkExpand();	
		},
						 
		startExpand: function() {				
		  var self = this;
			this.interval = window.setInterval(function() {self.checkExpand()}, 200);
		},
		
		stopExpand: function() {
			clearInterval(this.interval);	
		},
		
		checkExpand: function() {
			
			if (this.dummy == null)
			{
//			    var tPadding = this.textarea.css('padding');
//			    if(!tPadding){
//			        tPadding = 0;
//			    }
			    
				this.dummy = jQuery('<div></div>');
				this.dummy.css({
												'font-size'  : this.textarea.css('font-size'),
												'font-family': this.textarea.css('font-family'),
												'width'      : this.textarea.css('width'),
												//'padding'    : tPadding,
												'line-height': this.line_height + 'px',
												'overflow-x' : 'hidden',
												'position'   : 'absolute',
												'top'        : 0,
												'left'		 : -9999
												}).appendTo('body');
			}
			
			if ($.browser.msie)
			{
				var html = this.textarea.val().replace(/\n/g, '<BR>new');
			}
			else
			{
				var html = this.textarea.val().replace(/\n/g, '<br>new');
			}
			
			if (this.dummy.html() != html)
			{
				this.dummy.html(html);	
				if(this.dummy.height() + this.line_height > this.maxHeight){
				    this.textarea.css("overflow-y","auto");
				}else if(this.dummy.height() < this.minHeight){
				}
				else 
				{	
					//this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);	
					this.textarea.height((this.dummy.height() + this.line_height) + 'px');
					
				}
				
			}
		}
						 
	 });
})(jQuery);