/*
 * 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 = {
			inputCounterId:"inputCounter",
			outputCounterId:"outputCounter",
			msgText:"%n",
			errorClass:"error"
		};  
		var options = $.extend(defaults, options);
		
		return this.each(function(){
			var maxChars = $("#" + options.inputCounterId).text().replace(/[^0-9]/g, "");
			//$(this).bind("blur", function(){
			$(this).bind("keypress keyup keydown change click blur", function(){
				$("#output").html(abbr($("#text-input").val()));
				var charCountInput = $("#text-input").val().length;
				var charCountOutput = $("#output").html().length;
				$("#" + options.inputCounterId)
					.text(options.msgText.replace("%n", maxChars - charCountInput))
					.toggleClass(options.errorClass, charCountInput > maxChars);
				$("#" + options.outputCounterId)
					.text(options.msgText.replace("%n", maxChars - charCountOutput))
					.toggleClass(options.errorClass, charCountOutput > maxChars);
			}).change();
		});
	};
})(jQuery);