// jQuery.autoExpand Textarea (1.0)
// Sebastian Geidies (geidies@gmail.com)
// 
// Use and distibution is allowed under the terms & conditions of the GPL (http://www.gnu.org/licenses/gpl.html).
// This script requires jQuery to work. Download jQuery at http://jquery.com

(function($){
  $.fn.autoExpand = function(minheight){
    var minheight = minheight && !isNaN(parseInt(minheight, 10)) ? minheight : 120;
    var self = $(this);
    self.each(function(){
      var i = $(".autoexpand_clone").length;
      var obj = $(this);
      var lineheight = $("<p/>").width(obj.width()).appendTo("body").attr("id", "autoexpand_clone_"+i.toString(10) ).addClass("autoexpand_clone")
      .css({"position":"absolute",
          "left": (-1*obj.width())+"px",
          "top": "0px",
          "line-height": obj.css("line-height"),
          "font-size": obj.css("font-size"),
          "font-family": obj.css("font-family"),
          "overflow":"visible" }).html("Ayg<br>").height() + 2;
       $("#autoexpand_clone_" + i.toString(10) ).css("line-height", lineheight + "px");
       var expand = function(){
         var h = $("#autoexpand_clone_" + i.toString(10) ).html( obj.val().replace(/\n/g,"<br/>") ).height() + lineheight;
         obj.animate( {"height": h > minheight ? h : minheight } ,80 );
      };
      expand();
      obj.keyup(function(){
        expand();
      });
    });
    return self;
  };
	
	
	/* ------------------------------------------------------------------------
	Pretty Comments
	
	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: The facebook textarea :)
	Version: 1.4
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */

	$.fn.prettyComments = function(settings) {
		settings = jQuery.extend({
					animate: false, /* If you set it to true, cursor will dissapear in FF3 */
					animationSpeed: 'fast', /* fast/slow/normal */
					maxHeight : 400,
					alreadyAnimated: false, /* DONT CHANGE */
					init: true /* DONT CHANGE */
				}, settings);

		// Create the div in which the content will be copied
		$('body').append('<div id="comment_hidden"></div>');

		var setCSS = function(which){
			// Init the div for the current textarea
			$("#comment_hidden").css({
				'position':'absolute',
				'top': -10000,
				'left': -10000,
				'width': $(which).width(),
				'min-height': $(which).height(),
				'font-family': $(which).css('font-family'),
				'font-size': $(which).css('font-size'),
				'line-height': $(which).css('line-height')
			});
			
			if($.browser.msie && parseFloat($.browser.version) < 7){
				$("#comment_hidden").css('height',$(which).height());
			};
		};
		
		var copyContent = function(which){
			// Convert the line feeds into BRs
			theValue = $(which).attr('value') || "";
			theValue = theValue.replace(/\n/g,'<br />');
			
			$("#comment_hidden").html(theValue + '<br />');
			
			if(!settings.init){
				if($("#comment_hidden").height() > $(which).height()){
					if($('#comment_hidden').height() > settings.maxHeight){
						$(which).css('overflow-y','scroll');
					}else{
						$(which).css('overflow-y','hidden');
						expand(which);
					};
				}else if($("#comment_hidden").height() < $(which).height()){
					if($('#comment_hidden').height() > settings.maxHeight){
						$(which).css('overflow-y','scroll');
					}else{
						$(which).css('overflow-y','hidden');
						shrink(which);
					};
				};
			};
		};
		
		var expand = function(which){
			if(settings.animate && !settings.alreadyAnimated){
				settings.alreadyAnimated = true;
				$(which).animate({'height':$("#comment_hidden").height()},settings.animationSpeed,function(){
					settings.alreadyAnimated = false;
				});
			}else if(!settings.animate && !settings.alreadyAnimated){
				$(which).height($("#comment_hidden").height());
			};
		};
		
		var shrink = function(which){
			if(settings.animate && !settings.alreadyAnimated){
				settings.alreadyAnimated = true;
				$(which).animate({'height':$("#comment_hidden").height()},settings.animationSpeed,function(){
					settings.alreadyAnimated = false;
				});
			}else{
				$(which).height($("#comment_hidden").height());
			};
		};
		
		$(this).each(function(){
			$(this).css({
				'overflow':'hidden'
			})
			.bind('keyup',function(){
				copyContent($(this));
			});
			
			// Make sure all the content in the textarea is visible
			setCSS(this);
			copyContent($(this));
			
			if($("#comment_hidden").height() > settings.maxHeight){
				$(this).css({
					'overflow-y':'scroll',
					'height':settings.maxHeight
				});
			}else{
				$(this).height($("#comment_hidden").height());
			};
			
			settings.init = false;
		});
	};
	
	
	
	
	
	
	
	
	
})(jQuery);