﻿///<reference path="jQuery.intellisense.js" />
///<reference path="json2.js" />
///<reference path="WCFRestServiceProxy.js" />


var VoiceYourVoteUrl = "/VoiceYourVote/VoiceYourVote.svc/";
var VoiceYourVoteProxy = new serviceProxy(VoiceYourVoteUrl);

function GetResultsBySite(siteID, onSuccess, onError, onComplete) {
    VoiceYourVoteProxy.invoke("GetResultBySite",{id:siteId},onSuccess,onError,onComplete,false);
}

function GetAnswersBySite(siteID, onSuccess, onError, onComplete) {
    VoiceYourVoteProxy.invoke("GetAnswersBySite", { siteID: siteID }, onSuccess, onError, onComplete, false);
}

function SetNewVote(answerID, questionID, onSuccess, onError, onComplete) {
    VoiceYourVoteProxy.invoke("SetNewVote", { answerID: answerID, questionID: questionID }, onSuccess, onError, onComplete, false);
}

function BuildUrl(siteID, onSuccess, onComplete, onError) {
    VoiceYourVoteProxy.invoke("BuildUrl", { siteID: siteID}, onSuccess, onComplete, onError, false);
}

function PopulateGraph(answers, votes, question) {
    var i = 0;
    var numVotes = 0;
    $(".VYVGraph").append('<tr><td class="Question">' + question + '</td></tr>');
    for (var answer in answers) {
        $(".VYVGraph").append('<tr><td class="Answer">' + answers[i].Value + '</td></tr><tr><td class="AnswerBar"><img src="/Assets/Images/Bar' + (i + 1) + '.gif" width="' + (votes[i + 1].Value * 200) + '" height="10" alt="' + ((votes[i + 1].Value * 100) | 0) + '%"/>' + ((votes[i + 1].Value * 100) | 0) + '%</td></tr>');
        numVotes += ((votes[i + 1].Key)*1);
        i++;
    }
    $(".VYVGraph").append('<tr><td class="VYVTotal">Total Votes: ' + numVotes + '</td></tr>');
    $(".jqmConfirmTitle").prepend('' + question);
}

function confirm(msg, callback) {
    $('#confirm')
    .jqmShow()
    .end()
    .find(':submit:visible')
      .click(function() {
          if (this.value == 'yes')
              (typeof callback == 'string') ?
            window.location.href = callback :
            callback();
          $('#confirm').jqmHide();
          location.reload(true);
      });
}


$().ready(function() {
    $('#confirm').jqm({ overlay: 88, modal: true, trigger: false });

    // trigger a confirm whenever links of class alert are pressed.
    $('a.confirm').click(function() {
        confirm();
        return false;
    });
});


$(document).ready(function() {
    var questionText;
    var questionID;
    var answers = null;
    var votes;
    var i = 0;

    var siteID = $(".siteID").val();
    $(".siteID").hide();
    $.ajax({ async: false });
    BuildUrl(siteID, function(results) { questionText = results[0].Key; questionID = results[0].Value; votes = results; });
    GetAnswersBySite(siteID, function(results) {
        answers = results;
        while (answers == null) {
            i++;
        }
        for (var result in results) {
            $(".answerList").append('<p class="answerRow"><input name="answerGroup" type="radio" value="' + results[result].Key + '" />' + results[result].Value) + "</p>";
        };
        PopulateGraph(answers, votes, questionText);
    });
    $(".confirm").click(function() {
        $(".jqmConfirmWindow").show(); return false;
    });


    $("input[name='answerSection']").click(function() {
        var response = $("input[name='answerGroup']:checked").val();
        SetNewVote(response, questionID);
    });
})
