﻿(function ($) {
    $(document).ready(function () {
        $('.poll').each(function () {
            var poll = $(this);
            if (poll.attr('id').indexOf('poll') == -1) {
                // In future, double nesting .poll class names is probably a bad idea. Hacking here instead of fixing properly cos I'm lazy and it's 6:45pm and I'm about to go rob a beer from the fridge downstairs...
                return;
            }
            var id = poll.attr('id').split('-')[1];
            poll.find('input[type="submit"]').click(function () {
                var answerId = poll.find('input:radio:checked').val();
                var imageWidth = poll.find('img').width();
                if (typeof (answerId) !== "undefined") {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "/webservices/PollService.asmx/Vote",
                        data: "{pollId:'" + id + "', answerId:'" + answerId + "', resultsType:'" + resultsType + "', imageWidth:'" + imageWidth + "'}",
                        dataType: "json",
                        success: function (response) {
                            poll.find('.items, input[type="submit"]').remove();
                            poll.find('.instruction').hide();
                            poll.append(response.d);
                            poll.click();
                        }
                    });
                } else {
                    $('.poll .items').effect('shake', { times: 2 }, 150);
                }
                return false;
            });
        });
    });
})(jQuery);
