var capchaDiv = 'capcha_popup';

function checkCapcha() {
    var ImageID = document.getElementById('ImageID_id').value;
    var ImageAnswer = document.getElementById('ImageAnswer_id').value;
    perl_check_capcha(
        ImageID, ImageAnswer,
        function(status, ok) {
            if (status && ok) {
                document.getElementById('capchaForm').submit();
            } else {
                perl_get_capcha_id(
                    function (status, id) {
                        if (status && id) {
                            document.getElementById('Image_id').src = 'http://' + PerlVar_BlogsPerlHost + '/cgi-bin/journal/get_image?id='+id;
                            document.getElementById('ImageID_id').value = id;
                        }
                    }
                );
                alert('Неверно введён код защиты');
            }
        }
    );
    return false;
}

function hide_capcha() {
    document.getElementById(capchaDiv).style.visibility="hidden";
}

function show_capcha(event, JournalDir, PostID, Vote, Magick)
{
    if (!JournalDir || !PostID || !Vote)
        return;
    if (!event) event = window.event;
    var element = event.target || event.srcElement;
    var div = document.getElementById(capchaDiv);
    if (element && div) {
        // tricky, eh? I do this because want to save JournalDir, PostID, Vote in local scope for callback
        perl_get_capcha_id(
            function(status, id) {
                if (status && id) {
                    var coor = get_position(element);
                    var HTML = '<div><a onclick="hide_capcha(); return false;" href=""><img src="http://img.mail.ru/mail/ru/images/blogs/close_help.gif" height="7" width="7" border="0" style="margin: 5px; float: right;"/></a>';
                    HTML += '<div style="clear:both;"><img src="http://' + PerlVar_BlogsPerlHost + '/cgi-bin/journal/get_image?id='+id+'" width="151" border="0" vspace="0" hspace="0" height="51" alt="" id="Image_id" /></div>';
                    HTML += '<div style="clear:both;">Число на картинке:</div>';
                    HTML += '<div style="clear:both; display:inline;"><form action="' +JournalDir+'vote" method="get" id="capchaForm" onsubmit="return checkCapcha();"><input type="hidden" name="back" value="' +  PerlVar_back_value + '" /><input type="hidden" name="Magick" value="' + Magick + '" /><input type="hidden" name="PostID" value="'+PostID+'" /><input type="hidden" name="vote" value="'+Vote+'" /><input type="hidden" name="ImageID" value="'+id+'" id="ImageID_id" /><input type="text" name="ImageAnswer" id="ImageAnswer_id" /><input type="submit" value="OK" /></form></div></div>';
                    div.innerHTML = HTML;
                    div.style.top = (coor.top+element.offsetHeight)+"px";
                    div.style.visibility = "visible";
                    div.style.left = (coor.left - div.offsetWidth + element.offsetWidth)+"px";
                    event.cancelBubble = true;
                }
            }
        );
    }
    return false;
}

function poll_vote(PollID)
{
    var form = document.getElementById(PollID);
    if (!form) return;
    var answers = eval('answers_'+PollID);
    if (!answers.length) return;
    var send_values = new Array();
    for (var i=0; i<answers.length; i++) {
        var answer = document.getElementById('id_'+answers[i]);
        if (answer.checked) {
            if (answer.type=='radio') {
                perl_poll_vote(PollID, [ answers[i] ], vote_callback);
                return;
            }
            send_values.push(answers[i]);
        }
    }
    perl_poll_vote(PollID, send_values, vote_callback);
}

function vote_callback(PollID, count, rows)
{
    if (!PollID || !count || !rows) return;
    count = parseInt(count);
    var form = document.getElementById(PollID);
    if (!form) return;
    if (typeof rows != 'object') return;

    var done_div = document.getElementById('vote_done_'+PollID);
    done_div.innerHTML = '<div class="vote_done mb10">Спасибо, Ваш голос принят</div>';
    var rating_timer = setTimeout(function(){clearTimeout(rating_timer);done_div.innerHTML='';},3000);

    for (var i in rows) {
        if (typeof rows[i] == 'object') {
            var row = rows[i];
            var td1 = document.getElementById('td1_'+row.ID);
            var td2 = document.getElementById('td2_'+row.ID);
            td1.innerHTML = row.text;
            td1.style.padding = '3px 0';
            td2.innerHTML = '<img src="http://img.mail.ru/r/blogs/diagram'+(row.leader ? '_red' : '')+'.gif" border="0" align="absmiddle" height="10" width="'+parseInt(row.count/(count+1)*100)*3+'" style="margin-bottom: 2px; margin-left: 20px;">&nbsp;&nbsp;<b>'+row.count+(row.proc ? ' ('+row.proc+')' : '')+'</b>';
            if (row.leader) td2.style.color = '#f00';
        }
    }
    document.getElementById('submit_button_'+PollID).style.display='none';
    document.getElementById('answers_count_'+PollID).innerHTML=count; 
}

function voteThisPost(event, JournalDir, PostID, Vote, Magick) {
    perl_check_capcha_needed(
        PostID,
        function(needed) {
            if (!needed) {
                hide_vote(PostID);
                perl_vote_post(PostID, Vote, Magick, perl_vote_post_callback);
            } else {
                show_capcha(event, JournalDir, PostID, Vote, Magick);
            }
        }
    );
    return false;
}


function perl_vote_post_callback(Rating, PostID) {
        if(Rating != undefined) { show_vote_message(PostID, Rating) }
        else { show_vote_message(PostID, 'не удалось добавить оценку') }
}
function hide_vote(PostID) {
        document.getElementById('vote_' + PostID).style.display = "none";
        show_vote_message(PostID, 'подождите...');
}
function show_vote_message(PostID, message) {
        document.getElementById('vote_' + PostID).style.display = "";
        document.getElementById('vote_' + PostID).innerHTML =  '<div class="votes"><strong>' + message +  '</strong></div>';
}

