var charCounter = new Class({

	counterObject: null,

	areaObject: null,

	areaLimit: 300,

	areaCounter: 0,

	initialize: function(areaId, targetId, options) {
		this.areaLimit = options.limit;
		this.areaId = areaId;
		this.areaObject = $(areaId);
		this.counterObject = $(targetId);
		this.counterObject.innerHTML = this.areaLimit;
		this.addEvents();
		this.checkAreaSize();
	},

	addEvents: function(){
		this.areaObject.addEvent('keyup',this.checkAreaSize.bind(this));
	},

	checkAreaSize: function(){
		this.areaCounter = this.areaObject.getValue().length;
		if(this.areaCounter > this.areaLimit){
			this.counterObject.innerHTML = 0;
			this.areaObject.value = this.areaObject.getValue().substring(0,this.areaLimit);
			this.scrollToBottom();
		}else{
			this.counterObject.innerHTML = this.areaLimit - this.areaCounter;
		}
	},
	
	scrollToBottom: function() {
		new Fx.Scroll(this.areaId, {
			wait: true,
			duration: 10,
			offset: {'y': 25}
		}).toBottom();
	}

});

charCounter.implement(new Events);
charCounter.implement(new Options);