$(document).on('easy_IM_loaded', function() {
    window.EasyInstantMessenger.chatLi = $('#easy_servicebar').find('li:nth-child(5)');

    window.EasyInstantMessenger.openChat = function () {
        this.chatWindow = true;
        this.chatLi.append('<span id="easy_servicebar_component_beak" style="z-index: 901; right: 0;"><a id="easy_servicebar_close_toggler" class="icon-close" onclick="window.EasyInstantMessenger.closeChat()"></a></span>');
        $('#easy_instant_messages_wrapper').removeClass("easyim__wrapper--hidden");
        $('#easy_instant_messages_toggle').addClass("active");
        $('#easy_servicebar_component').css('display', 'none');
    };

    window.EasyInstantMessenger.closeChat = function () {
        this.chatWindow = false;
        this.chatLi.find("#easy_servicebar_component_beak").remove();

        window.clearInterval(this.interval);
        this.interval = window.setInterval(this.checkMessages, 70 * 1000, this);

        $('#easy_instant_messages_wrapper').addClass("easyim__wrapper--hidden");
        $('#easy_instant_messages_toggle').removeClass("active");
        this.displayReceivedMessagesCount();
    }
});