// AJAX blog editor functionality
document.observe('dom:loaded', function() {new Threader();});

var Threader = Class.create({
    // AJAX proxy
    url: '/threadproxy.php',

    initialize: function()
    {
        // show talkback editor
        $$('a.threadform').each(function(a)
        {
            a.observe('click', function(e)
            {
                Event.stop(e);
                $('cover').show();
                $('workspace').show();
                // compensate for scrolling in positioning workspace
                this.offset = Event.element(e).positionedOffset().top;
                var data = Event.element(e).name.split(':');
                new Ajax.Updater($('workspace-inner'), this.url, {
                    method: 'POST',
                    parameters: {system: 'blog', fid: data[0], parent: data[1], quote: 1, threadform: 1, lang: Event.element(e).lang},
                    onComplete: this.setup.bind(this)
                });
            }.bindAsEventListener(this))
        }, this);
        // moderate
        $$('a.moderate').each(function(a)
        {
            a.observe('click', function(e)
            {
                Event.stop(e);
                var data = Event.element(e).name.split(':');
                if (data[0] == 'delthread') {
                    new Ajax.Request(this.url, {
                        method: 'POST',
                        parameters: {system: 'blog', delthread: 1, tid: data[1]},
                        onComplete: function (r) {window.location = window.location.pathname; window.location.reload();}
                    });
                }
                else {
                    new Ajax.Request(this.url, {
                        method: 'POST',
                        parameters: {system: 'blog', delpost: 1, mid: data[1]},
                        onComplete: function (r) {$(data[1]).remove()}
                    });
                }
            }.bindAsEventListener(this))
        }, this);
    },

    // setup actions on the editor
    setup: function()
    {
        var height = $('form-outer').getHeight();
        $('workspace-inner').setStyle({marginTop: (this.offset - height) + 'px'});
        // close workspace
        $$('.closespace').each(function(s)
        {
            s.observe('click', function(e)
            {
                Event.stop(e);
                $('cover').hide();
                $('workspace').hide();
            }.bindAsEventListener(this));
        }, this);
        // add post
        $$('#threadpost').each(function(f)
        {
            f.observe('submit', function(e)
            {
                new Ajax.Request(this.url, {
                    method: 'POST',
                    parameters: Event.element(e).serialize(),
                    onComplete: function (r)
                    {
                        $('cover').hide();
                        $('workspace').hide();
                        if (r.responseJSON.mid)  // decide what to reload based on answer
                            window.location = window.location.pathname + '#' + r.responseJSON.mid;
                        else
                            window.location = window.location.pathname + '#talkback';
                        window.location.reload();
                    }.bind(this)
                })
                Event.stop(e);
            }.bindAsEventListener(this));
        }, this);
    }
});

