/**
*	@name							Elastic
*	@descripton						Elastic is Jquery plugin that grow and shrink your textareas automaticliy
*	@version						1.6.1
*	@requires						Jquery 1.2.6+
*	@author							Jan Jarfalk jan.jarfalk@unwrongest.com
*/

(function(jQuery){ 
	jQuery.fn.extend({  
		elastic: function() {
			//	We will create a div clone of the textarea
			//	by copying these attributes from the textarea to the div.
			var mimics = [
				'paddingTop',
				'paddingRight',
				'paddingBottom',
				'paddingLeft',
				'fontSize',
				'lineHeight',
				'fontFamily',
				'width',
				'fontWeight'];
			
			this.doit = function (){
			
				// Elastic only works on textareas
				if ( this.type != 'textarea' ) {
					return false;
				}				
				
				
				var $textarea	=	jQuery(this),
					$twin		=	jQuery('<div />').css({'position': 'absolute','top':'-5000px', 'left':'-5000px', 'word-wrap':'break-word'}),
					lineHeight	=	parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
					minheight	=	parseInt($textarea.css('height'),10) || lineHeight*3,
					maxheight	=	parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
					goalheight	=	0,
					i 			=	0;
					this.textarea = jQuery(this);
					
				// Append the twin to the DOM
				// We are going to meassure the height of this, not the textarea.
				$twin.appendTo($textarea.parent());
				
				// Copy the essential styles (mimics) from the textarea to the twin
				var i = mimics.length;
				while(i--){
					$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
				}
				
				var self = this;			
			    this.textarea.css({overflow: 'hidden', display: 'block'});
			    this.textarea.bind('focus', function() { startExpand() } ).bind('blur', function() { stopExpand() });
				
				
				 function startExpand() {				
		        var self = this;
			    this.interval = window.setInterval(function() {update()}, 10);
		        }
        		
		         function stopExpand (){
			        clearInterval(this.interval);	
		        }
				
				// Sets a given height and overflow state on the textarea
				function setHeightAndOverflow(height, overflow){					
					curratedHeight = Math.floor(parseInt(height,10));
					if($textarea.height() != curratedHeight){
						$textarea.css({overflow: overflow});
						$textarea.css({height: curratedHeight});						
						//$textarea.animate({'height': curratedHeight + 'px','overflow':overflow});
						
					}
				}
				
				
				// This function will update the height of the textarea if necessary 
				function update() {				
						
					// Get curated content from the textarea.
					maxheight	=	parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE;
					var textareaContent = $textarea.val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />').replace(/&/g,"&amp;");
					var twinContent = $twin.html();
					
					if(textareaContent+'&nbsp;' != twinContent){
					
						// Add an extra white space so new rows are added when you are at the end of a row.
						$twin.html(textareaContent+'&nbsp;');
						
						// Change textarea height if twin plus the height of one line differs more than 3 pixel from textarea height
						if(Math.abs($twin.height() - $textarea.height()) > 3){
							
							var goalheight = $twin.height() + 5;//+lineHeight;
							if(goalheight >= maxheight) {
								setHeightAndOverflow(maxheight,'auto');
							} else if(goalheight <= minheight) {
								setHeightAndOverflow(minheight,'hidden');
							} else {
								setHeightAndOverflow(goalheight,'hidden');
							}
							
						}
						
					}
					
				}				
				
				function adjustSize(){
					/*
						if( this.timer )
							clearTimeout(this.timer);
						
						this.timer = setTimeout(update, 200);
					*/
					update();
				}
				
				// Hide scrollbars
				$textarea.css({'overflow':'hidden'});
				
				// Update textarea size on keyup
				//$textarea.keyup(adjustSize);
				//$textarea.keydown(adjustSize);
				
				// And this line is to catch the browser paste event
				//$textarea.live('input paste',function(e){ setTimeout( update, 250); });				
				
				// Run update once when elastic is initialized
				update();
			}
			
			
			return this.each( this.doit );
			
        } 
    }); 
})(jQuery);



/* 
 * Auto Expanding Text Area (1.2.2)
 * 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($) {
//		  
//	var self = null;
// 
//	jQuery.fn.autogrow = function(o)
//	{	
//		return this.each(function() {
//			new jQuery.autogrow(this, o);
//		});
//	};
//	
//	jQuery.fn.elastic = jQuery.fn.autogrow;
//	
//	

//    /**
//     * 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
//     */
//	
//	jQuery.autogrow = function (e, o)
//	{
//	    
//		var $textarea = jQuery(e);
//		this.options		  	= o || {};
//		this.dummy			  	= null;
//		this.interval	 	  	= null;
//		this.line_height	  	= this.options.lineHeight || parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),10);
//		this.min_height		  	= this.options.minHeight || $textarea.height();
//		this.max_height		  	= this.options.maxHeight || parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE;
//		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.2'
//  };
//	
// 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
//	
//	jQuery.autogrow.fn.extend({
//						 
//		init: function() {			
//			var self = this;			
//			this.textarea.css({overflow: 'hidden', display: 'block'});
//			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()}, 10);
//		},
//		
//		stopExpand: function() {
//			clearInterval(this.interval);	
//		},
//		
//		checkExpand: function() {
//		
//			debugger;
//			if (this.dummy == null)
//			{
//				this.dummy = jQuery('<div></div>');
//				this.dummy.css({
//												'font-size'  : this.textarea.css('font-size'),
//												'font-family': this.textarea.css('font-family'),
//												'font-weight': this.textarea.css('font-weight'),
//												'width'      : this.textarea.css('width'),
//												
//												'padding-top'       : this.textarea.css('padding-top'),
//												'padding-left'      : this.textarea.css('padding-left'),
//												'padding-right'     : this.textarea.css('padding-right'),
//												'padding-bottom'    : this.textarea.css('padding-bottom'),
//												
//												'line-height': this.line_height + 'px',
//												'position'   : 'absolute',
//												'top'        : -5000,
//												'left'		 : -5000,
//												'word-wrap'  : 'break-word'
//												}).insertAfter(this.textarea);
//			}
//			
//			// Strip HTML tags
////			this.line_height	  	= this.options.lineHeight || parseInt(this.textarea.css('line-height'),10) || parseInt(this.textarea.css('font-size'),10);
////		    this.min_height		  	= this.options.minHeight || this.textarea.height();
////		    this.max_height		  	= this.options.maxHeight || parseInt(this.textarea.css('max-height'),10) || Number.MAX_VALUE;
//			var html = this.textarea.val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />').replace(/&/g,"&amp;");
//			
//			// IE is different, as per usual
////			if ($.browser.msie)
////			{
////				html = html.replace(/\n/g, '<BR>new');
////			}
////			else
////			{
////				html = html.replace(/\n/g, '<br>new');
////			}
//			
//			html += "&nbsp;";
//			
//			
//				this.dummy.html(html);	
//				
//				if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height))
//				{
//					this.textarea.css('overflow-y', 'auto');	
//				}
//				else
//				{
//					this.textarea.css('overflow-y', 'hidden');
//					if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height()))
//					{	
//						var goalheight = this.dummy.height();
//						
//						if( goalheight > this.line_height )
//							this.textarea.css({height: (this.dummy.height()) + this.line_height + 'px'}, 100);	
//						else
//							this.textarea.css({height: (this.dummy.height()) + (parseInt(this.textarea.css('padding-bottom')) || 5) + 'px'}, 100);	
//					}
//				}
//			
//		}
//						 
//	 });
//})(jQuery);