/*! Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Modified by Neil Monroe (neil.monroe@gmail.com)
 */
(function($){$.fn.extend({countable:function(c){return this.each(function(){var b=$(this),interval,prev_char_diff,$el;c=$.extend({threshold:.5,appendMethod:'insertAfter',target:b,startOpacity:.25,maxLength:parseInt(b.attr('maxlength'),10)||0,maxClassName:'maxed',className:'counter',tagName:'span',interval:750,positiveCopy:'You have {n}&nbsp;characters left.',negativeCopy:'You are {n}&nbsp;characters over.',fadeDuration:'normal',defaultText:''},c);$el=$('<'+c.tagName+'/>').html(c.positiveCopy.replace('{n}','<span class="num"/>')).addClass(c.className);if($.support.opacity)$el.css({opacity:0});$el[c.appendMethod](c.target);b.bind('keyup',check).bind('focus blur',function(a){if(a.type=='blur')clearInterval(interval);if(a.type=='focus'&&!interval)setInterval(check,c.interval)});function check(){var a=b.val(),length=(a==c.defaultText?0:a.length),percentage_complete=length/c.maxLength,char_diff=c.maxLength-length;if(prev_char_diff!=undefined&&char_diff==prev_char_diff)return;opacity=c.startOpacity+((c.threshold-percentage_complete)*((c.startOpacity*2)-2));if($el.is(':hidden')&&percentage_complete>=c.threshold)$el.show();if($el.is(':visible')&&percentage_complete<c.threshold)$el.hide();if($.support.opacity)$el.stop().fadeTo(c.fadeDuration,percentage_complete>=c.threshold?opacity:0);if(char_diff>=0){if($el.is('.'+c.maxClassName))$el.html(c.positiveCopy.replace('{n}','<span class="num"/>'))}else{if(!$el.is('.'+c.maxClassName))$el.html(c.negativeCopy.replace('{n}','<span class="num"/>'))}$el[(char_diff<0?'add':'remove')+'Class'](c.maxClassName);$el.find('.num').text(Math.abs(char_diff));if(char_diff==-1||char_diff==1)$el.html($el.html().replace(/characters\b/,'character'));else $el.html($el.html().replace(/character\b/,'characters'));prev_char_diff=char_diff};check()})}})})(jQuery);