//------------------------------------------------//
var Poll = {
    //=====================================================//
    submitPollAnswer: function(pollID, pollHistoryID){
        var url = "/index.php?_spAction=submitPollAnswer" +
                 "&poll_id=" + pollID + "&_room=poll" + "&poll_history_id=" + pollHistoryID + "&showHTML=0" ;

        $.get(url, {poll_history_id: pollHistoryID}, function (data) {
            var errorNo   = $(data).find("errors error error_no").text();
            var errorText = $(data).find("errors error error_text").text();
            if (errorNo == 0){
                var totalCount = $(data).find("header total_count").text();
                // var displayTitle = $(data).find("header display_title").html();
                var htmlText = "";
                $(data).find("row").each(function() {
                    var row = $(this);
                    var title    = row.find("title").text();
                    var ansCount = row.find("answer_count").text();

                    var ansCountPercentage = (ansCount == 0) ? 0 : ((ansCount/totalCount)*100).toFixed(2);

                    htmlText +=
                    '<div class="answer">' + title + "</div>" +
                    '<div class="percentageBarOuter"><div class="percentageBar" style="width:' + ansCountPercentage  + 'px;"></div></div>' +
                    '<div class="percentageText">' + ansCountPercentage + '%</div>' +
                    '<div class="seperator"></div>';
                });
            }
            else {
                htmlText = '<hr><div class="answer">' + errorText + '</div><br>';
            }
            $("#pollResult").html(htmlText);
        }, 'xml');

    }
}

$(function(){
    $('#frmPoll #submit_poll').click(function(e){
        e.preventDefault();
        var poll_id = $('#frmPoll #poll_id').val();
        var poll_hist_id = $("#frmPoll input:checked").val();
        
        if (poll_hist_id == undefined){
            var poll_hist_id = '';
        }
        
        Poll.submitPollAnswer(poll_id, poll_hist_id)
    });
});
