var TextAreaCounter = Class.create();
TextAreaCounter.prototype = {
	initialize : function(textArea, counterField, maxCount) {
		this.textArea = textArea;
		this.counterField = counterField;
		this.maxCount = maxCount;
		
		if (this.textArea == null) {
			return;
		}

		// bind event handler to this object
		this.checkCount.bindCheckCount = this.checkCount.bindAsEventListener(this);
		this.showCount.bindShowCount = this.showCount.bindAsEventListener(this);
		
		// set event handler
		Event.observe(this.textArea, "keyup", this.checkCount.bindCheckCount);
		Event.observe(this.textArea, "keyup", this.showCount.bindShowCount);
	},
	
	checkCount : function(event) {
		if (this.textArea == null) {
			return;
		}
		if(this.textArea.value.length > this.maxCount) {
			alert(this.maxCount + " 글자를 초과 입력할 수 없습니다.");
			this.textArea.value = this.textArea.value.substr(0, this.maxCount);
			this.showCount();
		}
	},
	
	showCount : function(event) {
		if (this.textArea == null) {
			return;
		}
		//this.counterField.innerHTML = this.textArea.value.length + " / " + this.maxCount + " 자";
		this.counterField.innerHTML = this.textArea.value.length;
	}
};