﻿google.load("visualization", "1", { packages: ["columnchart"] });
var pollHashes = new Object();
var pollTimers = new Object();
function startPoll(pollId, divId, questionDivId, legendDivId, width, height, hours) {
    pollHashes[divId] = -1;
    var params = { divId: divId, width: width, height: height, hours: hours, questionDivId: questionDivId, legendDivId: legendDivId };
    loadPoll(pollId, params);
    var timerId = pollTimers[divId];
    clearTimeout(timerId);
    timerId = setInterval(function() { loadPoll(pollId, params) }, 1000);
    pollTimers[divId] = timerId;
}
function loadPoll(pollId, params) {
    PollService.FindPoll(pollId, params.hours, loadPollSuccess, null, params);
}
function loadPollSuccess(poll, params) {
    if (poll.Hash != pollHashes[params.divId]) {
        drawPoll(document.getElementById(params.divId), poll, params.width, params.height);
    }
    document.getElementById(params.questionDivId).innerHTML = poll.Question;
    drawLegend(document.getElementById(params.legendDivId), poll);
    pollHashes[params.divId] = poll.Hash;
}
function drawLegend(targetDiv, poll) {
    var html = "";
    for (var index in poll.Scores) {
        var score = poll.Scores[index]; ;
         html += "<div style='margin:5px; margin-left:20px; margin-top:4px; height:10px; width:10px; float:left; background-color:" + score.Team.Color +"'></div><div style='float:left; font-size:10px;'><b>"+ score.Team.Name + "</b> - Votes: " + score.Total;
        if (!poll.Simple) {
            html +="  Score: " + score.Average;
        }
        html +="</div><div style='clear:both;'></div>";
    }
    targetDiv.innerHTML = html;
}
function drawPoll(targetDiv, poll, width, height) {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Year');
    for (var index in poll.Scores) {
        var score = poll.Scores[index];;
        data.addColumn('number', score.Team.Name);
    }
    data.addRows(1);
    data.setValue(0, 0, '');
    for (var index in poll.Scores) {
        var column = Number(index);
        var score = poll.Scores[index];
        if (poll.Simple) {
            data.setValue(0, column + 1, score.Total);
        }
        else {
            data.setValue(0, column + 1, score.Average);
        }
    }
    targetDiv.innerHTML = "";
    var chart = new google.visualization.ColumnChart(targetDiv);
    var title = poll.Name;
     if (poll.Hidden) {
     title = "";
     }
    if (poll.Name.length > 0) {
    if (!poll.Hidden) {
           title += " (" + poll.Total + " votes counted)";
        }else
        title += "";
    }
    var options = { title: title, is3D: true };
    options.width = width;
    options.height = height;
    options.legend = 'none';
	options.min = 0;
    var colors = new Array();
    for (var index in poll.Scores) {
        colors[index] = poll.Scores[index].Team.Color;
    }
    options.colors = colors; 
    chart.draw(data, options);
} 
