/* http://keith-wood.name/maxlength.html
   Textarea Max Length for jQuery v1.1.0.
   Written by Keith Wood (kwood{at}iinet.com.au) May 2009.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
(function($){function MaxLength(){this.regional=[];this.regional['']={feedbackText:'{r} characters remaining ({m} maximum)',overflowText:'{o} characters too many ({m} maximum)'};this._defaults={max:200,truncate:true,showFeedback:true,feedbackTarget:null,onFull:null};$.extend(this._defaults,this.regional[''])}$.extend(MaxLength.prototype,{markerClassName:'hasMaxLength',propertyName:'maxlength',_feedbackClass:'maxlength-feedback',_fullClass:'maxlength-full',_overflowClass:'maxlength-overflow',_disabledClass:'maxlength-disabled',setDefaults:function(a){$.extend(this._defaults,a||{});return this},_attachPlugin:function(c,d){c=$(c);if(c.hasClass(this.markerClassName)){return}var e={options:$.extend({},this._defaults),feedbackTarget:$([])};c.addClass(this.markerClassName).data(this.propertyName,e).bind('keypress.'+this.propertyName,function(a){if(!e.options.truncate){return true}var b=String.fromCharCode(a.charCode==undefined?a.keyCode:a.charCode);return(a.ctrlKey||a.metaKey||b=='\u0000'||$(this).val().length<e.options.max)}).bind('keyup.'+this.propertyName,function(){j._checkLength($(this))});this._optionPlugin(c,d)},_optionPlugin:function(a,b,c){a=$(a);var d=a.data(this.propertyName);if(!b||(typeof b=='string'&&c==null)){var e=b;b=(d||{}).options;return(b&&e?b[e]:b)}if(!a.hasClass(this.markerClassName)){return}b=b||{};if(typeof b=='string'){var e=b;b={};b[e]=c}$.extend(d.options,b);if(d.feedbackTarget.length>0){if(d.hadFeedbackTarget){d.feedbackTarget.empty().val('').removeClass(this._feedbackClass+' '+this._fullClass+' '+this._overflowClass)}else{d.feedbackTarget.remove()}d.feedbackTarget=$([])}if(d.options.showFeedback){d.hadFeedbackTarget=!!d.options.feedbackTarget;if($.isFunction(d.options.feedbackTarget)){d.feedbackTarget=d.options.feedbackTarget.apply(a[0],[])}else if(d.options.feedbackTarget){d.feedbackTarget=$(d.options.feedbackTarget)}else{d.feedbackTarget=$('<span></span>').insertAfter(a)}d.feedbackTarget.addClass(this._feedbackClass)}a.unbind('mouseover.'+this.propertyName+' focus.'+this.propertyName+'mouseout.'+this.propertyName+' blur.'+this.propertyName);if(d.options.showFeedback=='active'){a.bind('mouseover.'+this.propertyName,function(){d.feedbackTarget.css('visibility','visible')}).bind('mouseout.'+this.propertyName,function(){if(!d.focussed){d.feedbackTarget.css('visibility','hidden')}}).bind('focus.'+this.propertyName,function(){d.focussed=true;d.feedbackTarget.css('visibility','visible')}).bind('blur.'+this.propertyName,function(){d.focussed=false;d.feedbackTarget.css('visibility','hidden')});d.feedbackTarget.css('visibility','hidden')}this._checkLength(a)},_curLengthPlugin:function(a){var b=a.data(this.propertyName);var c=a.val();var d=c.replace(/\r\n/g,'~~').replace(/\n/g,'~~').length;return{used:d,remaining:b.options.max-d}},_checkLength:function(a){var b=a.data(this.propertyName);var c=a.val();var d=c.replace(/\r\n/g,'~~').replace(/\n/g,'~~').length;a.toggleClass(this._fullClass,d>=b.options.max).toggleClass(this._overflowClass,d>b.options.max);if(d>b.options.max&&b.options.truncate){var f=a.val().split(/\r\n|\n/);c='';var i=0;while(c.length<b.options.max&&i<f.length){c+=f[i].substring(0,b.options.max-c.length)+'\r\n';i++}a.val(c.substring(0,b.options.max));a[0].scrollTop=a[0].scrollHeight;d=b.options.max}b.feedbackTarget.toggleClass(this._fullClass,d>=b.options.max).toggleClass(this._overflowClass,d>b.options.max);var g=(d>b.options.max?b.options.overflowText:b.options.feedbackText).replace(/\{c\}/,d).replace(/\{m\}/,b.options.max).replace(/\{r\}/,b.options.max-d).replace(/\{o\}/,d-b.options.max);try{b.feedbackTarget.text(g)}catch(e){}try{b.feedbackTarget.val(g)}catch(e){}if(d>=b.options.max&&$.isFunction(b.options.onFull)){b.options.onFull.apply(a,[d>b.options.max])}},_enablePlugin:function(a){a=$(a);if(!a.hasClass(this.markerClassName)){return}a.prop('disabled',false).removeClass(this.propertyName+'-disabled');var b=a.data(this.propertyName);b.feedbackTarget.removeClass(this.propertyName+'-disabled')},_disablePlugin:function(a){a=$(a);if(!a.hasClass(this.markerClassName)){return}a.prop('disabled',true).addClass(this.propertyName+'-disabled');var b=a.data(this.propertyName);b.feedbackTarget.addClass(this.propertyName+'-disabled')},_destroyPlugin:function(a){a=$(a);if(!a.hasClass(this.markerClassName)){return}var b=a.data(this.propertyName);if(b.feedbackTarget.length>0){if(b.hadFeedbackTarget){b.feedbackTarget.empty().val('').css('visibility','visible').removeClass(this._feedbackClass+' '+this._fullClass+' '+this._overflowClass)}else{b.feedbackTarget.remove()}}a.removeClass(this.markerClassName+' '+this._fullClass+' '+this._overflowClass).removeData(this.propertyName).unbind('.'+this.propertyName)}});var h=['curLength'];function isNotChained(a,b){if(a=='option'&&(b.length==0||(b.length==1&&typeof b[0]=='string'))){return true}return $.inArray(a,h)>-1}$.fn.maxlength=function(a){var b=Array.prototype.slice.call(arguments,1);if(isNotChained(a,b)){return j['_'+a+'Plugin'].apply(j,[this[0]].concat(b))}return this.each(function(){if(typeof a=='string'){if(!j['_'+a+'Plugin']){throw'Unknown method: '+a;}j['_'+a+'Plugin'].apply(j,[this].concat(b))}else{j._attachPlugin(this,a||{})}})};var j=$.maxlength=new MaxLength()})(jQuery);