var Questions = {
    init : function(){
        $('#moderate').click( function() {
            Questions.moderate(this);
            return false;
        });

        $('#favorite').click( function() {
            Questions.favorite(this);
            return false;
        });
    },
    
    moderate : function(t){
        var http = $(t).attr("href");
        $.ajax({
            url: http,
            success: function(responseText) {
                var response = $(responseText);
                PopupInfo.init(responseText);
                $("#btn-send-popup").click(function(){
                    var params = null;
                    response.find('.mod-error').hide()
                    if (response.find('.mod-error:first').length) {
                        if ($('[name=modmessage]').val().length) {
                            params = {
                                modmessage:$('[name=modmessage]').val()
                                }
                        } else {
                            $('.mod-error').show()
                            return false;
                        }
                    }
                    $("#btn-close-popup").click();
                    $.ajax({
                        url: $("#moderate").attr("href"),
                        type: "post",
                        data: params,
                        success: function(responseText) {
                            PopupInfo.init(responseText);
                        }
                    });
                });
            }
        });
        return false;
    },

    favorite : function(t){
        $.ajax({
            url : $(t).attr('href'),
            success : function(data) {
                PopupInfo.init(data);
            }
        });
    }
}

var PopupInfo = {
    init: function(response) {
        if (response == -1) {
            alert("Błąd");
        }

        $("body").append(response);
        var popup = $("#popup-info");
        $("#btn-close-popup").click(function(){
            Modal.hide();
            popup.hide().remove();
        });
        Modal.show();
        popup.show();
    }
}

$(document).ready(function(){
    Questions.init();

    // Więcej tagów
    $("#more-tags").livequery('click', function(){
        if ($("#hidden-tags").length < 1) {
            $.ajax({
                url: $(this).attr("href"),
                dataType: "html",
                success: function(responseText) {
                    $('<div id="hidden-tags"></div>').html($("#question-tags").html()).appendTo("body").css("display", "none");
                    $("#question-tags").attr("class", "tags").html(responseText);
                }
            });
        } else {
            var content = $("#hidden-tags").html();
            $("#hidden-tags").html($("#question-tags").html());
            $("#question-tags").attr("class", "tags").html(content);
        }
        return false;
    });

    $("#close-tags").livequery('click', function(){
        var content = $("#hidden-tags").html();
        $("#hidden-tags").html($("#question-tags").html());
        $("#question-tags").attr("class", "").html(content);
        return false;
    });
});
