/*
*
*   【使い方】
*   自動で、textareaのサイズを可変する。
*
*   【使用例】
*   <script type="text/javascript" src="autoTextareaFit.js"></script>
*
*/

(function(){

	function autoTextareaFit(){
		var obj = document.getElementsByTagName("textarea");

		for (var i = 0, il = obj.length; i < il; i++) {

			if(obj[i].scrollHeight > obj[i].offsetHeight){
				obj[i].style.height = obj[i].scrollHeight + 4 + 'px';
			}

			//focus
			obj[i].onfocus = function() {
				if(this.scrollHeight > this.offsetHeight){this.style.height = this.scrollHeight + 30 + 'px';}
			}
/*
			//blur
			obj[i].onblur = function() {
				this.style.height = this.scrollHeight + 4 + 'px';
			}
*/
			//keyup
			obj[i].onkeyup = function() {
				if(this.scrollHeight > this.offsetHeight){this.style.height = this.scrollHeight + 30 + 'px';}
			}

		}
	}

	function addEvent(elem,event,func){
		if(elem.addEventListener) {
			elem.addEventListener(event, func, false);
		}else if(elem.attachEvent) {
			elem.attachEvent("on" + event, func);
		}
	}

	addEvent(window,"load",autoTextareaFit);
})();

