';
str += '';
$box.parent().find('.dem-loader').hide();
$box.html(str).data('vote', vote).find('#js-vote-fm' + vote.absnum).on('submit', function(event){
event.preventDefault();
var $form = $(this);
var $box = $form.closest('.js-vote-box');
var option_absnum = $form.find('input[name="poll"]:checked').val() || 0;
if (option_absnum > 0) {
$box.parent().find('.dem-loader').show();
$form.prop('disabled', true);
$.post(http_addr + lang + '/actions/vote', {vote:[{'absnum':$box.data('absnum') || 0, 'option_absnum': option_absnum}]}, function(data) {
var data = typeof data == 'string' ? JSON.parse(data) : data;
var done = false;
if (data.vote.success) {
var vote = data.vote.result;
$box.data('vote', vote);
set_options_done(vote.absnum, option_absnum);
done = true;
} else {
if (data.vote.result.errno == 0) {
done = true;
} else {
alert(data.vote.result.error);
}
$form.prop('disabled', false);
}
if (done) {
var done_votes = $.cookie('done_votes') || '';
done_votes = done_votes.split(',');
if ( done_votes.indexOf($box.data('vote').absnum) == -1) {
done_votes.push($box.data('vote').absnum);
$.cookie('done_votes', done_votes.join(','), {expires: 364, path: '/'});
}
build_vote_results($box, $box.data('vote'));
}else{
$box.parent().find('.dem-loader').hide();
}
});
} else {
alert('Выберите вариант ответа');
}
});
}
function set_options_done(vote, option){
var votes_options = $.cookie('votes_options') || '{}';
votes_options = JSON.parse(votes_options);
if ( typeof votes_options[vote] == 'undefined') {
votes_options[vote] = option;
$.cookie('votes_options', JSON.stringify(votes_options), {expires: 364, path: '/'});
}
}
function get_options_done(vote){
var votes_options = $.cookie('votes_options') || '{}';
votes_options = JSON.parse(votes_options);
return typeof votes_options[vote] != 'undefined' ? votes_options[vote] : 0;
}
function build_vote_results($box, vote) {
// get date of creations tart
var created = new Date(parseInt(vote.created) * 1000),
created_day = created.getDate(),
created_month = created.getMonth() + 1,
created_year = created.getFullYear(),
created_date;
created_month < 10 ? '0' + created_month : created_month;
created_day < 10 ? created_day = '0' + created_day : created_day;
created_date = created_day + '.' + created_month + '.' + created_year;
var done_votes = $.cookie('done_votes') || '';
done_votes = done_votes.split(',');
var opt = get_options_done(vote.absnum);
var str = '
' + vote.title + '
';
if (parseInt(vote.total_votes) == 0) {
vote.total_votes = 1;
}
if (vote.options.length > 0) {
for (var i in vote.options) {
var p = Math.ceil((parseInt(vote.options[i].votes)*100)/parseInt(vote.total_votes));
str
+= '