!function(a){a.textcounter=function(b,c){var d=this;d.$el=a(b),d.el=b,d.$el.data("textcounter",d),d.init=function(){d.options=a.extend({},a.textcounter.defaultOptions,c);var b=d.options.countDown?d.options.countDownText:d.options.counterText,e=d.options.countDown?d.options.max:0;d.$el.after("<"+d.options.countContainerElement+' class="'+d.options.countContainerClass+'">'+b+'<span class="'+d.options.textCountClass+'">'+e+"</span></"+d.options.countContainerElement+">"),d.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",d.checkLimits).trigger("click.textcounter"),d.options.init(d.el)},d.checkLimits=function(b){var c=d.$el,e=(c.next("."+d.options.countContainerClass),c.val()),f=0,g=0,h=void 0!==b.originalEvent;if(!a.isEmptyObject(e))if("word"==d.options.type)f=e.trim().replace(/\s+/gi," ").split(" ").length;else if(f=d.options.countSpaces?e.replace(/[^\S\n|\r|\r\n]/g," ").length:e.replace(/\s/g,"").length,d.options.countExtendedCharacters){var i=e.match(/[^\x00-\xff]/gi);f=null==i?e.length:e.length+i.length}if("auto"==d.options.max){var j=d.$el.attr("maxlength");"undefined"!=typeof j&&j!==!1?d.options.max=j:d.$el.next("."+d.options.countContainerClass).text("error: [maxlength] attribute not set")}if(g=d.options.countDown?d.options.max-f:f,d.setCount(g),d.options.min>0&&h&&(f<d.options.min?d.setErrors("min"):f>=d.options.min&&(d.options.mincount(d.el),d.clearErrors("min"))),d.options.max!==-1)if(f>=d.options.max&&0!=d.options.max)if(d.options.maxcount(d.el),d.options.stopInputAtMaximum){var k="";if("word"==d.options.type)for(var l=e.split(/[^\S\n]/g),m=0;m<l.length&&!(m>=d.options.max-1);)void 0!==l[m]&&(k+=l[m]+" ",m++);else if(d.options.countSpaces)k=e.substring(0,d.options.max);else for(var n=e.split(""),o=n.length,p=0,m=0;p<d.options.max&&m<o;)" "!==n[m]&&p++,k+=n[m++];c.val(k.trim()),g=d.options.countDown?0:d.options.max,d.setCount(g)}else d.setErrors("max");else d.clearErrors("max")},d.setCount=function(a){var b=d.$el,c=b.next("."+d.options.countContainerClass);c.children("."+d.options.textCountClass).text(a)},d.setErrors=function(a){var b=d.$el,c=b.next("."+d.options.countContainerClass);if(b.addClass(d.options.inputErrorClass),c.addClass(d.options.counterErrorClass),d.options.displayErrorText){switch(a){case"min":errorText=d.options.minimumErrorText;break;case"max":errorText=d.options.maximumErrorText}c.children(".error-text-"+a).length||c.append("<"+d.options.errorTextElement+' class="error-text error-text-'+a+'">'+errorText+"</"+d.options.errorTextElement+">")}},d.clearErrors=function(a){var b=d.$el,c=b.next("."+d.options.countContainerClass);c.children(".error-text-"+a).remove(),0==c.children(".error-text").length&&(b.removeClass(d.options.inputErrorClass),c.removeClass(d.options.counterErrorClass))},d.init()},a.textcounter.defaultOptions={type:"character",min:0,max:200,countContainerElement:"div",countContainerClass:"text-count-wrapper",textCountClass:"text-count",inputErrorClass:"error",counterErrorClass:"error",counterText:"Total Count: ",errorTextElement:"div",minimumErrorText:"Minimum not met",maximumErrorText:"Maximum exceeded",displayErrorText:!0,stopInputAtMaximum:!0,countSpaces:!1,countDown:!1,countDownText:"Remaining: ",countExtendedCharacters:!1,maxcount:function(a){},mincount:function(a){},init:function(a){}},a.fn.textcounter=function(b){return this.each(function(){new a.textcounter(this,b)})}}(jQuery);