/*
 * jQuery Growfield Library 2
 *
 * http://code.google.com/p/jquery-dynamic/
 * licensed under the MIT license
 *
 * autor: john kuindji
 *
 * With additional features by Ezekiel Victor.
 */
(function(d){if(d.support==void 0)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 j=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,onComplete:function(){}};this.enabled=this.dummy=this.busy=this.initial=this.sizeRelated=this.prevH=this.firstH=false};j.prototype={toggle:function(a){return(a=="disable"||a===false)&&this.enabled?this.setEvents("off"):(a=="enable"||a===true)&&!this.enabled?this.setEvents("on"):this},setEvents:function(a){var b=this.o,e=this.opt,c=this,f=false;if(a=="on"&&!this.enabled){a=b.height()==0?true:false;if(!a||h)d(function(){c.prepareSizeRelated()});else d(window).one("load",
function(){c.prepareSizeRelated()});if(e.auto)if(b.bind("keyup.growfield",function(a){c.keyUp(a);return true}),b.bind("focus.growfield",function(a){c.focus(a);return true}),b.bind("blur.growfield",function(a){c.blur(a);return true}),f={overflow:b.css("overflow"),cssResize:b.css("resize")},d.browser.safari&&b.css("resize","none"),this.initial=f,b.css({overflow:"hidden"}),!a||h)d(function(){c.createDummy()});else d(window).one("load",function(){c.createDummy()});else if(b.bind("keydown.growfield",function(a){c.manualKeyUp(a);
return true}),b.css("overflow-y","auto"),!a||h)d(function(){c.update(b.height())});else 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 d=this.o.val(),c=this.opt,f=this.dom,g=this.o,h=this,k=this.prevH,j=!c.auto,l=c.auto,a=this.convertHeight(Math.round(a),"inner"),a=c.min>a?c.min:c.max&&a>c.max?c.max:c.auto&&!d?c.min:a;c.max&&c.auto&&(k!=c.max&&a==c.max&&(g.css("overflow-y","scroll"),c.animate||g.focus(),j=true,l=false),k==c.max&&a<c.max&&(g.css("overflow-y","hidden"),c.animate||g.focus(),l=false));if(a==k)return true;this.prevH=a;b?(h.busy=true,g.animate({height:a},{duration:c.animate,easing:c.easing,overflow:null,inline:true,
complete:function(){j||g.css("overflow","hidden");l||g.focus();h.busy=false;c.onComplete()},queue:false})):(f.style.height=a+"px",c.onComplete())},manualKeyUp:function(a){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;d.browser.safari&&(a=a.substring(0,a.length-1));if(!e.lh||!e.fs)a+="\n111\n111";this.dummy.val(a);if(d.browser.msie)this.dummy[0].style.height=this.dummy[0].scrollHeight+"px";b=this.dummy[0].scrollHeight;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,f=d.support.boxModel;return a+(f?e.bt:0)*c+(f?e.bb:0)*c+(f?e.pt:0)*c+(f?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,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 a=d(this).data("growfield");if(a==void 0)return true;a.toggle(false);d(this).removeData("growfield");return true});if("restart"==a)return this.each(function(){var a=d(this).data("growfield");if(a==void 0)return true;a.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),f=c.data("growfield");f==void 0&&(e=true,c.data("growfield",new j(this)),f=c.data("growfield"));if(e){var g=d.extend({},d.fn.growfield.defaults,a);f.setOptions(g)}!e&&(!a||b=="object")&&f.setOptions(a);b=="string"&&(a.indexOf("!")==0&&d.fn.growfield.presets[a.substr(1)]?c.unbind("."+i+"."+a.substr(1)):d.fn.growfield.presets[a]&&f.setOptions(d.fn.growfield.presets[a],a));e&&!g.skipEnable&&f.toggle(true);!e&&(b=="boolean"||a=="enable"||a=="disable")&&f.toggle(a)})};d.fn.growfield.defaults=
{};d.fn.growfield.presets={}})(jQuery);
