/*! word-and-character-counter.js v2.4 (c) Wilkins Fernandez MIT License */ (function(e){e.fn.extend({counter:function(t){var n={type:"char",count:"down",goal:140,text:true,target:false,append:true,translation:"",msg:""};var r="",i="",s=false,t=e.extend({},n,t);var o={init:function(n){var i=n.attr("id"),s=i+"_count";o.isLimitless();r=e("");var u=e("
").attr("id",i+"_counter").addClass("input-counter").append(r).append(" "+o.setMsg());if(!t.target||!e(t.target).length){t.append?u.insertAfter(n):u.insertBefore(n)}else{t.append?e(t.target).append(u):e(t.target).prepend(u)}o.bind(n)},bind:function(e){e.bind("keypress.counter keydown.counter keyup.counter blur.counter focus.counter change.counter paste.counter",o.updateCounter);e.bind("keydown.counter",o.doStopTyping);e.trigger("keydown")},isLimitless:function(){if(t.goal==="sky"){t.count="up";s=true;return s}},setMsg:function(){if(t.msg!==""){return t.msg}if(t.text===false){return""}if(s){if(t.msg!==""){return t.msg}else{return""}}this.text=t.translation||"character word left max";this.text=this.text.split(" ");this.chars="s ( )".split(" ");this.msg=null;switch(t.type){case"char":if(t.count===n.count&&t.text){this.msg=this.text[0]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(t.count==="up"&&t.text){this.msg=this.text[0]+this.chars[0]+" "+this.chars[1]+t.goal+" "+this.text[3]+this.chars[2]}break;case"word":if(t.count===n.count&&t.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(t.count==="up"&&t.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.chars[1]+t.goal+" "+this.text[3]+this.chars[2]}break;default:}return this.msg},getWords:function(t){if(t!==""){return e.trim(t).replace(/\s+/g," ").split(" ").length}else{return 0}},updateCounter:function(s){var u=e(this);if(i<0||i>t.goal){o.passedGoal(u)}var a=e(r).parent();if(t.type===n.type){if(t.count===n.count){i=t.goal-u.val().length;if(i<=0){r.text("0")}else{r.text(i)}}else if(t.count==="up"){i=u.val().length;r.text(i)}}else if(t.type==="word"){if(t.count===n.count){i=o.getWords(u.val());if(i<=t.goal){i=t.goal-i;r.text(i)}else{r.text("0")}}else if(t.count==="up"){i=o.getWords(u.val());r.text(i)}}var f=i/t.goal;if(f==0){jQuery(a).removeClass("near-limit");jQuery(a).addClass("at-limit")}else if(f<=.34){jQuery(a).removeClass("at-limit");jQuery(a).addClass("near-limit")}else{jQuery(a).removeClass("near-limit");jQuery(a).removeClass("at-limit")}return},doStopTyping:function(e){var r=[46,8,9,35,36,37,38,39,40,32];if(o.isGoalReached(e)){if(e.keyCode!==r[0]&&e.keyCode!==r[1]&&e.keyCode!==r[2]&&e.keyCode!==r[3]&&e.keyCode!==r[4]&&e.keyCode!==r[5]&&e.keyCode!==r[6]&&e.keyCode!==r[7]&&e.keyCode!==r[8]){if(t.type===n.type){return false}else if(e.keyCode!==r[9]&&e.keyCode!==r[1]&&t.type!=n.type){return true}else{return false}}}},isGoalReached:function(e,r){if(s){return false}if(t.count===n.count){r=0;return i<=r?true:false}else{r=t.goal;return i>=r?true:false}},wordStrip:function(t,n){var r=n.replace(/\s+/g," ").split(" ").length;n=e.trim(n);if(t<=0||t===r){return n}else{n=e.trim(n).split(" ");n.splice(t,r,"");return e.trim(n.join(" "))}},passedGoal:function(e){var n=e.val();if(t.type==="word"){e.val(o.wordStrip(t.goal,n))}if(t.type==="char"){e.val(n.substring(0,t.goal))}if(t.type==="down"){r.val("0")}if(t.type==="up"){r.val(t.goal)}}};return this.each(function(){o.init(e(this))})}})})(jQuery)