var TextLimit = Class.create();

TextLimit.prototype = {
	initialize: function(textfield, remaining, length) {
		this.length = length;
		Event.observe(textfield, 'keypress', this._check_length.bindAsEventListener(this));
		Event.observe(textfield, 'keyup', this._check_length.bindAsEventListener(this));
		Event.observe(textfield, 'keydown', this._check_length.bindAsEventListener(this));
		this.content = textfield;
		this.remaining = remaining;
		this._check_length();
	},

	_check_length: function() {
		var field = $(this.content);
		if (field.value.length > this.length) {
			field.value = field.value.substring(0, this.length);
		} else {
			$(this.remaining).value = this.length - field.value.length;
			$(this.remaining).scrollTop = $(this.remaining).scrollHeight;
	        }
	}
}