$(document).ready(function() {
        //edycja wiadomosci:
        function hideForms() {
            $("form.editForm").hide().prev("div.anwserBody").show();
        }
        $("a.editMessage").live("click",function() {
                hideForms();
                var id = $(this).attr("id").replace("edit_", "");
                var textHolder = $(this).parents("div.answer").children("div.anwserBody");
                var text = textHolder.siblings(".anwserNotMarkdowned").text();
                if (textHolder.siblings(".editForm").length) {
                    textHolder.siblings(".editForm").children("textarea").val(text);
                    textHolder.hide();
                    textHolder.siblings(".editForm").show();
                }
                else {
                    //tworzymy formularz
                    var form = $("<form></form>").attr("method", "post").attr("action", "/forum/threads/editMessage/"+id).addClass("editForm");
                    $("<textarea></textarea>").attr("name", "body").val(text).appendTo(form).addClass("rogi");
                    $('<input type="submit" />').val("Zapisz").appendTo(form);
                    $('<input type="button" />').val("Anuluj").addClass("cancelEdit").appendTo(form);
                    form.hide();
                    textHolder.after(form);
                    textHolder.hide();
                    form.show();
                }
                return false;
            });
        $("input.cancelEdit").live("click", function() {
                hideForms();
            });
        $("form.editForm").live("submit", function() {
                data = $(this).serialize();
                var form = $(this);
                $.ajax({url: $(this).attr("action"),
                            type: "POST",
                            cache:false,
                            data:data,
                            success:function(data) {
                            //dostalismy html wypowiedzi!
                            form.parents("li").fadeOut(500, function() {
                                    $(this).html(data);
                                    $(this).fadeIn(500);
                                });
                        }});
                hideForms();
                return false;
            });
        //usuwanie wiadomośći AJAX:
        $("a.deleteMessage").live("click", function() {
                //check = confirm("Czy napewno chcesz usunąć tę wiadomość?");
                //if (!check)
                //    return false;
                var link = $(this);
                $.ajax({url: $(this).attr("href"),
                            dataType: "text",
                            success: function(data) {
                            if (data==1) {
                                $(link).parents("li.answer").fadeOut(1000, function() {
                                        $(this).remove();
                                    });
                            }
                        },
                    });
                return false;
            });
        //edycja tematu:
        $("a.edytujTytul").click(function() {
                if (!$(this).siblings('span').length)
                    return false;
                var id = $(this).siblings("span").attr("id").replace("thread_","");
                var title = $(this).siblings("span").text();
                //tworzymy form:
                var form = $('<form class="editTitleForm" action="/forum/threads/editThread/'+id+'" method="post"></form>')
                    .append($('<input type="text" />').attr("name", "title").val(title))
                    .append($('<input type="submit"/>').val("Zapisz"))
                    .append($("<a></a>").addClass("cancelEdit").text("anuluj"));
                $(this).parent().after(form);
                $(this).parent().hide();
                return false;
            });
        $("a.cancelEdit").live("click",function() {
                $(this).parent().hide();
                $("h2#temat").show();
            });
        //przenoszenie watku:
        $("a#moveThread").click(function() {
                $("form#moveThreadForm").show();
                return false;
            });
        $("a.cancelMove").click(function() {
                $("form#moveThreadForm").hide();
                return false;
            });
        $("a#showHistory").click(function() {
                $("div#threadHistory").toggle();
                return false;
            });
    });

