//第一次呈现由服务端完成，投票项目，类型等有服务端生成。，客户端负责：2.提交投票数据。3。返回投票结果数据
$(document).ready(function() {
    //投票ajax
    $(".voteBox").each(function() {
        var myVoteBox = $(this);
        var myVoteMsg = myVoteBox.find(".voteMsg span");
        var myVotePanel = myVoteBox.find(".votePanel");
        var myShowPanel = myVoteBox.find(".showPanel");
        var myVateId = myVoteBox.find(".voteId").val();
        var myUnit = myVoteBox.find(".unit").text();
        //显示ajax请求loading,在ajax请求时调用
        function showAjaxLoading() {
            myVoteBox.find(".loading").show("normal");
        }
        //隐藏ajax请求loading
        function hideAjaxLoading() {
            myVoteBox.find(".loading").hide("normal");
        }
        //处理投票数据，并显示(返回数据,是否投票)
        function showVote(result, type) {
            //投票结束后根据返回显示结果
            myVoteMsg.hide();
            if (result[0].indexOf("+") > 0) {
                result[0] = result[0].replace("+", "");
                //已经投票过
                myVoteMsg.eq(1).show();
            }
            else {
                //投票成功
                myVoteMsg.eq(0).show();
            }
            //开始处理数据
            var count = 0;
            for (var i = 0; i < result.length; i++) {
                count = count + (result[i] - 0);
            }
            myVoteBox.find(".showVoteList span").each(function(i) {
                var mySpan = $(this);
                var myCount = result[i] - 0;
                var percent = ((myCount * 100) / count).toFixed(2);
                if (myCount == 0) {
                    percent = 0.00;
                }
                mySpan.text("[" + myCount + myUnit +" "+ percent + "%]");
            });
            if (!type) {
                myVoteMsg.hide();
            }
            myVotePanel.hide("normal");
            myShowPanel.show("normal");
            hideAjaxLoading(); //隐藏loading
        }
        //提交投票事件
        myVoteBox.find(".voteButton").click(function() {
            var myValueList = "";
            myVoteBox.find(".voteList input").each(function() {
                if ($(this).attr("checked")) {
                    myValueList = myValueList + "|1";
                }
                else {
                    myValueList = myValueList + "|0";
                }
            });
            //ajax提交投票
            showAjaxLoading();
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "WebService/WS_Vote.asmx/Submit",
                data: "{voteId:'" + myVateId + "',valueList:'" + myValueList + "'}",
                dataType: 'json',
                success: function(result) {
                    showVote(result, true);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    myVoteMsg.hide();
                    myVoteMsg.eq(2).show();
                    myVotePanel.hide("normal");
                    myShowPanel.show("normal");
                    hideAjaxLoading();
                }
            });

        })
        //显示投票事件
        myVoteBox.find(".showButton").click(function() {
            //ajax查看投票
            showAjaxLoading();
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "WebService/WS_Vote.asmx/VoteValueList",
                data: "{voteId:'" + myVateId + "'}",
                dataType: 'json',
                success: function(result) {
                    showVote(result, false);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    myVoteMsg.hide();
                    myVoteMsg.eq(2).show();
                    myVotePanel.hide("normal");
                    myShowPanel.show("normal");
                    hideAjaxLoading();
                }
            });
        });
    });
});
$(function() {
    $(".shanrui").show();
});