(function($)
{
   $.fn.expandableinput = function (settings) {

      var copypaste=false;

      var config = {
         cssClass: "",
         rowsNo: "1"
      };

      if (settings) {
         $.extend(config, settings);
      }

      function expandContent(element) {
         var boxHeight = element.scrollHeight;
         if (navigator.userAgent.indexOf('Safari') != -1  //safari
            || (document.all && !(navigator.userAgent.indexOf( 'Opera' ) != -1))) { //ie
            boxHeight -= 2;
         }

         $(element)[0].style.height = boxHeight+"px";
       }

      return this.each(function () {
         $(this).attr("rows", config.rowsNo);
         if (config != null && config.cssClass != "") {
            $(this).addClass(config.cssClass);
         }
         $(this).css("overflow","hidden");

         $(this).bind("keydown", function (event) {
            switch (event.which) {
               case 13: // enter
                  // on ctrl+enter expand one line
                  if (event.ctrlKey == true) {
                     $(this).val($(this).val() + '\n');  //TODO: IE doesnt move cursor to new line
                     expandContent(this);
                  }
                  // on enter submit the form
                  if (event.ctrlKey == false) {
                     event.preventDefault();
                     $(this).blur();
                     $(this).closest("form").focus().submit();
                  }
              case 86: // ctrl+v
                   if (event.ctrlKey == true) {
                     copypaste = true;
                   }
                  break;
               default:
                  expandContent(this);
                  break;
            }
         });

         $(this).bind("keyup", function (event) {
            switch (event.which) {
                case 86: // ctrl+v
                   if (copypaste == true) {
                     var e = jQuery.Event("keydown");
                     e.which = 40;
                     $(this).trigger(e);
                     copypaste=false;
                   }
                   break;
               default:
                  break;
            }
         });
      });
   };
})(jQuery);
