Ajax.FJUpdater = Class.create();

Object.extend(Object.extend(Ajax.FJUpdater.prototype, Ajax.Request.prototype), {
    initialize: function(container, url, options, my_options) {
        this.container = container;
        this.transport = Ajax.getTransport();
        this.nextFunc = my_options.nextFunc;
        if (my_options.timeoutFunc) {
            this.timeoutFunc = my_options.timeoutFunc;
            this.timeout     = my_options.timeout;
        }
        this.errmsg = my_options.errmsg || '<p>読み込みに失敗しました。</p>';
        if (my_options.wait_msg_id) {
            this.wait_msg_id = my_options.wait_msg_id;
            this.wait_msg    = my_options.wait_msg || '<p>処理中です。</p>';
        };
        this.scroll_to_top = my_options.scroll_to_top;
        this.setOptions(options);

        var onComplete = this.options.onComplete || Prototype.emptyFunction;
        this.options.onComplete = (function(transport, param) {
            this.updateContent();
            onComplete(transport, param);
        }).bind(this);

        if (this.wait_msg_id) {
            $(this.wait_msg_id).style.display = '';
            $(this.wait_msg_id).innerHTML = this.wait_msg;
        }
        else {
            if (!$(this.container).innerHTML) {
                $(this.container).innerHTML = '<p>処理中です。</p>';
            }
        }
        this.request(url);
    },
 
    updateContent: function() {
        var receiver = this.container;
        var response;
        if (this.success()) {
            response = this.transport.responseText;
        }
        else {
            response = this.errmsg;
        }
        if (!this.options.evalScripts) response = response.stripScripts();

        if (receiver = $(receiver)) {
            if (this.options.insertion)
                new this.options.insertion(receiver, response);
            else
                receiver.update(response);
        }

        if (this.success()) {
            if (this.onComplete)
                setTimeout(this.onComplete.bind(this), 10);
        }
        if (this.scroll_to_top == 'page') {
            window.scrollTo(0, 0);
        }
        else if (this.scroll_to_top == 'element') {
            $(this.container).scrollTop = 0;
        }
        if (this.wait_msg_id) {
            $(this.wait_msg_id).style.display = 'none';
        }
        if (this.timeout) {
            setTimeout(this.timeoutFunc, this.timeout);
        }
        if (this.nextFunc) {
            this.nextFunc();
        }
    }
});

FJAjaxParts = function() {};

