AjaxGuestBook = function(setting) {
    this.init(setting);
}

AjaxGuestBook.prototype = {
    elem_id         : null,
    wait_msg_id     : null,
    wait_msg        : null,
    main_formname   : null,
    main_form_head_id : null,
    delete_formname : null,
    main_url        : null,
    edit_url        : null,
    errmsg          : null,
    tid             : null,
    gid             : null,

    init : function(setting) {
        this.elem_id         = setting.elem_id ? setting.elem_id
                                               : 'ajax_guestbook';
        this.wait_msg_id     = setting.msg_id ? setting.msg_id
                                              : 'ajax_guestbook_msg';
        $(this.wait_msg_id).style.display = 'none';
        this.wait_msg        = setting.wait_msg || '<p>処理中です。</p>';
        this.main_formname   = setting.main_formname ? setting.main_formname
                                                     : 'guestbook_form';
        this.main_form_head_id = setting.main_form_head_id ?
                                 setting.main_form_head_id :
                                 'guestbook_head';
        this.delete_formname = setting.delete_formname ?
                               setting.delete_formname :
                               'delete_form';
        this.main_url        = setting.main_url;
        this.edit_url        = setting.edit_url;
        this.scroll_to_top   = setting.scroll_to_top ?
                               setting.scroll_to_top :
                               'element';
        this.errmsg          = setting.errmsg || '<p>読み込みに失敗しました。</p>';
        if (setting.tid) {
           this.tid = setting.tid;
        }
        if (setting.gid) {
           this.gid = setting.gid;
        }
    },

    show : function(action, mode, params_h, option) {
        var url, formname, _nextFunc, _timeout, _timeoutFunc;

        if (action == 'main') {
            url = this.main_url;
            formname = this.main_formname;
        }
        else if (action == 'delete') {
            url = this.edit_url;
            formname = this.delete_formname;
        }
        if (option) {
            if (option.nextFunc) _nextFunc = option.nextFunc;
            if (option.timeout) {
                _timeout = option.timeout;
                _timeoutFunc = option.timeoutFunc;
            }
        }

        if (!params_h) {
            params_h = new Object();
        }
        if (this.gid) {
            params_h.gid = this.gid;
        }
        if (this.tid) {
            params_h.tid = this.tid;
        }
        var params_a = Array();
        for (var property in params_h) {
            if (typeof(params_h[property]) != 'function') {
                params_a.push(property + '=' + encodeURI(params_h[property]));
            }
        }
        var params = params_a.join('&');
        if (mode == 'get') {
            new Ajax.FJUpdater(this.elem_id, 
                               url,
                               { method : 'get',
                                 parameters : params,
                                 requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'] }, 
                               { nextFunc : _nextFunc,
                                 errmsg : this.errmsg,
                                 wait_msg_id : this.wait_msg_id,
                                 wait_msg : this.wait_msg,
                                 timeout : _timeout,
                                 timeoutFunc : _timeoutFunc,
                                 scroll_to_top : this.scroll_to_top });
        }
        else if (mode == 'post') {
            if (params) {
                url += "?" + params;
            }
            new Ajax.FJUpdater(this.elem_id, 
                               url,
                               { method : 'post',
                                 parameters : Form.serialize(formname) }, 
                               { nextFunc : _nextFunc,
                                 errmsg : this.errMsg,
                                 wait_msg_id : this.wait_msg_id,
                                 wait_msg : this.wait_msg,
                                 timeout : _timeout,
                                 timeoutFunc : _timeoutFunc,
                                 scroll_to_top : this.scroll_to_top });
        }
    },

    view_page : function(v_page, v_params, v_option) {
        if (!v_params) {
            v_params = new Object();
        }
        if (v_page != 1) {
            v_params.page = v_page;
        }
        this.show('main', 'get', v_params, v_option);
        return false;
    },

    preview_comment : function(v_params, v_option) {
        $('preview_comment').value = 1;
        $('post_btn').disabled = 1;
        $('preview_btn').disabled = 1;
        this.show('main', 'post', v_params, v_option);
        return false;
    },

    post_comment : function(v_params, v_option) {
        $('post_comment').value = 1;
        $('post_btn').disabled = 1;
        $('preview_btn').disabled = 1;
        this.show('main', 'post', v_params, v_option);
        return false;
    },

    delete_login : function(v_params, v_option) {
        $('delete_btn').disabled = 1;
        this.show('delete', 'post', v_params, v_option);
        return false;
    },

    delete_comment : function(v_params, v_option) {
        var ct;
        $('delete_btn').disabled = 1;

        ct = this.check_delete_count();
        if (ct > 0) {
            if (confirm(ct + '件のコメントを削除します')) {
                this.show('delete', 'post', v_params, v_option);
            }
        }
        else {
            alert('削除する投稿にチェックを入れてください');
        }
        $('delete_btn').disabled = 0;
        return false;
    },

    delete_logout : function(v_params, v_option) {
        if (!v_params) {
            v_params = new Object();
        }
        v_params.logout = 1;
        this.show('delete', 'get', v_params, v_option);
        return false;
    },

    delete_view_page : function(v_page, v_params, v_option) {
        var ct;

        ct = this.check_delete_count();
        if (ct > 0) {
           if (!confirm('チェックされているコメントは削除せずに、ページを移動します')) {
               return false;
           }
        }
        if (!v_params) {
            v_params = new Object();
        }
        if (v_page && v_page != 1) {
            v_params.page = v_page;
        }
        this.show('delete', 'get', v_params, v_option);
        return false;
    },

    check_delete_count : function() {
        var ct = 0, i, delete_form;

        if (document.forms[this.delete_formname]) {
            delete_form = document.forms[this.delete_formname];
            for (i = 0; i < delete_form.length; i++) {
                if (delete_form.elements[i].type == 'checkbox' &&
                    delete_form.elements[i].id.match(/post_(\d)*/) &&
                    delete_form.elements[i].checked) {
                    ct++;
                }
            }
        }
        return ct;
    },

    scroll_to_comment_form : function() {
        if (this.scroll_to_top == 'element') {
            $(this.elem_id).scrollTop = $(this.main_form_head_id).offsetTop;
        }
        else if (this.scroll_to_top == 'page') {
            location.href = "#" + this.main_form_head_id;
        }
        return false;
    }
};

