
var CharCounter = Class.create({
	initialize: function (element, options) {
		this.element = $(element);
		this.options = Object.extend({
			maxLength: 0,
			elCounter: null,
			countDown: false
		}, options);
		Event.observe ( element, 
			'keyup', 
			this.count.bindAsEventListener(this));
	},
	count: function ( evt ) {
		var txt = evt.element().getValue();
		var len = txt.length;
		var max = this.options.maxLength;
		if (len>=max){
			evt.element().value = txt.substring(0,max);
			evt.stop(); // just don't do it!
		}
		// check it we need to display the current len
		if (this.options.elCounter) {
			$(this.options.elCounter).innerHTML = ( this.options.countDown ? max - len : len );	
		}		
	}
});

