﻿$('#AjaxComment').click(function() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Features/Comments/Ajax/CommentsService.asmx/PostComment",
        data: "{'pageId':'" + $('#CommentPageRef').attr("value") + "','comment':'" + $('#commentText').html() + "'}",
        dataType: "json",
        success: function(msg) {
            $('.commentsList').append(
                    '<li id="comment_' + msg.d.pid + '">\
                        <div class="commentAdmin">\
                            <a rel="pid_' + msg.d.pid + '" class="AjaxShowComment" href="javascript:;">\
                                <img src="/templates/images/fff_icons/add.png" alt="Visa" />\
                            </a>\
                            <a rel="pid_' + msg.d.pid + '" class="AjaxDeleteComment" href="javascript:;">\
                                <img src="/templates/images/fff_icons/delete.png" alt="Radera" />\
                            </a>\
                            <a rel="pid_' + msg.d.pid + '" class="AjaxTrashComment" href="javascript:;">\
                                <img src="/templates/images/fff_icons/bin_closed.png" alt="Flytta till papperskorgen" />\
                            </a>\
                        </div>\
                        <img class="avatar" src="' + msg.d.avatar + '" />\
                        <div class="content">\
                            <span class="created">\
                                <span class="creator">' + msg.d.username + '</span>\
                                <span class="de-em">skrev</span>\
                                ' + msg.d.date + '\
                            </span>\
                                ' + msg.d.message + '\
                        </div>\
                        <div class="clear"></div>\
                    </li>'
                );
            $('#commentText').html("");
        },
        error: function(xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });
});

$('.AjaxDeleteComment').live('click', function(e) {
    var pageId = this.rel.substring(4);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Features/Comments/Ajax/CommentsService.asmx/HideComment",
        data: "{'pageId':'" + pageId + "'}",
        dataType: "json",
        success: function(msg) {
            if (msg.d === true) {
                $('#comment_' + pageId).find('p').text('Meddelanded har raderats');
            }
            else {
                alert('Ett fel uppstod.');
            }
        },
        error: function(xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });
    return false;
});

$('.AjaxShowComment').live('click', function(e) {
    var pageId = this.rel.substring(4);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Features/Comments/Ajax/CommentsService.asmx/ShowComment",
        data: "{'pageId':'" + pageId + "'}",
        dataType: "json",
        success: function(msg) {
            $('#comment_' + pageId).find('p').html(msg.d.message);
        },
        error: function(xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });
    return false;
});

$('.AjaxTrashComment').live('click', function(e) {
    var pageId = this.rel.substring(4);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Features/Comments/Ajax/CommentsService.asmx/DeleteComment",
        data: "{'pageId':'" + pageId + "'}",
        dataType: "json",
        success: function(msg) {
            if (msg.d === true) {
                $('#comment_' + pageId).remove();
            }
            else {
                alert('Ett fel uppstod.');
            }
        },
        error: function(xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });
    return false;
});
