﻿var showMessage = function(object, msg) {
    var div = $('<div class="vote-notification"><h3>' + msg + '</h3>(点击关闭)</div>');
    div.click(function(event) {
        $(".vote-notification").fadeOut("fast", function() { $(this).remove(); });
    });
    object.parent().append(div);
    div.fadeIn("fast");
};

var AD = {
    init: function() {

    }
}


var answerId;
var voteNumberClass = "vote-count-post";
var voteContainerId = "vote-buttons";
var favoriteContainerId = "question-favorite";
var answerContainerIdPrefix = 'answer-container-';
var imgIdPrefixAccept = 'answer-img-accept-';
var imgClassPrefixFavorite = 'question-img-favorite';
var imgIdPrefixQuestionVoteup = 'question-img-upvote-';
var imgIdPrefixQuestionVotedown = 'question-img-downvote-';
var imgIdPrefixAnswerVoteup = 'answer-img-upvote-';
var imgIdPrefixAnswerVotedown = 'answer-img-downvote-';
var divIdFavorite = 'favorite-number';

var getFavoriteButton = function() {
    var favoriteButton = 'div.' + favoriteContainerId + ' img[class=' + imgClassPrefixFavorite + ']';
    return $(favoriteButton);
};
var getFavoriteNumber = function() {
    var favoriteNumber = '#' + divIdFavorite;
    return $(favoriteNumber);
};
var getQuestionVoteUpButton = function() {
    var questionVoteUpButton = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixQuestionVoteup + ']';
    return $(questionVoteUpButton);
};
var getQuestionVoteDownButton = function() {
    var questionVoteDownButton = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixQuestionVotedown + ']';
    return $(questionVoteDownButton);
};
var getAnswerVoteUpButtons = function() {
    var answerVoteUpButton = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixAnswerVoteup + ']';
    return $(answerVoteUpButton);
};
var getAnswerVoteDownButtons = function() {
    var answerVoteDownButton = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixAnswerVotedown + ']';
    return $(answerVoteDownButton);
};
var getAnswerVoteUpButton = function(id) {
    var answerVoteUpButton = 'div.' + voteContainerId + ' img[id=' + imgIdPrefixAnswerVoteup + id + ']';
    return $(answerVoteUpButton);
};
var getAnswerVoteDownButton = function(id) {
    var answerVoteDownButton = 'div.' + voteContainerId + ' img[id=' + imgIdPrefixAnswerVotedown + id + ']';
    return $(answerVoteDownButton);
};

var VoteType = {
    acceptAnswer: 0,
    questionUpVote: 1,
    questionDownVote: 2,
    answerUpVote: 3,
    answerDownVote: 4,
    favorite: 5

};

var callback_favorite = function(object, voteType, data) {
    if (data.allowed == "0" && data.success == "0") {
        showMessage(object, "匿名用户无法收藏,请先登录或者注册");
    }
    else if (data.status == "2") {
        showMessage(object, "您不能收藏自己发表的问题");
    }
    else if (data.status == "1") {
        object.attr("src", "/Content/Images/vote-favorite-off.png");
        var fav = getFavoriteNumber();

        if (data.count == 0)
            data.count = '';
        fav.empty();
        fav.append("<b>" + data.count + "</b>");
    }
    else if (data.success == "1") {
        object.attr("src", "/Content/Images/vote-favorite-on.png");
        var fav = getFavoriteNumber();
        fav.empty();
        fav.append("<b class='favoritecount-selected'>" + data.count + "</b>");

    }
    else {
        showMessage(object, data.message);
    }
};

var callback_vote = function(object, voteType, data) {

    if (data.allowed == "-2") {
        if (voteType == VoteType.questionUpVote || voteType == VoteType.answerUpVote) {
            showMessage(object, "需要+15积分才能投支持票.");
        }
        else if (voteType == VoteType.questionDownVote || voteType == VoteType.answerDownVote) {
            showMessage(object, "需要+30积分才能投反对票.");
        }
    }

    if (data.allowed == "-1") {
        showMessage(object, "不能对自己的帖子进行投票");
    }

    if (data.status == "2") {
        showMessage(object, "您已经投过票");
    }
    else if (data.status == "1") {
        Vote.setVoteImage(voteType, false, object);
        Vote.setVoteNumber(object, data.count);
        showMessage(object, "感谢的您的投票");
    }
};

