var NewsComments = Class.create({
    initialize: function(conf) 
    {
        this.formId   = conf.form.id;
        this.showForm = conf.form.show;
        
        this.commentsId = conf.comments.id;
        this.showComments = conf.comments.show;
        this.toggleCommentsUrl = conf.comments.toggleUrl;
        
        Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
    },

    onLoad: function()
    {
        var formTrigger = $(this.formId + '-trigger');
        var commentTrigger = $(this.commentsId + '-trigger');

        formTrigger.observe('click', this.onShowForm.bindAsEventListener(this));
        commentTrigger.observe('click', this.toggleComments.bindAsEventListener(this));
        
        formTrigger.style.display = 'inline';
        commentTrigger.style.cursor = 'pointer';
        $(this.formId).style.display = (this.showForm)? 'block' : 'none';
        $(this.commentsId).style.display = (this.showComments)? 'block' : 'none';
    },
    
    onShowForm: function(event)
    {
        var form = $(this.formId);
        form.style.display = (form.style.display == 'block')? 'none' : 'block';
    },
    
    onToggle: function(transport)
    {
        if (!transport.responseText.isJSON()) {
            return;
        }
        
        var result = transport.responseText.evalJSON();
        if (!result) {
            return;
        }
        
        var commentList = $(this.commentsId);
        commentList.style.display = (result.show)? 'block' : 'none';
    },
    
    toggleComments: function(event)
    {
        var req = new Ajax.Request(this.toggleCommentsUrl, {
            method: 'get',
            onSuccess: this.onToggle.bind(this)
        });
    }
});