﻿// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;
var optionText = {
    1: 'Television'
    , 2: 'Radio'
    , 3: 'Internet'
    , 4: 'Newspaper'
    , 5: 'Publication'
    , 6: 'Billboard'
    , 7: 'Other'
    , 8: 'Employer'
};

$(document).ready(function () {
    $("#poll").submit(formProcess); // setup the submit handler

    if ($("#poll-results").length > 0) {
        animateResults();
    }
    if ($.cookie('vote_id')) {
        $("#poll-container").empty();
        votedID = $.cookie('vote_id');
        $.ajax({
            type: "POST",
            url: getPollResultsUrl,
            data: "id=28",
            dataType: "json",
            success: function (response, textStatus, XMLHttpRequest) {
                if (response.error == "false") {
                    //Add into the DOM
                    //alert(JSON.stringify(response.data));
                    //alert(response.data.Table[1].Count);
                    loadResults(response.data);

                } else {
                    alert("error: " + response.message);
                }
            },
            error: function (response, textStatus, AjaxException) {
                alert('error: ' + AjaxException + ' ' + projectName + ' ' + timePeriodId);
            }
        });
    }
});

function formProcess(event) {
    event.preventDefault();
    
    var id = $("input[@name='poll']:checked").attr("value");
    id = id.replace("opt", '');

    $("#poll-container").fadeOut("slow", function () {
        $(this).empty();

        votedID = id;
        //$.getJSON(submitPollUrl + "?id=28&vote=" + id, loadResults);

        $.ajax({
            type: "POST",
            url: submitPollUrl,
            data: "id=28"
                + "&vote=" + id,
            dataType: "json",
            success: function (response, textStatus, XMLHttpRequest) {
                if (response.error == "false") {
                    //Add into the DOM
                    loadResults(response.data);

                } else {
                    alert("error: " + response.message);
                }
            },
            error: function (response, textStatus, AjaxException) {
                alert('error: ' + AjaxException + ' ' + projectName + ' ' + timePeriodId);
            }
        });

        $.cookie('vote_id', id, { expires: 365 });
    });
}

function animateResults() {
    $("#poll-results div").each(function () {
        var percentage = $(this).next().text();
        $(this).css({ width: "0%" }).animate({
            width: percentage
        }, 'slow');
    });
}

function loadResults(data) {
    var total_votes = 0;
    var percent;

    for (id in data.Table) {
        total_votes = total_votes + parseInt(data.Table[id].Count);
    }

    var results_html = "<div id='poll-results'><h3>Poll Results</h3>\n<p class='question'>How people are hearing about us:</p>\n<dl class='graph'>\n";
    for (id in data.Table) {
        var response = data.Table[id].Response;
        if (response !== null) {
            percent = Math.round((parseInt(data.Table[id].Count) / parseInt(total_votes)) * 100);
            if (data.Table[id].Response !== votedID) {
                results_html = results_html + "<dt class='bar-title'>" + optionText[response] + "</dt><dd class='bar-container'><div id='bar" + data.Table[id].Response + "'style='width:0%;'>&nbsp;</div><strong>" + percent + "%</strong></dd>\n";
            } else {
                results_html = results_html + "<dt class='bar-title'>" + optionText[response] + "</dt><dd class='bar-container'><div id='bar" + data.Table[id].Response + "'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>" + percent + "%</strong></dd>\n";
            }
        }
    }

    results_html = results_html + "</dl><p>Total Votes: " + total_votes + "</p></div>\n";

    $("#poll-container").append(results_html).fadeIn("slow", function () {
        animateResults();
    });
}
