
/* :::::::::: CommentsController :::::::::::::: */
var CommentsController = Class.create(); 
CommentsController.prototype =
{
    initialize: function( sUrl, nItemID, sCommentsBlockID, sInputID, sButtonID )
    {
        this.url = sUrl;
        this.id = nItemID;
        this.comments = $(sCommentsBlockID);
        this.input = $(sInputID);
        this.button = $(sButtonID);
        this.buttonText = this.button.innerHTML;
    },
    
    comment: function( )
    {                                  
        var comment = this.input.value;
        if(comment.toString().blank())
        {
            this.input.focus();
            return;
        }

        this.button.update('Подождите...');
        new Ajax.Request(this.url, {
            parameters:{'comment': comment, 'id':this.id}, 
            onSuccess: function (transport) 
            {
                try
                {
                    var response = eval("(" + transport.responseText + ")");
                    if(response.errno == 403) {
                        document.location = '/forbidden/';
                    }
                    else if (response.errno == 1) {
                        this.button.update(this.buttonText);
                        this.input.value = '';
                        this.comments.update( response.comments ); //update comments block
                    }
                    else {
                        this.button.update('ошибка');
                    }
                }
                catch(e){}
            }.bind(this),
             
            onFailure: function (transport) 
            {                                
                this.button.update('ошибка');
            }.bind(this)
        });
    }   
}
