/*! jQuery.counter.js (jQuery Character and Word Counter plugin)
    v2.0 (c) Wilkins Fernandez
    MIT License
*/
(function(a){a.fn.extend({counter:function(b){var c={type:"char",count:"down",goal:140,text:true,msg:""},d="",e="",b=a.extend({},c,b),f={init:function(b){var c=b.attr("id"),e=c+"_count";a("<div/>").attr("id",c+"_counter").html("<span id="+e+"/> "+f.setMsg()).insertAfter(b);d=a("#"+e);f.bind(b)},bind:function(a){a.bind("keypress.counter keydown.counter keyup.counter blur.counter focus.counter change.counter paste.counter",f.updateCounter);a.bind("keydown.counter",f.doStopTyping);a.trigger("keydown")},setMsg:function(){if(b.msg!==""){return b.msg}this.text="character word left max".split(" ");this.chars="s ( )".split(" ");this.msg="";switch(b.type){case"char":if(b.count===c.count&&b.text){this.msg=this.text[0]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(b.count==="up"&&b.text){this.msg=this.text[0]+this.chars[0]+" "+this.chars[1]+b.goal+" "+this.text[3]+this.chars[2]}break;case"word":if(b.count===c.count&&b.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(b.count==="up"&&b.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.chars[1]+b.goal+" "+this.text[3]+this.chars[2]}break;default:}return this.msg},getWords:function(b){if(b!==""){return a.trim(b).replace(/\s+/g," ").split(" ").length}else{return 0}},updateCounter:function(g){var h=a(this);if(e<0||e>b.goal){f.passedGoal(h)}if(b.type===c.type){if(b.count===c.count){e=b.goal-h.val().length}else if(b.count==="up"){e=h.val().length}d.text(e)}else if(b.type==="word"){if(b.count===c.count){e=f.getWords(h.val());e=b.goal-e;d.text(e)}else if(b.count==="up"){e=f.getWords(h.val());d.text(e)}}return},doStopTyping:function(d){var e=[46,8,9,35,36,37,38,39,40,32],g=a(this);if(f.isGoalReached(d)){if(d.keyCode!==e[0]&&d.keyCode!==e[1]&&d.keyCode!==e[2]&&d.keyCode!==e[3]&&d.keyCode!==e[4]&&d.keyCode!==e[5]&&d.keyCode!==e[6]&&d.keyCode!==e[7]&&d.keyCode!==e[8]){if(b.type===c.type){return false}else if(d.keyCode!==e[9]&&d.keyCode!==e[1]&&b.type!=c.type){return true}else{return false}}}},isGoalReached:function(a){var d;if(b.count===c.count){d=0;return e<=d?true:false}else{d=b.goal;return e>=d?true:false}},wordStrip:function(b,c){var d=c.replace(/\s+/g," ").split(" ").length;c=a.trim(c);if(b<=0||b===d){return c}else{c=a.trim(c).split(" ");c.splice(b,d,"");return a.trim(c.join(" "))}},passedGoal:function(a){var c=a.val();if(b.type==="word"){a.val(f.wordStrip(b.goal,c))}if(b.type==="char"){a.val(c.substring(0,b.goal))}if(b.type==="down"){d.val("0")}if(b.type==="up"){d.val(b.goal)}}};return this.each(function(){f.init(a(this))})}})})(jQuery)
