/* ------------------------------------------------------------
 * PROJECT        : 
 * FILENAME       : jq.charCounter.js
 * ------------------------------------------------------------
 * LAST UPDATED   : 23 Oct 2009
 * ------------------------------------------------------------
 * AUTHOR(S)      : Tom Deater (http://www.tomdeater.com)
 *                : Kevin Scholl (ksscholl@magellanhealth.com)
 * ------------------------------------------------------------
 * NOTE(S)        : 
 * ------------------------------------------------------------ */

/*

SUMMARY

This plugin attaches a character counter to the specified textarea element.

OPTIONAL SETTINGS

max
An integer specifying the maximum number of characters allowed. Default is 255.

container
A string specifying the HTML element that will contain the counter display.

classname
A string denoting the class to apply to the container element.

format
A string specifying the appearance of the counter display

pulse
A boolean value stating whether the counter should blink when the maximum number fo characters is reached

USAGE

$("el").charCounter(max, settings);

EXAMPLE

$(document).ready(function() {
	$("#el").toggleModule({
		initialState : "hidden",
		moreText     : "Example 3"
		});
	});

MINIMUM BROWSER SUPPORT

Apple Safari 3, Google Chrome 1, Microsoft Internet Explorer 6, Mozilla Firefox 3, Opera 9

*/

jQuery.fn.charCounter = function(max, settings) {
	max = max || 255;
	settings = $.extend({
		container : "<span style=\"display: block\">",
		classname : "example",
		format    : "(%1 / " + max + " characters left)",
		pulse     :  true
		}, settings);
	var p;
	
	function count(el, container) {
		el = $(el);
		if (el.val().length > max) {
			el.val(el.val().substring(0, max));
			if (settings.pulse && !p) {
				pulse(container, true);
				};
			};
		container.html(settings.format.replace(/%1/, (max - el.val().length)));
		};
	
	function pulse(el, again) {
		if (p) {
			window.clearTimeout(p);
			p = null;
			};
		el.animate({ opacity: 0.1 }, 100, function() {
			$(this).animate({ opacity: 1.0 }, 100);
			});
		if (again) {
			p = window.setTimeout(function() { pulse(el) }, 200);
			};
		};
	
	return this.each(function() {
		var container = (!settings.container.match(/^<.+>$/)) 
			? $(settings.container) 
			: $(settings.container)
				.insertAfter(this)
				.addClass(settings.classname);
		$(this)
			.bind("keydown", function() { count(this, container); })
			.bind("keypress", function() { count(this, container); })
			.bind("keyup", function() { count(this, container); })
			.bind("focus", function() { count(this, container); })
			.bind("mouseover", function() { count(this, container); })
			.bind("mouseout", function() { count(this, container); })
			.bind("paste", function() { 
				var me = this;
				setTimeout(function() { count(me, container); }, 10);
				});
		if (this.addEventListener) {
			this.addEventListener('input', function() { count(this, container); }, false);
			};
		count(this, container);
		});
	
	};