var Script = function() {

    $(document).ready(function(e) {
        $(".menu-toggler").trigger('click');


        var cont = $('#chats');
        var getLastPostPos = function() {
            var height = 0;
            cont.find(".in:last").each(function() {
                height = height + $(this).outerHeight();
            });

            return height;
        }

        var bottomCoord = $('.chats').prop('scrollHeight') + 'px';
        $('.chats').slimScroll({
            scrollTo: bottomCoord
        });
    });

    function startRefresh() {
        var id = 0;
        if ($('#general_content').is(':empty')) {

            var dataStringg = 'id=' + id;
        } else {
            id = $('.in:last').data('id');
            var last_date = $('.date:last').data('id');
            var dataStringg = 'id=' + id + '&last_date=' + last_date;
        }
        $.ajax({
            type: "POST",
            url: "/messenger/get-general",
            data: dataStringg,
            beforeSend: function() {
                $(".unread").remove();
            },
            success: function(data) {
                var trimmed = $.trim(data);
                var cont = $('#chats');

                $("#general_content").append(trimmed);

                setTimeout(startRefresh, 30000);
            }
        });

    };
    $(function() {
        startRefresh();
    });

}();