var callback_accept = function(object, voteType, data) {


    if (data.allowed == "0" && data.success == "0") {
        showMessage(object, "匿名用户无法设定最佳答案");
    }
    else if (data.allowed == "-1") {
        showMessage(object, "您不是该问题的创建者,无法设定最佳答案");
    }
    else if (data.status == "1") {
        object.attr("src", "/Content/Images/vote-accepted.png");
        $("#" + answerContainerIdPrefix + answerId).removeClass("accepted-answer");

    }
    else if (data.success == "1") {
        var acceptedButtons = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixAccept + ']';
        $(acceptedButtons).attr("src", "/Content/Images/vote-accepted.png");
        var answers = ("div[id^=" + answerContainerIdPrefix + "]");
        $(answers).removeClass("accepted-answer");

        object.attr("src", "/Content/Images/vote-accepted-on.png");
        $("#" + answerContainerIdPrefix + answerId).addClass("accepted-answer");
    }
};

var handleFail = function(xhr, msg) {
    alert("Callback invoke error: " + msg)
};

var Vote = {
    init: function() {



        // accept answers
        if (questionAuthorId == currentUserId) {
            var acceptedButtons = 'div.' + voteContainerId + ' img[id^=' + imgIdPrefixAccept + ']';
            $(acceptedButtons).unbind('click').click(function(event) {
                Vote.accept($(event.target))
            });
        }
        // set favorite question
        var favoriteButton = getFavoriteButton();
        favoriteButton.unbind('click').click(function(event) {
            Vote.favorite($(event.target))
        });

        // question vote up
        var questionVoteUpButton = getQuestionVoteUpButton();
        questionVoteUpButton.unbind('click').click(function(event) {
            Vote.vote($(event.target), VoteType.questionUpVote)
        });

        var questionVoteDownButton = getQuestionVoteDownButton();
        questionVoteDownButton.unbind('click').click(function(event) {
            Vote.vote($(event.target), VoteType.questionDownVote)
        });

        var answerVoteUpButton = getAnswerVoteUpButtons();
        answerVoteUpButton.unbind('click').click(function(event) {
            Vote.vote($(event.target), VoteType.answerUpVote)
        });

        var answerVoteDownButton = getAnswerVoteDownButtons();
        answerVoteDownButton.unbind('click').click(function(event) {
            Vote.vote($(event.target), VoteType.answerDownVote)
        });

    },

    setVoteImage: function(voteType, undo, object) {
        var flag = undo ? "" : "-on";
        var arrow = (voteType == VoteType.questionUpVote || voteType == VoteType.answerUpVote) ? "up" : "down";
        object.attr("src", "/Content/Images/vote-arrow-" + arrow + flag + ".png");

        // if undo voting, then undo the pair of arrows.
        if (undo) {
            if (voteType == VoteType.questionUpVote || voteType == VoteType.questionDownVote) {
                $(getQuestionVoteUpButton()).attr("src", "/Content/Images/vote-arrow-up.png");
                $(getQuestionVoteDownButton()).attr("src", "/Content/Images/vote-arrow-down.png");
            }
            else {
                $(getAnswerVoteUpButton(answerId)).attr("src", "/Content/Images/vote-arrow-up.png");
                $(getAnswerVoteDownButton(answerId)).attr("src", "/Content/Images/vote-arrow-down.png");
            }
        }
    },

    setVoteNumber: function(object, number) {
        var voteNumber = object.parent('div.' + voteContainerId).find('span.' + voteNumberClass);
        $(voteNumber).text(number);
    },

    submit: function(object, voteType, callback) {
        $.ajax({
            type: "POST",
            cache: false,
            dataType: "json",
            url: "/Question.mvc/Vote/" + questionId,
            data: { "voteType": voteType, "answerId": answerId },
            error: handleFail,
            success: function(data) { callback(object, voteType, data) }
        });
    },

    vote: function(object, voteType) {
        if (!currentUserId || currentUserId == "-1") {
            showMessage(object, "匿名用户无法投票,请先<a href='/User/Login/'>登录或者注册</a>");
            return false;
        }
        if (voteType == VoteType.answerUpVote) {
            answerId = object.attr("id").substring(imgIdPrefixAnswerVoteup.length);
        }
        else if (voteType == VoteType.answerDownVote) {
            answerId = object.attr("id").substring(imgIdPrefixAnswerVotedown.length);
        }
        Vote.submit(object, voteType, callback_vote);
    },


    favorite: function(object) {
        if (!currentUserId || currentUserId == "-1") {
            showMessage(object, "匿名用户无法收藏,请先登录或者注册");
            return false;
        }
        Vote.submit(object, VoteType.favorite, callback_favorite);
    },

    accept: function(object) {
        answerId = object.attr("id").substring(imgIdPrefixAccept.length);
        Vote.submit(object, VoteType.acceptAnswer, callback_accept);
    }




};
