
var comment = {

    init: function()
    {
        if ($('comments'))
        {
            comment.cutLongContent();
            comment.highlight();
        }
    },


    insert: function(e)
    {                
        e.stop();
        if ($('commentEditor').comment.value != '')
        {
            comment.unhideRepty();

            var form = $('commentEditor');
            form.set('class','hiddenCommentEditor');
            form.inject($('commentRepty'), 'before');   
            
            new Request.JSON({
                url: '/ajax.php?do=commentInsert',
                onComplete: function(response)
                {   
                    if (response['restrict']) 
                    {                                  
                        user.restrictDialog(response, 'center');
                    }
                    else if (response)
                    {   
                        $('comments').set('html', response);
                        comment.init();
                    }
                }
            }).post({'object_id': $('comment_object_id').value, 'object_type': $('comment_object_type').value, 'comment_id': form.comment_id.value, 'comment': form.comment.value});
        }
    },


    repty: function(id, this_)
    {                  
        this_ = $(this_);
        
        if ($('commentEditor'))
        {
            $('commentEditor').set('class','');
            $('commentEditor').comment_id.value = id;
            $('commentEditor').comment.value = '';
            $('commentEditor').inject(this_, 'before'); 
            comment.unhideRepty();
            this_.set('class','hiddenRepty');
        }
        
        else
        {
            new Request.JSON({
                url: '/ajax.php?do=commentEditor',
                onComplete: function(response)
                {
                    if (response['restrict']) 
                    {                                  
                        user.restrictDialog(response, this_);
                    }
                    else if (response)
                    {   
                        this_.set('class','hiddenRepty');
                        this_.getParent().set('html', this_.getParent().get('html') + response);
                        $('commentEditor').addEvent('submit', comment.insert);
                    }
                }
            }).post({'comment_id': id});
        }
    },


    unhideRepty: function()
    {
        if ($('content').getElement('a.hiddenRepty')) $('content').getElement('a.hiddenRepty').set('class','');
    },


    close: function()
    {             
        $('commentEditor').set('class','hiddenCommentEditor');
        comment.unhideRepty();
    },


    cutLongContent: function()
    {
        var str = '';
        $('comments').getElements('div.middle').each(function(elem)
        {
            elem = elem.getElements('p')[1];
            str = elem.get('html');
            if (str.length > 340)
            {
                str = str.substring(0, 270) + "<span>... </span><a href='#' class='cut'>далее</a><span class='cut'>" + str.substring(270) + "</span>";
                elem.set('html', str);
                elem.getElement('a.cut').addEvent('click', comment.showCuttedContent);
            }
        });
    },


    showCuttedContent: function(e)
    {
        e.stop();
        if (this.getPrevious()) this.getPrevious().destroy();
        this.getNext().removeClass('cut');
        this.destroy();
    },
    
    
    highlight: function()
    {
        $('comments').getElements('span.vote b').each(function(elem)
        {
            var v = elem.get('text');
            var b = elem.getParent().getParent().getParent().getParent();
            if (v.contains('+') && v.replace('+','').toInt() >= 4) 
            {
                b.addClass('good');
            }
            else b.removeClass('good');
        });
    }

}

