
var which_form = null;


function recreateLinks() {
    $('a.com').each(function(idx) {
        $(this).click(function() {
            var outer_this = $(this).attr('title')
            $('a.com').each(function(i) {
                id = $(this).attr('title') 
                if (outer_this != id) {
                    //alert('klikniete id: '+outer_this+', bierzace id w petli: '+id)
                    $('#inner_form_placement_'+id).slideUp()
                    $('#inner_form_placement_'+id).html('')
                }
            });
            var cur_id = $(this).attr('title');
            var dest = '#inner_form_placement_'+cur_id
            var source = $('#inner_form_content').html()
            var quote_this = $('.__quote_this_orig_'+cur_id).text()
            var quote_nick = $('.__comment_nick_'+cur_id).val()
            source = source.replace('inner_form_container', 'inner_form_container_copy_'+cur_id).replace('comment_form_inner', 'comment_form_inner_copy_'+cur_id)
            $(dest).html(source)
            $('#inner_form_container_copy_'+cur_id).show()
            $('#inner_form_placement_'+cur_id).slideDown()
            $('#inner_form_container_copy_'+cur_id+' textarea').val("[quote nick="+quote_nick+"]"+quote_this+"[/quote]\n")
            $("#comment_form_inner_copy_"+cur_id).append("<input type='hidden' name='parent_id' value='"+cur_id+"'>");
            $("#comment_form_inner_copy_"+cur_id).validate();
            return false;
        });
    });
}

function success(responseText, statusText) {
    var comment_list = $("#comment_container > #single_comment")
    var txt = ''
    if (comment_list.length < 10) {
        $(".__no_comments").html('')
        $("#comment_container").append(responseText)
        recreateLinks();
    }
    else {
        txt = ' (na ostatniej stronie komentarzy)'
    }
    if (which_form == '0')
        $('#output1').html("<center>Twój komentarz został dodany jako najnowszy"+txt+".</center>");
    else
        $('#output1_inner').html("<center>Twój komentarz został dodany jako najnowszy"+txt+".</center>");
}

function error(responseText, statusText, errorThrown) {
    if (which_form == '0')
        $('#output1').html("<center>Wystąpił błąd. Prosimy spróbować ponownie.</center>");
    else
        $('#output1_inner').html("<center>Wystąpił błąd. Prosimy spróbować ponownie.</center>");
}

$(document).ready(function (){

    $(".__show").each(function() {
        //$(this).show()
        $(this).click(function () {
            var id = $(this).attr('id')
            var tmp = id.split('_')
            id = tmp[1]
            $('#post_'+id).attr('style', 'display:none') 
            $('#post_'+id).removeClass('hide') 
            $('#post_'+id).slideDown()
            $(this).hide()
            $('#add_'+id).show()
            //$('#quote_'+id).show()
        });

    });

    $(".__down, .__up").each(function() {
        $(this).show()
        $(this).click(function () {
            var rand_no = Math.random();
            var id = $(this).attr('id')
            var tmp = id.split('_')
            id = tmp[3]
            var vote = tmp[2]  
            $.ajax({
               type: "GET",
               url: "/mod_receipts__comment__vote.do",
               data: "post_id="+id+"&vote="+vote+"&r="+rand_no,
               success: function(msg) {
                    if (msg == 'null') {
                        alert('Musisz się zalogować!')
                    }
                    else {
                        $('#_count_'+id).html(msg)
                        if (parseInt(msg)<-3) {
                            $('#post_'+id).addClass('hide') 
                            $('#show_'+id).show()
                            $('#add_'+id).hide()
                            //$('#quote_'+id).hide()
                        }
                        if (parseInt(msg)>=-3) {
                            $('#post_'+id).removeClass('hide') 
                            $('#show_'+id).hide()
                            $('#add_'+id).show()
                            //$('#quote_'+id).show()
                        }
                        if (parseInt(msg)>=0) {
                            $('#_count_'+id).removeClass('red') 
                        }
                        if (parseInt(msg)==-1) {
                            $('#_count_'+id).addClass('red') 
                        }
                    }
               },
               error: function(req, status, err) {
                    //$('#vote_fail_trigger').trigger('click')
               },
               beforeSend: function() {
                    //var dest_id = '#remove_gfx_'+id
                    //$(dest_id).attr('src', '/i/loader.gif')
               }
           });
        });
    
    });
});

