var App = function () {

    // Komunikat
    // ---
    // App.alert('(success||s)', 'Operacja powiodła się')
    // App.alert('(error|e)', 'Wystąpił błąd')
    // ---
    var alert = function (type, html, time) {
        time = time || 6;

        var title = 'Ostrzeżenie';

        if (type == 'success' || type == 's') {
            type = 'success';
            title = 'Powodzenie';
        } else if (type == 'info' || type == 'i') {
            type = 'info';
            title = 'Info';
        } else if (type == 'error' || type == 'e') {
            type = 'error';
            title = 'Błąd';
        }

        new PNotify({
            title: title,
            text: html,
            type: type,
            styling: 'brighttheme'
        });
    }

    return {

        alert: function (type, html, time) {
            time = time || 6;
            alert(type, html, time)
        },

        alert_401: function () {
            alert('e', 'Nie jesteś zalogowany. Proszę się zalogować'); // Brak autoryzacji!
        },

        alert_403: function () {
            alert('e', 'Nie posiadasz odpowiednich uprawnień aby wykonać tę operację'); // Brak uprawnień!
        },

        error: function (html, time) {
            App.alert('e', html, time);
        }

    };

}();

$(function () {
    var spinner = [];

    $.each($('a[data-load-text], button[data-load-text]'), function () {
        var text = $(this).attr('data-text');
        $(this).html(text);
    });

    $(document).on('start', 'a[data-load-text], button[data-load-text]', function (event, spinner_color) {
        spinner_color = spinner_color || '#fff';

        var text = $(this).attr('data-load-text');
        $(this).html(text);
        $(this).attr('loading', 'loading');
    });

    $(document).on('stop', 'a[data-load-text], button[data-load-text]', function () {
        var text = $(this).attr('data-text');
        $(this).html(text);
        $(this).removeAttr('loading');

        var loader = $(this).find('span.loader');
        loader.hide();
    });
});

$(function () {
    // Wyszukiwanie na stronie głównej
    (function () {
        var searchForm = $('[action="/home/search"]');

        searchForm.find('#picking_branza').autocomplete({
            source: "/home/input/branza",
            minLength: 1,
            select: function (event, ui) {
                searchForm.find('[name=branza_id]').val(ui.item.id)
            }
        });

        searchForm.find('#picking_miasto').autocomplete({
            source: "/home/input/miasto",
            minLength: 1,
            select: function (event, ui) {
                searchForm.find('[name=miasto_id]').val(ui.item.id)
            }
        });
    })();
});