var extCmtTreeClass = new Class({
    Extends: lsCmtTreeClass,
    initialize: function(options) {
        this.parent(options);
        if ((typeof(bIsUserAdministrator) !== 'undefined') && bIsUserAdministrator) {
            this.isAdmin = true;
        }
    },

    clearCommentText: function(html) {
        if (html) {
            var n = html.indexOf("<!--[[AdminTextEditMark]]-->");
            if (n>=0) html=html.substring(0, n-1);
        }
        html=html.replace(new RegExp("<br>|<noindex>|</noindex>|<pre [^>]+>|</pre>|<span [^>]+>|</span>","g"),"");
        return html;
    },

    editComment: function(nCommentId, sCommentDate, sUserId) {
        var dCommentDate = sCommentDate;
        if ((typeof( sCommentDate ) == 'string')) {
            if (sCommentDate.substr(4, 1)=='-') {
                // Преобразование к "американскому" стандарту
                sCommentDate = sCommentDate.substr(5, 2)+'/'+sCommentDate.substr(8, 2)+'/'+sCommentDate.substr(0, 4)+sCommentDate.substr(10);
            }
            dCommentDate = new Date(sCommentDate);
        }
        var nSecondsExpired = 0;
        if ((typeof(this.commentTimers[nCommentId]) !== 'undefined') &&
            (this.commentTimers[nCommentId].seconds>0)) {
            nSecondsExpired = nSecondsForEdit - this.commentTimers[nCommentId].seconds;
        } else {
            var dCurrentDate = new Date();
            nSecondsExpired = (dCurrentDate.valueOf() - dCommentDate.valueOf()) / 1000;
        }
        if (!this.isAdmin) {
            if ((nSecondsForEdit>0) && (nSecondsForEdit < nSecondsExpired)) {
                msgErrorBox.alert('Error','You cannot edit this comment');
                return;
            }
        }

        var idComment = nCommentId;

        divCurrentForm=$('reply_'+this.iCurrentShowFormComment);
        divNextForm=$('reply_'+idComment);

        var slideCurrentForm = new Fx.Slide(divCurrentForm);
        var slideNextForm = new Fx.Slide(divNextForm);

        $('comment_preview_'+this.iCurrentShowFormComment).set('html','').setStyle('display','none');
        if (this.iCurrentShowFormComment==idComment) {
            slideCurrentForm.toggle();
        } else {
            slideCurrentForm.slideOut();
            divNextForm.set('html',divCurrentForm.get('html'));
            divCurrentForm.set('html','');
            divNextForm.setStyle('display','block');
            slideNextForm.hide();

            slideNextForm.slideIn();
        }
        $('form_comment_text').focus();
        var html=$('comment_text_'+idComment).get('html');
        html = this.clearCommentText(html);

        $('form_comment_text').setProperty('value', html);
        $('form_comment_reply').setProperty('value',idComment);
        $('form_comment_cmd').setProperty('value', 'edit');
        this.iCurrentShowFormComment=idComment;
    },

    addComment: function(formObj, topicId) {
        if ($('form_comment_cmd').getProperty('value')=='edit') {
            // редактирование комментария
            var thisObj=this;
            formObj=$(formObj);
            JsHttpRequest.query(
                DIR_WEB_ROOT+'/include/ajax/commentEdit.php',
                {
                    params: formObj
                },
                function(result, errors) {
                    if (!result) {
                        thisObj.enableFormComment();
                        msgErrorBox.alert('Error','Please try again later');
                    }
                    if (result.bStateError) {
                        thisObj.enableFormComment();
                        msgErrorBox.alert(result.sMsgTitle,result.sMsg);
                    } else {
                        $('form_comment_text').disabled=true;
                        $('comment_text_'+result.sCommentId).set('html', result.sCommentText);
                        //thisObj.responseNewComment(topicId,$('update-comments'),result.sCommentId,true);
                        thisObj.hideCommentForm(result.sCommentId);
                        $('form_comment_text').setProperty('value', '');
                        $('form_comment_cmd').setProperty('value', 'replay');
                    }
                },
                true
                );
            $('form_comment_text').addClass('loader');
        } else {
            // добавление нового комментария
            this.parent(formObj, topicId);
        }
    },
    injectComment: function(idCommentParent,idComment,sHtml) {
        this.parent(idCommentParent,idComment,sHtml);

        var thisObj = this;
        var newComentId = idComment.toString();
        var newCommentDate = (new Date()).toString();
        var newCommentContent = $('comment_content_id_'+newComentId);
        if (newCommentContent) {
            newCommentContent.addEvent('click', function(){
                thisObj.editComment(newComentId, newCommentDate, 0);
            });
        }
        if (!this.isAdmin && (nSecondsForEdit > 0)) {
            // устанавливаем таймер для отсчета времени, когда можно редактировать
            var spanTimer = $('cmt_edit_time_'+newComentId);
            if (spanTimer) {
                thisObj.commentTimers[newComentId] = {
                    'comment_id': newComentId,
                    'span': spanTimer,
                    'seconds': nSecondsForEdit,
                    'func': function() {
                        if ((--this.seconds) > 0) {
                            this.span.set('text', ' ('+this.seconds+' sec)');
                        } else {
                            $clear(this.func);
                            this.span.hide();
                            var li=$('cmt_edit_'+this.comment_id);
                            if (li) li.hide();
                        }
                    }
                };
                thisObj.commentTimers[newComentId].func.periodical(1000, thisObj.commentTimers[newComentId]);
            }
        }
        $('form_comment_text').setProperty('value', '');
        $('form_comment_cmd').setProperty('value', 'replay');
    },
    commentTimers: {},
    isAdmin: false
});


