CharacterCounter = Class.create();
CharacterCounter.prototype = {
	initialize: function(textareaId, counterId, maxCount){
		this.textarea = $(textareaId);
		this.counter = $(counterId);
		this.maxCount = maxCount;
    // hook up events on textarea
    // tally on key up/down events
    // still doesn't capture holding down of key (at least in FF on Linux)
		Event.observe(this.textarea,'keyup',this.enforceAndDisplayCount.bind(this));
    // tally when focus is acquired
		Event.observe(this.textarea,'focus',this.enforceAndDisplayCount.bind(this));
    // handle single button paste events (such as middle button on Linux)
		Event.observe(this.textarea,'click',this.enforceAndDisplayCount.bind(this));
    // re-tally when textarea loses focus (e.g. from mouse paste event)
		Event.observe(this.textarea,'change',this.enforceAndDisplayCount.bind(this));
		this.enforceAndDisplayCount();
	},

	enforceAndDisplayCount: function(){
    var curr_count = this.maxCount - this.textarea.value.length;
		if (curr_count <= 0) {
			this.textarea.value = this.textarea.value.substr(0,this.maxCount);
		}
    else {
  		this.counter.update(curr_count);
    }
	}
}