$(document).ready(function(){
    preparePollResultsDisplay();
});

function preparePollResultsDisplay() {

    var voteBox = ".quick-vote-box.voted";// if user has previously voted
    $("#submitPoll").click(function(e) {
        $("#vote-container").ajaxStart(function() {
            $(this).html('<div id="candy"><!-- candy cane --></div>');
            animateCandy("candy", "-");
        });
        // get form data
        var vote = "";
        $("form#poll-form input:radio:checked").each(function() {
           vote = this.value;
        });
        var mentometerId = $("#mentometerId").val();
        var publicationId = $("#publicationId").val();
        var redirectTo = $("#redirectTo").val();
        var dataString = "vote=" + vote + "&mentometerId=" + mentometerId + "&publicationId=" + publicationId + "&redirectTo=" + redirectTo;
        $.ajax({
            type: "POST",
            url : "/ll/poll/vote.do",
            async: true,
            data : dataString,
            success : function(data) {
                $("#vote-container").html($(data).find(".quick-vote-box.voted").html()); //parse the result from 'data'
                drawVoteBars("#vote-container");
                }
        });
        e.preventDefault();
    });
    drawVoteBars(voteBox);
}

function drawVoteBars(element) {
    if ($(".poll").length > 0) { // only run when poll box found
        if ($(element).length > 0) {
            $('#exhibition-poll .voted').css("border:1px #CCCCCC solid; display:block;");
            $('#exhibition-poll .voted .noVoteOptions').css("margin-left:0; border:1px #CCCCCC solid;");
            for (var i = 1; i < 3; i++) {
                var bar = document.getElementById("answer_" + i);
                var pixels = 2 * $("#hidden_" + i).text(); // - multipled by 2 (2 pixels for every percentage point (200px wide bar))
                var bg;
                if (i % 2 == 0) {
                    bg = '#296190';
                } else {
                    bg = '#368ed8';
                }
                bar.style.cssText = "background:" + bg + " url(" + $("#poll-bar-png-path").text() + ") no-repeat " + pixels + "px top;";
            }
        }
    }
}

/* todo rdc - this was used when building the poll originally as 'ece' wasn't logging the user's polling correctly (on DEV atleast), keep until 'ece' tested */
/*function preparePollResultsDisplay() {
            // attach behaviour to poll submit button
            $("#submitPoll").click(function() {
                var exdate = new Date();
                exdate.setDate(exdate.getDate() + 1);
                //the cookie will expire in 24 hours (based on GMT).
                document.cookie = "voted=true;expires=" + exdate.toUTCString();
            });

            if (document.cookie.indexOf("voted=true") != -1) {
            //alert("load cookie (debugging): " + getCookie("voted"));
            $(".voteOptions").hide();
            $(".panswer").show();
            $(".noVoteOptions").show();

            for (var i = 1; i < 3; i++) {
                var bar = document.getElementById("answer_" + i);
                var pixels = 2 * $("#hidden_" + i).text(); // - multipled by 2 (2 pixels for every percentage point (200px wide bar))
                var bg;
                if (i % 2 == 0) {
                    bg = '#296190';
                } else {
                    bg = '#368ed8';
                }
                bar.style.cssText = "background:" + bg + " url(images/pollbar.png) no-repeat " + pixels + "px top;"
            }
        } else {
            $(".voteOptions").show();
            $(".panswer").hide();
            $(".noVoteOptions").hide();
        }
}*/

/* only used for poll debugging purposes... */
function getCookie(c_name) {
        if (document.cookie.length > 0) {
            var c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                var c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return decodeURI(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
}
