﻿$(document).ready(function()
{
    $.ajaxSetup({
        type: "post",
        dataType: 'json',
        contentType: "application/json"
    });

    $("a[vs^='o_']").click(function()
    {
        var vs = $(this).attr("vs");
        var $divs = $("a[vs='" + vs + "']").parents(".IssueResult");
        if ($divs.length > 0)
        {
            $divs.children().filter(".IssueOptions").hide();
            $divs.append("<div class='Info'><img src=\"images/loading.gif\">提交中...</div>");
        }

        vote(vs, $divs);
    });

    effectHover($(".IssueStatistics .bar b, .IssueStatistics .bartext b"));

});


function effectHover(obj)
{
    obj.hover(function()
    {
        var bar, barText, title;
        if ($(this).parent().attr("class") == "bar")
        {
            bar = $(this);
            title = $(bar).attr("title");
            barText = $(this).parent().siblings(".bartext").children("b:contains('" + title + "')");
        } else
        {
            barText = $(this);
            title = $(barText).text().substring(0, $(barText).text().indexOf("("));
            bar = $(this).parent().siblings(".bar").children("b[title='" + title + "']");
        }

        $(bar).addClass("barHover").css({ opacity: 0.6 });
        
        $(barText).addClass("barTextHover");
    }, function()
    {
        var bar, barText, title;
        if ($(this).parent().attr("class") == "bar")
        {
            bar = $(this);
            title = $(bar).attr("title");
            barText = $(this).parent().siblings(".bartext").children("b:contains('" + title + "')");
        } else
        {
            barText = $(this);
            title = $(barText).text().substring(0, $(barText).text().indexOf("("));
            bar = $(this).parent().siblings(".bar").children("b[title='" + title + "']");
        }

        $(bar).removeClass("barHover").css({ opacity: "" });
        $(barText).removeClass("barTextHover");
    });
}


function vote(str, jqy, sid) {
    $.ajax({
        action: "vote",
        url: "WebSvc/SurveySvc.asmx/v",
        data: "{vs:'" + str + "'}",
        success: function(data)
        {

            if (jqy != null && jqy.length != 0)
            {
                $(jqy).html(data.d);

                $(jqy).parent().find("span.count").each(function()
                {
                    $(this).html(parseInt($(this).html()) + 1);
                });

                $(jqy).find(".bar").each(function()
                {
                    orgwidth = $(this).width();
                    if (orgwidth > 0)
                    {
                        $(this).css("width", 1);
                        $(this).animate({ width: orgwidth });
                    }
                });
                effectHover($(jqy).find(".bar b"));
                effectHover($(jqy).find(".bartext b"));
            }
            if (sid != null && sid != 0)
            {
                location.href = "IssueDetail.aspx?id=" + sid + "&view=map";
            }

        },
        error: function(xhr)
        {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
            $(jqy).html("您已经投过了, 请刷新页面获得调查结果...");
        }
    });
}


function drawPieChart(sid, divId) {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Options');
    data.addColumn('number', 'Votes');
    $.ajax({
        action: "draw",
        type: "post",
        dataType: "json",
        contentType: "application/json",
        url: "WebSvc/SurveySvc.asmx/gs",
        data: "{sid:'" + sid + "'}",
        success: function(result) {
            var stat = result.d;
            for (option in stat) {
                var curRow = data.addRow();
                data.setValue(curRow, 0, option);
                data.setValue(curRow, 1, stat[option]);
                curRow++;
            }

            // Create and draw the visualization.
            new google.visualization.PieChart(document.getElementById(divId)).draw(data, { is3D: true });
        },
        error: function(xhr, status, error) {
            alert(error);
        }
    });
}