/*
 * jQuery Growfield Library 2
 *
 * http://code.google.com/p/jquery-dynamic/
 * licensed under the MIT license
 *
 * autor: john kuindji
 */
(function(d){if(d.support==undefined)d.support={boxModel:d.boxModel};var h=false;d(window).one("load",function(){h=true});d.fx.prototype.originalUpdate=d.fx.prototype.update;d.fx.prototype.update=false;d.fx.prototype.update=function(){if(!this.options.inline)return this.originalUpdate.call(this);this.options.step&&this.options.step.call(this.elem,this.now,this);(jQuery.fx.step[this.prop]||jQuery.fx.step._default)(this)};var l=function(a){this.dom=a;this.o=d(a);this.opt={auto:true,animate:100,easing:null,
min:false,max:false,restore:false,step:false};this.enabled=this.dummy=this.busy=this.initial=this.sizeRelated=this.prevH=this.firstH=false};l.prototype={toggle:function(a){if((a=="disable"||a===false)&&this.enabled)return this.setEvents("off");if((a=="enable"||a===true)&&!this.enabled)return this.setEvents("on");return this},setEvents:function(a){var b=this.o,e=this.opt,c=this,g=false;if(a=="on"&&!this.enabled){a=b.height()==0?true:false;!a||h?d(function(){c.prepareSizeRelated()}):d(window).one("load",
function(){c.prepareSizeRelated()});if(e.auto){b.bind("keyup.growfield",function(f){c.keyUp(f);return true});b.bind("focus.growfield",function(f){c.focus(f);return true});b.bind("blur.growfield",function(f){c.blur(f);return true});g={overflow:b.css("overflow"),cssResize:b.css("resize")};d.browser.safari&&b.css("resize","none");this.initial=g;b.css({overflow:"hidden"});!a||h?d(function(){c.createDummy()}):d(window).one("load",function(){c.createDummy()})}else{b.bind("keydown.growfield",function(f){c.manualKeyUp(f);
return true});b.css("overflow-y","auto");!a||h?d(function(){c.update(b.height())}):d(window).one("load",function(){c.update(b.height())})}b.addClass("growfield");this.enabled=true}else if(a=="off"&&this.enabled){if(this.dummy){this.dummy.remove();this.dummy=false}b.unbind(".growfield").css("overflow",this.initial.overflow);d.browser.safari&&b.css("resize",this.initial.cssResize);this.enabled=false}return this},setOptions:function(a){var b=this.opt;d.extend(b,a);if(!d.easing)b.easing=null},update:function(a,
b){var e=this.o.val(),c=this.opt,g=this.dom,f=this.o,m=this,j=this.prevH,n=!c.auto,k=c.auto;a=this.convertHeight(Math.round(a),"inner");a=c.min>a?c.min:c.max&&a>c.max?c.max:c.auto&&!e?c.min:a;if(c.max&&c.auto){if(j!=c.max&&a==c.max){f.css("overflow-y","scroll");c.animate||f.focus();n=true;k=false}if(j==c.max&&a<c.max){f.css("overflow-y","hidden");c.animate||f.focus();k=false}}if(a==j)return true;this.prevH=a;if(b){m.busy=true;f.animate({height:a},{duration:c.animate,easing:c.easing,overflow:null,
inline:true,complete:function(){n||f.css("overflow","hidden");k||f.focus();m.busy=false},queue:false})}else g.style.height=a+"px"},manualKeyUp:function(a){if(a.ctrlKey)a.keyCode!=38&&a.keyCode!=40||this.update(this.o.outerHeight()+this.opt.step*(a.keyCode==38?-1:1),this.opt.animate)},keyUp:function(a){if(this.busy)return true;if(d.inArray(a.keyCode,[37,38,39,40])!=-1)return true;this.update(this.getDummyHeight(),this.opt.animate)},focus:function(){if(this.busy)return true;this.opt.restore&&this.update(this.getDummyHeight(),
this.opt.animate)},blur:function(){if(this.busy)return true;this.opt.restore&&this.update(0,false)},getDummyHeight:function(){var a=this.o.val(),b=0,e=this.sizeRelated;if(d.browser.safari)a=a.substring(0,a.length-1);if(!e.lh||!e.fs)a+="\n111";this.dummy.val(a);if(d.browser.msie)this.dummy[0].style.height=this.dummy[0].scrollHeight+"px";b=this.dummy[0].scrollHeight;if(e.lh&&e.fs)b+=e.lh>e.fs?e.lh+e.fs:e.fs*2;if(d.browser.msie)this.dummy[0].style.height="20px";return b},createDummy:function(){var a=
this.o,b=this.o.val();a=a.clone().addClass("growfieldDummy").attr("name","").attr("tabindex",-9999).css({position:"absolute",left:-9999,top:0,height:"20px",resize:"none"}).insertBefore(a).show();b||a.val("dummy text");this.dummy=a;this.update(!jQuery.trim(b)?0:this.getDummyHeight(),false)},convertHeight:function(a,b){var e=this.sizeRelated,c=b=="inner"?-1:1,g=d.support.boxModel;return a+(g?e.bt:0)*c+(g?e.bb:0)*c+(g?e.pt:0)*c+(g?e.pb:0)*c},prepareSizeRelated:function(){var a=this.o,b=this.opt;if(!b.min){b.min=
parseInt(a.css("min-height"),10)||this.firstH||parseInt(a.height(),10)||20;if(b.min<=0)b.min=20;if(!this.firstH)this.firstH=b.min}if(!b.max){b.max=parseInt(a.css("max-height"),10)||false;if(b.max<=0)b.max=false}if(!b.step)b.step=parseInt(a.css("line-height"),10)||parseInt(a.css("font-size"),10)||20;this.sizeRelated={pt:parseInt(a.css("paddingTop"),10)||0,pb:parseInt(a.css("paddingBottom"),10)||0,bt:parseInt(a.css("borderTopWidth"),10)||0,bb:parseInt(a.css("borderBottomWidth"),10)||0,lh:parseInt(a.css("lineHeight"),
10)||false,fs:parseInt(a.css("fontSize"),10)||false}}};d.fn.growfield=function(a){if("destroy"==a)return this.each(function(){var e=d(this).data("growfield");if(e==undefined)return true;e.toggle(false);d(this).removeData("growfield");return true});if("restart"==a)return this.each(function(){var e=d(this).data("growfield");if(e==undefined)return true;e.toggle(false).toggle(true)});var b=typeof a;return this.each(function(){if(!/textarea/i.test(this.tagName)||d(this).hasClass("growfieldDummy"))return true;
var e=false,c=d(this),g=c.data("growfield");if(g==undefined){e=true;c.data("growfield",new l(this));g=c.data("growfield")}if(e){var f=d.extend({},d.fn.growfield.defaults,a);g.setOptions(f)}if(!e&&(!a||b=="object"))g.setOptions(a);if(b=="string")if(a.indexOf("!")==0&&d.fn.growfield.presets[a.substr(1)])c.unbind("."+i+"."+a.substr(1));else d.fn.growfield.presets[a]&&g.setOptions(d.fn.growfield.presets[a],a);e&&!f.skipEnable&&g.toggle(true);if(!e&&(b=="boolean"||a=="enable"||a=="disable"))g.toggle(a)})};
d.fn.growfield.defaults={};d.fn.growfield.presets={}})(jQuery);
