/*
 * charCounter - counts down the number of characters in a textarea
 * By Andrea Pinkus (http://www.dowhatyouwish.com)
 * Copyright (c) 2009 Andrea Pinkus
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

(function($) {
	$.fn.charCounter = function(options){
		var defaults = {
			counterId:"counter",
			msgText:"%n",
			errorClass:"error"
		};  
		var options = $.extend(defaults, options);
		
		return this.each(function(){
			var maxChars = $("#" + options.counterId).text().replace(/[^0-9]/g, "");
			$(this).bind("keypress keyup keydown change click blur", function(){
				var charCount = $(this).val().length;
				$("#" + options.counterId)
					.text(options.msgText.replace("%n", maxChars - charCount))
					.toggleClass(options.errorClass, charCount > maxChars);
			}).change();
		});
	};
})(jQuery);