var Comments = {
    nArtId : '',

    init:function()
    {
        Comments.nArtId = $('#ArtID').val();

//        $('.comimgbutton').each(function() {
//            $(this).mouseover(function(){this.borderColor = $(this).css('border-color');$(this).css({'border-color':'#b81d00'});});
//            $(this).mouseout(function(){$(this).css({'background-image':'url(../images/editor/buttonbg.gif)', 'border-color':this.borderColor});});
//            $(this).mousedown(function(){$(this).css({'background-image':'url(../images/editor/buttonbgover.gif)', 'border-color':'#999999'});});
//            $(this).mouseup(function(){$(this).css({'background-image':'url(../images/editor/buttonbg.gif)', 'border-color':'#b81d00'});});
//        });

        $('div.comitem').each(function(){
                $(this).hover(
                        function() {$(this).find('div.reply').css('opacity',100);},
                        function() {$(this).find('div.reply').css('opacity',0);}
                );
        });

        $('.flagComment').each(function(){
                Comments.flag(this);
        });

        $('.replyComment').each(function(){
                Comments.reply(this);
        });

        $('#commentfield').click(function(){
            var strCommentField = $('#commentfield').val();
            
            if(strCommentField.match(/Напишете вашия коментар/gi))
                {
                    $('#commentfield').val('');
                }
        });
        
        var bSubmit = true;
        
        $('#PublishComment').click(function(event){
            event.preventDefault();
            
            if(bSubmit)
            {
                bSubmit = false;

                var Content = $('#commentfield').val();
                var Attachment = $('#Attachment').val();

                Content = (Content.match(/Напишете вашия коментар/gi)) ? '' : Content;
                
                if(Content != '' || Attachment != '')
                {
                    //alert("Content="+Content+"&Attachment="+Attachment+"&ArticleID="+Comments.nArtId);

                    $.ajax({
                       cache: false,
                       type: "POST",
                       url: strRootPath+"comments/submitform.html",
                       data: "Content="+Content+"&Attachment="+Attachment+"&ArticleID="+Comments.nArtId,
                       success: function(data){
                            $('#removeComments').html('');
                            $('#commentslist').html(data);
                            $('#commentfield').val('Напишете вашия коментар...');
                            $('#Attachment').val('');
                            CRate.init();
                            bSubmit = true;
                       }
                     });
                }
            }
        });
        
        $("#uploadifycomment").uploadify({
            'uploader'       : strRootPath+'flash/uploadifycomment.swf',
            'wmode'          : 'transparent',
            'script'         : strRootPath+'comments/insertimage/'+Comments.nArtId+'.html',
            'cancelImg'      : strRootPath+'images/cancel.png',
            'fileDataName'   : 'FilePath',
            'fileDesc'       : 'Image Files',
            'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
            'queueID'        : 'fileQueue',
            'width'          :  102,
            'height'         :  26,
            'auto'           : true,
            'multi'          : false,
            'onComplete'     : function(event, queueID, fileObj, response, data) {
                if(response) {
                    $('#Attachment').val(response);
                }
            }
        });

        CRate.init();
    },

    sort:function(nArticleID, strFlag)
    {
        var strParams = 'Param=' + escape(strFlag);
        $.ajax({
            cache: false,
            type: "POST",
            url: strRootPath+'comments/sort/'+nArticleID+'.html',
            data: strParams,
            success: function(data){
                if(data != '')
                {
                    $('#commentslist').html(data);
                    $('.sortcomment a').removeClass('comactive');
                    CRate.init();
                }
            }
        });
    },

    deleteComment: function(strURL,This)
    {
        $.ajax({
           cache: false,
           type: "POST",
           url: strURL,
           success: function(data){
                if(data==1)
                    {
                        $(This).parent().parent().remove();
                    }
           }
        });
    },

    flag:function(This)
    {
        $(This).click(function(e){
            e.preventDefault();
            var flagParams = "ArticleID="+$(This).parent().attr('rel')+"&CommentID="+$(This).attr('rel');
            
            $.ajax({
               cache: false,
               type: "POST",
               url: strRootPath+"comments/flag.html",
               data: flagParams,
               success: function(data){
                   if(data == 1)
                       {
                           alert('Благодарим Ви за сигнализирането!');
                       }
               }
            });
        });
    },

    reply:function(This)
    {
        $(This).click(function(e){
            e.preventDefault();

            $('.replyCommentForm').html('');

            var flagParams = "ArticleID="+$(This).parent().attr('rel')+"&CommentID="+$(This).attr('rel');
            var destCont = $(This).parent().parent().parent().find('div.replyCommentForm');

            $.ajax({
               cache: false,
               type: "POST",
               url: strRootPath+"comments/reply.html",
               data: flagParams,
               success: function(data){
                    $(destCont).html(data);
                    
                    $("#uploadifyreplycomment").uploadify({
                        'uploader'       : strRootPath+'flash/uploadifycomment.swf',
                        'wmode'          : 'transparent',
                        'script'         : strRootPath+'comments/insertimage/'+Comments.nArtId+'.html',
                        'cancelImg'      : strRootPath+'images/cancel.png',
                        'fileDataName'   : 'FilePath',
                        'fileDesc'       : 'Image Files',
                        'fileExt'        : '*.jpg;*.jpeg;*.png;*.gif',
                        'queueID'        : 'fileQueueReply',
                        'width'          :  102,
                        'height'         :  26,
                        'auto'           : true,
                        'multi'          : false,
                        'onComplete'     : function(event, queueID, fileObj, response, data) {
                            if(response) {
                                $('#ReplyAttachment').val(response);
                            }
                        }
                    });
                    
                    $('#ReplyCommentfield').unbind('click').click(function(){
                        var strCommentField = $('#ReplyCommentfield').val();

                        if(strCommentField.match(/Напишете вашия коментар/gi))
                            {
                                $('#ReplyCommentfield').val('');
                            }
                    });

                    $('#PublishReply').click(function(event){
                        event.preventDefault();

                        var Content = $('#ReplyCommentfield').val();
                        var Attachment = $('#ReplyAttachment').val();
                        var ParentID = $('#ParentID').val();

                        Content = (Content.match(/Напишете вашия коментар/gi)) ? '' : Content;
                        
                        if(Content != '' || Attachment != '')
                        {
                            //alert("Content="+Content+"&Attachment="+Attachment+"&ArticleID="+Comments.nArtId+"&ParentID="+ParentID);

                            $.ajax({
                               cache: false,
                               type: "POST",
                               url: strRootPath+"comments/submitform.html",
                               data: "Content="+Content+"&Attachment="+Attachment+"&ArticleID="+Comments.nArtId+"&ParentID="+ParentID,
                               success: function(data){
                                    Comments.reloadComments();
                               }
                             });
                        }
                    });
               }
            });
        });
    },

    reloadComments:function()
    {
        $.ajax({
           cache:false,
           url: strRootPath+'comments/list/'+Comments.nArtId+'.html',
           success: function (data) {
               $('#commentslist').html(data);
               CRate.init();
           }
        });
    }
};
