﻿/** 
* Prototype base AutoRowText
* @authors 장성원  
* 
* @Usage :  (textarea, serverControl TextBox MultiLine)
*    new AutoRowText($("TextBox2"));
*    new AutoRowText($("TextBox3"), {
*        maxheight: 100,
*        minheight: 50,
*        lineheight: 15,
*        checkLength: 100  //글자수 체크 한글기준 단위 넣기 
*    });
* 
* @requires : 1.6 higher
*   - prototype <http://www.prototypejs.org>
*
*/
var AutoRowText = Class.create({
    initialize: function(textbox, options) {
        this.id = textbox;  //textbox엘리먼트
        this.options = Object.extend({
            maxheight: 200, //최대 세로 길이
            minheight: 50,   // 최소길이
            lineheight: 14,   //한줄높이
            checkLength: 500 //기본 한글 500자 영문 1000자           
        }, options);

        //글이 없으면 기본값
        if ($F(this.id).strip() == "") {
            //setMinheight를 this인자 쓰게끔 셋팅 및 호출
            this.setMinHeight.bind(this)();
        }
        else {//글이 있으면 계산해서 높이 조절
            this.setCheckHeight.bind(this)();
        }

        //사파리 브라우저가 아닐때만 동작한다. (사파리는 keyup동작에 버그로 이상동작을 한다)
        if (!Prototype.Browser.WebKit) {
            //일단 keyup일때만 동작하고press일때는 한동작 느린것으로보임
            Event.observe($(this.id), "keyup", this.countHeight.bind(this));
        }

    },
    countHeight: function(event) {
        var nCurrentHeight = $(this.id).scrollHeight; // Textarea 의 실제 높이 값을 반환(스크롤링 된 영역 포함)

        if (Prototype.Browser.Gecko || Prototype.Browser.Chrome) { // 파이어 폭스 or 크롬 일때만 다른 로직을 탐
            if (nCurrentHeight <= this.options.maxheight) { // 최대값을 넘지 않는 범위
                this.setMinHeight.bind(this)();
                $(this.id).setStyle({
                    height: $(this.id).scrollHeight + "px",
                    overflowY: "hidden"
                });
                return;
            }
        }
        else { // 떨림 방지용 Buffer
            nCurrentHeight += this.options.lineheight;
        }
        if (this.options.minheight >= nCurrentHeight) {
            this.setMinHeight.bind(this)();
        } else if (this.options.maxheight < nCurrentHeight) {
            $(this.id).setStyle({
                height: this.options.maxheight + "px",
                overflowY: "auto"
            });
        } else {
            $(this.id).setStyle({
                height: nCurrentHeight + "px",
                overflowY: "hidden"
            });
        }
        //글자 수 체크 로직 검사
        this.checkLength();
    },
    setMinHeight: function() {
        $(this.id).setStyle({
            height: this.options.minheight + "px",
            overflowY: "hidden"
        });
    },
    setCheckHeight: function() {
        var check = $F(this.id).split("\n");
        var checkheight = this.options.lineheight * (check.length + 1);

        if (checkheight < this.options.minheight)
            checkheight = this.options.minheight;
        $(this.id).setStyle({
            height: checkheight + "px",
            overflowY: "hidden"
        });
    },
    checkLength: function() {
        var tempByteLength = 0, cutByteLength = 0;
        for (var i = 0; i < $(this.id).value.length; i++) {
            if (escape($(this.id).value.charAt(i)).length > 4) {
                tempByteLength += 2;
            } else {
                tempByteLength++;
            }

            if (tempByteLength < this.options.checkLength) {
                cutByteLength++;
            }
        }
        if (tempByteLength > this.options.checkLength) {
            alert("메시지란에 허용 길이 이상의 글을 쓰셨습니다. \n 메시지란에는 한글 " + (this.options.checkLength / 2) + "자 영문 " + this.options.checkLength + "자 까지만 쓰실 수 있습니다");
            $(this.id).value = $(this.id).value.substring(0, (cutByteLength % 2 == 1) ? cutByteLength : cutByteLength + 1);
        }
    }
});