var Dialog = {
    show: function(url) {
        this._url = url;
        Curtain.draw(this.afterload.bind(this));
    },

    afterload: function() {
        if (null == $(this._id)) {
            this._generateMarkup();
        } else {
            this._appendReadyMessage();
        }
        this._position();
        this._request(this._url);
        this._addEvent();
    },

    hide: function() {
        this._removeEvent();
        $(this._id).remove();
        Curtain.hide();
    },

    _cleanUp: function() {
        $(this._id).update('');
    },

    _generateMarkup: function() {
        // 다이얼로그 박스
        var a = document.createElement('div');
        a.setAttribute('id', this._id);

        // 컨텐츠 영역
        var b = document.createElement('div');
        b.setAttribute('id', this._contents_id);
        a.appendChild(b);

        // 하단 닫기 영역
        var c = document.createElement('div');
        c.setAttribute('id', 'g_dialog_bottom');
        var d = document.createElement('button');
        d.setAttribute('id', 'g_dialog_btn');
        //d.innerHTML = '닫기';
        d.innerHTML = '<img src=' + WEBROOT + '/img/dialog_close_btn.gif border=0>';
        c.appendChild(d);
        a.appendChild(c);

        // 최종 문서에 추가하기
        document.body.appendChild(a);

        // 기본 메세지 추가
        this._appendReadyMessage();
    },

    _appendReadyMessage:function() {
        $(this._contents_id).update('<p class="ready">회원님의 요청을 처리중입니다.<br /> 잠시 기다려 주세요.</p>');
    },

    _request: function(url) { // Ajax Call을 한다.
        new Ajax.Updater($(this._contents_id), url, {onComplete:this._position.bind(this)});
    },

    _position: function() {
        var y = window.document.documentElement.scrollTop;
        var dim1 = document.body.getDimensions();
        var dim2 = $(this._id).getDimensions();
        var x = dim1.width / 2 - dim2.width / 2;
        with ($(this._id).style) {
            top = y + 125 + 'px';
            left = x + 'px';
        }
    },

    _addEvent: function() {
        Event.observe($('g_dialog_btn'), 'click', this.hide.bind(this));
        Event.observe(window, 'scroll', this._position.bind(this));
        Event.observe(window, 'resize', this._position.bind(this));
    },

    _removeEvent: function() {
        Event.unloadCache();
    },

    _id: 'g_dialog',
    _contents_id: 'g_dialog_contents',
    _url: null
}

