/**
 * Author - koder_alex
 * ISQ - 669906617
 * VK - https://vk.com/koder_alex
 * KL - http://koder-alex.ru
 * It is forbidden to give, sell, modify.
 */
/*document.write('<link rel="stylesheet" href="/template/css/SmileButton.css" type="text/css" />');*/
function ssSet(a, b) {
    if ("undefined" != typeof b)try {
        return sessionStorage.setItem(a, JSON.stringify(b))
    } catch (c) {
    } else try {
        return sessionStorage.removeItem(a)
    } catch (c) {
    }
    return !1
}
function ssGet(a) {
    try {
        return JSON.parse(sessionStorage.getItem(a))
    } catch (b) {
    }
    return !1
};
var timeR = null;
var SmileButton = function () {
    return {
        init: function () {
            $("#SmileButton").show();
        }, click: function (e, a, c) {
            var b = $("textarea");
            if(b.length <= 0)
                b = $('[data-id=emoji-block_1]');
            b=b.get(0);
            b.focus();
            a || (a = "");
            if (window.attachEvent && -1 === navigator.userAgent.indexOf("Opera")) {
                if (c = b.sel) {
                    b = c.text.length, c.text = e + c.text + a, c.moveEnd("character", -a.length), c.moveStart("character", -b), c.select();
                }
            } else {
                var h = b.scrollTop, f = b.value.substr(0, b.selectionStart), k = b.value.substr(b.selectionEnd),
                    g = b.value.substr(b.selectionStart, b.selectionEnd - b.selectionStart);
                b.value = f + e + g + a + k;
                c ? (b.selectionStart = f.length + e.length + g.length + a.length, b.selectionEnd = b.selectionStart) : (b.selectionStart = f.length + e.length, b.selectionEnd = b.selectionStart + g.length);
                b.scrollTop = h;
            }
            return !1;
        }, Toggle: function () {
            $("#smile_panel").remove();
            $(".sp_tab-" + ssGet("smileTabId")).removeClass("sp_tab_inactive").addClass("sp_tab_selected");
            return true;
        }, SelectTab: function (n, id) {
            var node = $(n);
            node.removeClass("sp_tab_inactive").addClass("sp_tab_selected");
            if (ssGet("smileTabId") != id) {
                $(".sp_tab-" + ssGet("smileTabId")).removeClass("sp_tab_selected").addClass("sp_tab_inactive");
            }
            ssSet("smileSelectedTab" + id, null);
            var data = ssGet("smileSelectedTab" + id);
            if (data === null || data.length <= 0) {
                $.post("/ajax?act=smile", {id: id}, function (a) {
                    ssSet("smileSelectedTab" + id, a);
                });
            }
            clearInterval(timeR);
            timeR = setInterval(function () {
                data = ssGet("smileSelectedTab" + id);
                if (data === null || data.length <= 0) {
                    return;
                }
                var node = $(".smile_list").empty();
                for (var i = 0; i < data.length; i++) {
                    node.append('<div class="smile_item" onclick="return SmileButton.click(' + "'" + data[i].emoji + "'" + ');">' + '<span class="smilebtn">' + '<img src="/template/emoji/' + data[i].file + '_x1.png">' + "</span>" + "</div>");
                }
                ssSet("smileTabId", id);
                clearInterval(timeR);
            }, 100);
            return true;
        }
    };
}();
$(function () {
    SmileButton.init();
});
var timerS = undefined;
$(document).on("click", "i[id=SmileButton]", function (event) {
    var node = null;
    event.preventDefault();
    if ($("#smile_panel").length > 0) {
        $("#smile_panel").remove();
        return;
    }
    node = $('.create_message');
    if(node.length <= 0)
        node = $(event.target).parents().find('form, #emojistatus');
    var n = '<div id="smile_panel" class="smile_panel">' + '<div class="sp_tabs">' + '<div class="sp_smile_tabs">' + '<div class="scroller_wrap">' + '<div class="sp_smile_cont scroller_cont">';
    var data = ssGet("smileCat");
    if (data === null) {
        $.post("/ajax?act=smile", function (a) {
            ssSet("smileCat", a);
        });
    }
    timerS = setInterval(function () {
        data = ssGet("smileCat");
        if (data === null || data.length <= 0) {
            return;
        }
        if (ssGet("smileTabId") === null) {
            ssSet("smileTabId", data[0].id);
        }
        for (var i = 0; i < data.length; i++) {
            n += '<span class="sp_tab sp_tab-' + data[i].id + ' sp_tab_inactive" onclick="return SmileButton.SelectTab(this, ' + data[i].id + ');"><img src="/template/icons/emojiCat/' + data[i].id + '.png" class="sp_icon"></span>';
        }
        n += "</div>" + "</div>" + "</div>" + '<div class="sp_body">' + '<div class="sp_cont">' + '<div class="scroller_cont">' + '<div class="sp_page">' + '<div class="smile_list"></div>' + "</div>" + "</div>" + "</div>" + "</div>" + "</div>";
        node.after(n);
        SmileButton.SelectTab(".sp_tab-" + ssGet("smileTabId"), ssGet("smileTabId"));
        clearInterval(timerS);
    }, 100);
});