﻿/*/// <reference path="../jquery-1.7.1.js" />
/// <reference path="../jquery.alerts.js" />
*/
if (typeof $mvc === 'undefined') {
    $mvc = {};
}


/************************************************************************************************************
 * Articles
 ***********************************************************************************************************/


    //删除文章

$(function () {
    $(".remove-article").click(function () {
        var articleId = $(this).attr("meta:id");
        jConfirm("该文章及其所有评论将被删除！\n确定删除吗？", "删除", function (ok) {
            if (ok) {
                $mvc.Ajax.RemoveArticle({ articleId: articleId })
                    .success(function (data) {
                        $("#article-" + data.articleId).fadeOut("slow", function () { $(this).remove(); });
                    });
            }
        });
    });
});

/************************************************************************************************
*类别
************************************************************************************************/
//删除类别
$(function () {
    $('.remove-category').click(function () {
        var id = $(this).attr("meta:id");
        jConfirm('确定删除吗？', '该类别将被删除！', function (ok) {
            if (ok) {
                $mvc.Ajax.RemoveCategory({ categoryId: id })
                .success(function (data) {
                    $("#category-" + data.commentId).next(".admin").fadeOut("slow", function () { $(this).remove(); });
                    $("#category-" + data.commentId).fadeOut("slow", function () { $(this).remove(); });
                });
            }
        });
    });
});

/************************************************************************************************************
* Comments
***********************************************************************************************************/

//创建评论
$(function () {
    $('#create-comment').click(function () {
        $mvc.Ajax.CreateComment({ articleId: $("#articleId").val(), name: $("#comment-name").val(), email: $("#comment-email").val(), body: $("#comment-body").val() })
        .success(function (data) {
            CreateCommentSuccess(data);
        });
    });
});

//成功创建评论
function CreateCommentSuccess(data) 
{
	$(".new-comment").removeClass("new-comment").show("normal");

	var commentText = "";
	commentText += "<div id=\"comment-" + data.commentId + "\" class=\"comment new-comment\">";
	commentText += "<div class=\"comment-header\">由 " + data.name + " 于 " + data.date + " 发表</div>";
	commentText += "<blockquote>" + data.body + "</blockquote>";
	commentText += "</div>";
    
	var comment = $(commentText);

	// clear the body box
	$("#comment-body").val("");

	// add the new comment to the other comments
	comment
		.hide()
		.appendTo("#article-comments")
		.slideDown("slow");
}

//修改评论
$(function () {
    $('.edit-comment').click(function () {
        var id = $(this).attr("meta:id"),
		comment = $("#comment-" + id),
		bodyText = comment.find(".body").text(),
		nameText = comment.find(".name").text();

        // hide all the childrend	
        comment.children().hide();

        var commentText = "";
        commentText += "<form><div class=\"comment-header field\"><label for=\"name-" + id + "\">作者</label><br/><input type=\"text\" id=\"name-" + id + "\" class=\"edit-name\" value=\"" + nameText + "\" /></div>";
        commentText += "<div class=\"field\"><label for=\"body-" + id + "\">内容</label><br/><textarea class=\"edit-body\" id=\"body-" + id + "\">" + bodyText + "</textarea><br/><button type=\"button\" class=\"update\" meta:id=\"" + id + "\">修改</button>&nbsp;<button type=\"button\" class=\"cancel\">取消</button></div></form>";

        var commentForm = $(commentText);
        // update the form
        commentForm.find(".update").click(function () {
            id = $(this).attr("meta:id");
            var nameFormText = $(this).prevAll(".edit-name").val();
            var bodyFormText = $(this).prevAll(".edit-body").val();
            $mvc.Ajax.EditComment({ commentId: id, name: nameFormText, body: bodyFormText })
        .success(function (data) {
            comment = $("#comment-" + data.commentId);
            comment.children("form").remove();
            comment.children(".body").text(data.body);
            comment.children(".name").text(data.name);
            comment.children().show();
        });
        });
        // cancel the update
        commentForm.find(".cancel").click(function () {
            $(this).parents(".comment").children(":hidden").show();
            $(this).parents("form").remove();
        });

        // add the form to the current comment
        comment.append(commentForm);

    });
});

//删除评论
$(function () {
    $('.remove-comment').click(function () {
        var id = $(this).attr("meta:id");
        jConfirm('确定删除吗？', '该评论将被删除！', function (ok) {
            if (ok) {
                $mvc.Ajax.RemoveComment({ commentId: id })
                .success(function (data) {
                    $("#comment-" + data.commentId).next(".admin").fadeOut("slow", function () { $(this).remove(); });
                    $("#comment-" + data.commentId).fadeOut("slow", function () { $(this).remove(); });
                });
            }
        });
    });
});

/************************************************************************************************************
* Rate
***********************************************************************************************************/

//给文章评级
$(function () {
    $('#rate-article').click(function () {
        $mvc.Ajax.RateArticle({ articleId: $("#articleId").val(), rating: $("#rating").val() })
        .success(function (data) {
            RateArticleSuccess(data);
        });
    });
});

//成功给文章评级
function RateArticleSuccess(data) {
    var value = data.averageRating;
    var imagePosition = "50";

    if (value <= 1.3)
        imagePosition = "10";
    else if (value <= 1.8)
        imagePosition = "15";
    else if (value <= 2.3)
        imagePosition = "20";
    else if (value <= 2.8)
        imagePosition = "25";
    else if (value <= 3.3)
        imagePosition = "30";
    else if (value <= 3.8)
        imagePosition = "35";
    else if (value <= 4.3)
        imagePosition = "40";
    else if (value <= 4.8)
        imagePosition = "45";

    $("#article-votes").replaceWith("<span >" + data.votes + "</span>");
    $("#article-rating-value")
		.replaceWith("<img src=\"/Content/images/stars" + imagePosition + ".gif\" alt=\"" + value + "\" />");

    $(".rate-article :input").attr("disabled", "true");
    $(".rate-article").append("评价成功！");
}

/*************************************************************************************************
*通知专题
**************************************************************************************************/
//删除通知专题
$(function () {
    $('.remove-advice').click(function () {
        var id = $(this).attr("meta:id");
        jConfirm('确定删除吗？', '该通知专题将被删除！', function (ok) {
            if (ok) {
                $mvc.Ajax.RemoveAdvice({ adviceId: id })
                .success(function (data) {
                    $("#advice-" + data.adviceId).next(".admin").fadeOut("slow", function () { $(this).remove(); });
                    $("#advice-" + data.adviceId).fadeOut("slow", function () { $(this).remove(); });
                });
            }
        });
    });
});

/*************************************************************************************************
*通知专题索引
**************************************************************************************************/
//删除通知专题索引
$(function () {
    $('.remove-adviceindex').click(function () {
        var id = $(this).attr("meta:id");
        jConfirm('确定删除吗？', '该通知专题索引将被删除！', function (ok) {
            if (ok) {
                $mvc.Ajax.RemoveAdviceIndex({ adviceindexId: id })
                .success(function (data) {
                    $("#adviceindex-" + data.adviceindexId).next(".admin").fadeOut("slow", function () { $(this).remove(); });
                    $("#adviceindex-" + data.adviceindexId).fadeOut("slow", function () { $(this).remove(); });
                });
            }
        });
    });
});


