/* ------------------------------------------------------------------------
	Pretty Comments
	
	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: The facebook textarea :)
	Version: 1.1
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top, w00t
------------------------------------------------------------------------- */

	var prettyComments = {
		version : '1.1',
		options : {
			'animate' : true,
			'animationSpeed' : 'normal', /* fast/slow/normal */
			'alreadyAnimed' : false,
			'padding' : 30,
			'init' : true /* DONT CHANGE */
		},
		init : function(which){
			// IE doubles the padding value 
			if($.browser.msie && parseFloat($.browser.version) < 7) this.options['padding'] = this.options['padding']/2;
			
			prettyComments.theTextarea = $('#' + which);
			
			// Create the div in which the content will be copied
			$('body').append('<div id="hidden_'+ this.theTextarea.attr('id') +'"></div>');
			
			prettyComments.theHiddenDiv = $('#hidden_' + this.theTextarea.attr('id'));
			
			// Now set the hiden div dimensions the same as the textarea (min-height is set differently in IE/Firefox, YAY for shitty browsers);
			this.theHiddenDiv.css({
				'display' : 'none',
				'width' : this.theTextarea.width() + 'px',
				'font-family' : this.theTextarea.css('font-family'),
				'font-size' : this.theTextarea.css('font-size'),
				'line-height' : this.theTextarea.css('line-height')
			});
			
			this.theTextarea.css({'overflow':'hidden','display':'inline'});

			if($.browser.msie && parseFloat($.browser.version) < 7){
				this.theHiddenDiv.css('height',this.theTextarea.height() + 'px');
			}else{
				this.theHiddenDiv.css('min-height',this.theTextarea.height() + 'px');
			};
			
			this.options['initialHeight'] = this.theTextarea.height();
			
			this.theTextarea.bind('keyup',function(){
				prettyComments.copyContent();
			});
			
			// On the init, make sure the textarea has the right dimensions
			this.copyContent();
		},
		copyContent : function(){
			// Convert the line feeds into BRs
			theValue = "";
			theValue += this.theTextarea.attr('value');
			theValue = theValue.replace(new RegExp( "\\n", "g" ),'<br />');
			
			this.theHiddenDiv.html(theValue);
			
			if(this.theHiddenDiv.height() > this.theTextarea.height() - this.options['padding']){
				this.expand();
			}else if(this.theHiddenDiv.height() < this.theTextarea.height() - this.options['padding']){
				this.shrink();
			};
		},
		expand : function(){			
			if(this.options['animate'] && !this.options['alreadyAnimed'] && !this.options['init']){
				this.options['alreadyAnimed'] = true;
				this.theTextarea.animate({'height':this.theHiddenDiv.height() + this.options['padding']},this.options['animationSpeed'],function(){
					prettyComments.options['alreadyAnimed'] = false;
				});
			}else if(!this.options['animate'] && !this.options['alreadyAnimed'] && !this.options['init']){
				this.theTextarea.height(this.theHiddenDiv.height() + this.options['padding']);
			}else if(this.options['init']){
				this.theTextarea.height(this.theHiddenDiv.height());
				this.options['init'] = false;
			};
		},
		shrink : function(){
			this.options['alreadyAnimed'] = true;
			this.theTextarea.animate({'height':this.theHiddenDiv.height() + this.options['padding']},this.options['animationSpeed'],function(){
				prettyComments.options['alreadyAnimed'] = false;
			});
		}
	};