jQuery.fn.counter = function(max) {

	jQuery(this).each(function()
	{
		jQuery(this).attr('maxlength', max);

		var val = $(this).attr('value');
		var cur = 0;
		if (val) // value="", or no value at all will cause an error
		{
			cur = val.length;
		}
		var left = max - cur;
		$(this).parent().after('<div class="counter">' + i18n.remainingCharacterCount + ' <span class="number">' + left.toString() + '</span></div>');

		$(this).keypress(function(e) {
			var max = $(this).attr('maxlength');
			var val = $(this).attr('value');
			var cur = 0;
			if (val)
			{
				cur = val.length;
			}
			var left = max - cur;
			if (left < 0)
			{
				$(this).val($(this).val().substr(0, max));
				left = 0;
			}
			$('.number', $(this).parent().next('.counter')).text(left.toString());
			return this;
		});
		$(this).focus(function(e){
			$(this).parent().next('.counter').show();
		});
		$(this).blur(function(e){
			$(this).parent().next('.counter').hide();
		});
	});
	return this;
}

jQuery(document).ready(function()
{
	jQuery('#comment').counter(2000);
});
