/**
 * Obsługa artukułu
 *
 * @author Sebastian Bielawski
 * @copyright Netface (c) 2010, http://www.netface.pl
 */

var EditTags = {
    // Inicjalizacja
    init: function() {
        EditTags.autocompleter();
        EditTags.send();
    },

    // Wysłanie zapytania
    send: function() {
        var forma = $("#form-edit-tags");
        forma.submit(function(){
            $.ajax({
                url: forma.attr("action"),
                type: "post",
                data: forma.serialize(),
                beforeSend: function() {
                    
                },
                success: function(responseText) {
                    $("#article-tags").html(responseText);
                    $("#edit-tags").fadeIn();
                },
                error: function(responseText) {
                    alert(responseText);
                }
            });
            return false;
        });
    },

    // Autocompleter
    autocompleter: function() {
        $("#article_tags").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "/tags/index/autocomplete",
                    dataType: "json",
                    data: {
                        query: request.term.match(/,?\ ?([^,]+)$/)[1]
                        },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                label: item,
                                value: $("#article_tags").attr("value").match(/^(.*?,?\ ?)([^,]+)$/)[1] + item
                            }
                        }))
                    }
                })
            },
            minLength: 2,
            close: function() {
                $(this).attr("value", $(this).attr("value") + ", ").focus();
            }
        });
    }
}

$(document).ready(function(){
    // Dodawanie do ulubionych
    $("#add-article-favourites").click(function(){
        $.ajax({
            url: $(this).attr("href"),
            success: function(responseText) {
                PopupInfo.init(responseText);
            }
        });
        return false;
    });

    // Zgłaszanie do moderacji
    $("#moderate").click(function(){
        $(this).stop();
        var http = $(this).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;
                        }
                    }
                    $.ajax({
                        url: $("#moderate").attr("href"),
                        data: params,
                        type: "post",
                        dataType: "html",
                        success: function(responseText) {
                            PopupInfo.init(responseText);
                        }
                    });
                });
            }
        });
        return false;
    });

    // Zgłoszenie do usunięcia
    $("#delete").click(function(){
        $.ajax({
            url: $(this).attr("href"),
            dataType: "html",
            success: function(responseText) {
                PopupInfo.init(responseText);
            }
        });
        return false;
    });

    $("#edit-tags").attr("axis", window.location);

    // Edycja tagów
    $("#edit-tags").click(function() {
        if ($(this).attr("class") == "logon1") {
            return false;
        }

        $.ajax({
            url: $(this).attr("href"),
            beforeSend: function() {

            },
            success: function(responseText) {
                $("#edit-tags").fadeOut("fast");
                $("#article-tags").html(responseText);
                EditTags.init();
            },
            error: function(responseText) {
                alert(responseText);
            }
        });
        return false;
    });

    // 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($("#article-tags").html()).appendTo("body").css("display", "none");
                    $("#article-tags").attr("class", "tags").html(responseText);
                }
            });
        } else {
            var content = $("#hidden-tags").html();
            $("#hidden-tags").html($("#article-tags").html());
            $("#article-tags").attr("class", "tags").html(content);
        }
        return false;
    });

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