var Stickers = function i() {

    var form = null;
    var node = null;

    var STICKERS_CAT_LIST = 'STICKER_CAT_LIST';
    var STICKERS_SELECTED_CAT = 'STICKERS_SELECTED_CAT';
    var STICKERS_LIST = 'STICKERS_LIST_';
    var STICKERS_PRELOAD = '<div id="stickers_loader"><div class="speeding_wheel"></div></div>';

    function getStickersCat(args) {

        Tick.stopTick(getStickersCat);
        Tick.setTime(1e2);
        if (ssGet(STICKERS_CAT_LIST) !== null) {
            Tick.setTick(generatePanel);
            return;
        }
        $.post('/ajax/?act=stickers', args, function (d) {
            ssSet(STICKERS_CAT_LIST, d), Tick.setTick(generatePanel);
        });
    }

    function getStickersList() {
        Tick.stopTick(getStickersList);
        $.post('/ajax/?act=stickers', {
            id: ssGet(STICKERS_SELECTED_CAT)
        }, function (d) {
            ssSet(STICKERS_LIST + ssGet(STICKERS_SELECTED_CAT), d);
            generateStickersList();
        });
    }

    function generatePanel() {
        Tick.stopTick(generatePanel);
        generatePreload(1);
        var n = '<div id="stick_panel" class="smile_panel">' + '<div class="sp_tabs">' + '<div class="sp_smile_tabs">' + '<div class="scroller_wrap">' + '<div class="sp_smile_cont scroller_cont">';
        n += '<a class="sp_tab sp_tab_inactive" href="/stickers/">' +
            '<img src="/template/icons/store.svg" class="sp_icon">' +
            '</a>';
        for (var i = 0, d = ssGet(STICKERS_CAT_LIST); i < d.length; i++) {
            n += '<span class="sp_tab sp_tab_inactive" data-sp-tab="' + d[i].id_cat + '" onclick="return Stickers.SelectTab(this);">' +
                '<img src="https://files.vineti.ru/stickers/' + d[i].cover + '" class="sp_icon">' +
                '</span>';
        }
        n += "</div>" + "</div>" + "</div>" +
            '<div class="stickers_ttt_wrap">' +
            '<div class="stickers_block_cont">' +
            '<div class="stickers_list_cont">' +
            '<div class="stickers_list_scroll">' +
            '<div class="stickers__row"></div>' + "</div>" + "</div>" + "</div>" + "</div>" + "</div>";
        node.after(n);
        d !== undefined && d.length > 0 && ssGet(STICKERS_SELECTED_CAT) === null && (ssSet(STICKERS_SELECTED_CAT, parseInt(d[0].id_cat)));
        Stickers.SelectTab("#stick_panel span.sp_tab[data-sp-tab=" + ssGet(STICKERS_SELECTED_CAT) + "]", false);
    }

    function generateStickersList() {
        Tick.stopTick(generateStickersList);
        generatePreload(1);
        var node = $("#stick_panel .stickers__row").empty();
        for (var i = 0, d = ssGet(STICKERS_LIST + ssGet(STICKERS_SELECTED_CAT)); i < d.length; i++) {
            node.append('<div class="sticker_item" onclick="return Stickers.SelectStickers(this, ' + d[i].code + ');">' + '<label class="sticker_thumb">' + '<img src="https://files.vineti.ru/stickers/' + d[i].image + '" class="th_img">' + "</label>" + "</div>");
        }
    }

    function generatePreload(type) {
        var n = $("#StickersButton");
        $('#stickers_loader').remove();
        if (type === 0) {
            n.hide().before(STICKERS_PRELOAD);
        } else {
            n.show();
        }
    }

    function clearCacheStickers() {
        for (var i = 0, key; i < sessionStorage.length; i++) {
            key = sessionStorage.key(i), 0 === key.indexOf(STICKERS_LIST) && ssSet(key, null);
        }
    }

    return {
        init: function () {
            return $(function () {
                $("#StickersButton")
                    .off('click')
                    .show()
                    .on('click', Stickers.toggle);
            }), function () {
                location.href.search(/stickers/) > 0 && (null !== ssGet(STICKERS_CAT_LIST) && ssSet(STICKERS_CAT_LIST, null), clearCacheStickers(), ssSet(STICKERS_SELECTED_CAT, null));
                form = node = null;
                return true;
            }();
        }, close: function () {
            var n = $("#stick_panel");
            if (n.length > 0) {
                n.remove();
                return true;
            }
            return false;
        }, toggle: function (event) {

            $("#smile_panel").remove();
            if (Stickers.close()) {
                return false;
            }
            generatePreload(0);
            form = $(event.target).parents().find('form');
            node = $('.create_message');
            if (node.length <= 0)
                node = form;

            Tick.stopTick(getStickersCat);
            Tick.setTime(5e2);
            Tick.setTick(getStickersCat);
            return false
        }, SelectTab: function (t, p) {
            $('#stick_panel .sp_tab_selected').removeClass('sp_tab_selected').addClass("sp_tab_inactive");
            t = $(t).removeClass("sp_tab_inactive").addClass("sp_tab_selected");
            var d = parseInt(t.attr('data-sp-tab'));
            ssGet(STICKERS_SELECTED_CAT) !== d && (ssSet(STICKERS_SELECTED_CAT, d));
            var s = ssGet(STICKERS_LIST + d);
            p === undefined && generatePreload(0);
            Tick.setTime(5e2);
            Tick.setTick(s === null ? getStickersList : generateStickersList);
        }, SelectStickers: function (t, d) {
            if (/^([0-9]{1,9})$/.test(d) === false)
                return;
            form.find('textarea').val('[stickers=' + d + ']');
            form.find('input[type=submit]').click();
            form.find('textarea').val('');
        }
    }
}();
Stickers.init();