﻿/// <reference path="jquery.confirm.js" />
/// <reference path="jquery.dialog.js" />
/// <reference path="jquery.form.js" />
(function ($) {
    var urls = null;
    $.fn.initTopicPage = function(options){
        urls = options;
    };
    $(function () {
        $('#DeleteTopicButton').click(function (e) {
            e.preventDefault();
            var id = $(this).closest('li').find('input[type=hidden]').attr('value');
            $('#DeletePostTopicDialog').dialog({
                open: true,
                title: 'حذف تاپیک',
                afterDisplay: function () {
                    $('#TopicPostDeleteForm').attr('action', urls.deleteTopic);
                    $('#IDToDelete').attr('value', id);
                }
            });
        });
        $('.commentOnPost').click(function (e) {
            e.preventDefault();
            var id = $(this).closest('li').find('input[type=hidden]').attr('value');
            $('#CommentOnPostDialog').dialog({
                title: 'ارسال نظر روی پست',
                open: true,
                width: 500,
                afterDisplay: function () {
                    $('#AddPostCommentForm').attr('action', urls.addPostComment);
                    $('#PostIDToComment').attr('value', id);
                    $('#CommentOnPostTextArea').focus();
                }
            });
        });
        $('.editPostComment').click(function (e) {
            e.preventDefault();
            var id = $(this).closest('li').find('input[type=hidden]').attr('value');
            $.ajax({
                url: urls.getCommentText,
                type: 'post',
                data: { id: id },
                success: function (result) {
                    $('#CommentOnPostDialog').dialog({
                        title: 'ارسال نظر روی پست',
                        open: true,
                        width: 500,
                        afterDisplay: function () {
                            $('#AddPostCommentForm').attr('action', urls.editPostComment);
                            $('#CommentOnPostTextArea').html(result);
                            $('#PostIDToComment').attr('value', id);
                            $('#CommentOnPostTextArea').focus();
                        }
                    });
                },
                error: function () {
                    alert('امکان انجام این عملیات وجود ندارد');
                }
            });
        });
        $('.deletePostComment').confirm({
            title: 'حذف نظر',
            message: 'آیا حذف نظر را تائید می کنید؟',
            onAccept: function (e) {
                e.closest('form').submit();
            }
        });
        $('.deletePostPermanent').confirm({
            title: 'حذف پست',
            message: 'آیا حذف دائم پست را تائید می کنید؟',
            onAccept: function (e) {
                e.closest('form').submit();
            }
        });
        $('.deleteForumPost').click(function (e) {
            e.preventDefault();
            var id = 0;
            $(this).closest('form').find('input[type=hidden]').each(function () {
                if ($(this).attr('name') === 'id')
                    id = $(this).attr('value');
            });
            $('#DeletePostTopicDialog').dialog({
                open: true,
                title: 'حذف پست',
                afterDisplay: function () {
                    $('#TopicPostDeleteForm').attr('action', urls.deletePost);
                    $('#IDToDelete').attr('value', id);
                }
            });
        });
        $('#AddAttachementForm').ajaxForm({
            iframe: true,
            dataType: 'json',
            beforeSubmit: function () {
                $('#AttachementFileInput').val('');
                $('#AttachementFileInput').attr('disabled', 'disabled');
                $('.attachementAjaxIcon').show();
            },
            success: function (result) {
                $('#AttachementFileInput').removeAttr('disabled');
                $('.attachementAjaxIcon').hide();
                if (result.invalidFile === true || result === true)
                    alert('امکان افزودن ضمیممه وجود ندارد.');
                else {
                    $('#DeletedAttachements').find('input[type=hidden]').each(function () {
                        if ($(this).val() === result.fileId)
                            $(this).remove();
                    });
                    var newAttachement = $('<div class="newAttachement" style="margin-right: 20px; margin-bottom: 4px; padding: 4px;"></div>');
                    newAttachement.append('<input type="hidden" name="NewAttachementIds" class="fileId" value="' + result.fileId + '" />');
                    newAttachement.append('<a href="#" style="margin-left: 4px;" class="removeAttachement iconLink_15 delete_icon"></a>');
                    newAttachement.append('<label>' + result.fileName + ' - سایز: ' + result.fileSize + '</label>');
                    $('#Attachements').append(newAttachement);
                    document.getElementById('AttachementFileInput').setAttribute('type', 'input');
                    document.getElementById('AttachementFileInput').setAttribute('type', 'file');
                }
            },
            error: function () {
                $('#AttachementFileInput').removeAttr('disabled');
                $('.attachementAjaxIcon').hide();
            }
        });
        $('.removeAttachement').live('click', function (e) {
            e.preventDefault();
            var closest = $(this).closest('.newAttachement');
            var fileId = closest.find('.fileId').val();
            $('#DeletedAttachements').append('<input type="hidden" name="DeletedAttachementIds" value="' + fileId + '" />');
            closest.remove();
        });
        $('#AttachementFileInput').change(function () {
            if ($(this).val() != '')
                $('#AddAttachementForm').submit();
        });
        $('.recoverContent').confirm({
            title: 'بازگرداندن مطلب',
            message: 'آیا بازگرداندن مطلب را تائید می کنید؟',
            onAccept: function (e) {
                e.closest('form').submit();
            }
        });
        $('.deletePermanentContent').confirm({
            title: 'حذف مطلب',
            message: 'آیا حذف نهایی مطلب را تائید می کنید؟',
            onAccept: function (e) {
                e.closest('form').submit();
            }
        });
    });
    $('.changeContentLike').live('click', function (e) {
        e.preventDefault();
        var obj = $(this);
        var href = obj.attr('href');
        obj.removeAttr('href');
        obj.addClass('disabledLink');
        $.ajax({
            url: href,
            type: 'get',
            cache: false,
            success: function (result) {
                if (result != null && result.accessDenied) {
                    alert('امکان انجام درخواست شما وجود ندارد.');
                }
                else {
                    obj.removeClass('disabledLink');
                    if (result === false) {
                        obj.attr('href', href);
                        return;
                    }
                    obj.attr('href', result.url);
                    obj.parent().find('.contentLikesCount').html(result.likes);
                    if (result.type === 'like')
                        obj.html('نپسندیدم');
                    else
                        obj.html('پسندیدم');
                }
            },
            error: function () {
                obj.removeClass('disabledLink');
                obj.attr('href', href);
            }
        });
    });
    $('.changeContentBadReport').live('click', function (e) {
        e.preventDefault();
        var obj = $(this);
        var href = obj.attr('href');
        obj.removeAttr('href');
        obj.addClass('disabledLink');
        $.ajax({
            url: href,
            type: 'get',
            cache: false,
            success: function (result) {
                if (result != null && result.accessDenied) {
                    alert('امکان انجام درخواست شما وجود ندارد.');
                }
                else {
                    obj.removeClass('disabledLink');
                    if (result === false) {
                        obj.attr('href', href);
                        return;
                    }
                    obj.attr('href', result.url);
                    obj.parent().find('.contentLikesCount').html(result.likes);
                    if (result.type === 'add')
                        obj.html('حذف گزارش');
                    else
                        obj.html('گزارش تخلف');
                }
            },
            error: function () {
                obj.removeClass('disabledLink');
                obj.attr('href', href);
            }
        });
    });
    $.fn.parseComment = function () {
        var comment = $(this);
        comment.find('.acceptCommentForm').ajaxForm({
            success: function (result) {
                if (result != null && result.accessDenied) {
                    alert('امکان انجام درخواست شما وجود ندارد.');
                }
                else {
                    var object = $(result);
                    var id = object.attr('id');
                    var oldObject = $('#' + id);
                    object.insertBefore(oldObject);
                    oldObject.remove();
                }
            }
        });
        comment.find('.deleteComment').click(function (e) {
            e.preventDefault();
            var commentId = $(this).closest('.commentHeader').find('#commentId').val();
            var commentElement = $(this).parent().parent().parent().parent();
            $('#CommentToDeleteID').val(commentId);
            $('#CommentDelete').dialog({
                open: true,
                width: 350,
                afterDisplay: function () {
                    $('#DeleteCommentForm').ajaxForm({
                        success: function (result) {
                            if (result != null && result.accessDenied) {
                                alert('امکان انجام درخواست شما وجود ندارد.');
                            } else {
                                var newElement = $(result);
                                $('#CommentDelete').dialogclose();
                                newElement.insertAfter(commentElement);
                                newElement.hide();
                                commentElement.remove();
                                newElement.fadeIn(200);
                                newElement.parseComment();
                            }
                        }
                    });
                }
            });
        });
        comment.find('.deleteCommentPermanentForm').ajaxForm({
            success: function (result) {
                if (result != null && result.accessDenied) {
                    alert('امکان انجام درخواست شما وجود ندارد.');
                } else {
                    $('#' + result).fadeOut(200, function () {
                        $(this).remove();
                        if ($('.commentRow').length == 0)
                            $('#NoComments').show();
                    });
                }
            }
        });
        comment.find('.recoverCommentForm').ajaxForm({
            success: function (result) {
                if (result != null && result.accessDenied) {
                    alert('امکان انجام درخواست شما وجود ندارد.');
                } else {
                    var newElement = $(result);
                    var oldElement = $('#' + newElement.attr('id'));
                    newElement.insertBefore(oldElement);
                    newElement.hide();
                    oldElement.remove();
                    newElement.fadeIn(200);
                    newElement.parseComment();
                }
            }
        });
        comment.find('.deleteCommentPermanent').confirm({
            title: 'حذف دائم نظر',
            message: 'آیا حذف دائم این نظر را تائید می کنید؟',
            onAccept: function (e) {
                e.closest('form').submit();
            }
        });
        comment.find('.editComment').click(function (e) {
            e.preventDefault();
            var commentId = $(this).parent().parent().find('#commentId').val();
            var url = $(this).attr('data-url');
            $('#CommentEditor').dialog({
                open: true,
                width: 650,
                afterDisplay: function () {
                    $.ajax({
                        url: url,
                        type: 'get',
                        cache: false,
                        data: { id: commentId },
                        success: function (result) {
                            if (result != null && result.accessDenied) {
                                alert('امکان انجام درخواست شما وجود ندارد.');
                            }
                            else {
                                $('#CommentEditorContent').html(result);
                                $('#CommentEditorTextArea').focus();
                            }
                        }
                    });
                }
            });
        });
    };
})(jQuery);

