var Script = function() {

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

        var cont = $('#chats');
        var list = $('.chats', cont);
        var form = $('.chat-form', cont);
        var input = $('input', form);
        var btn = $('.btn', form);
        var user_id = $('#chats').data('id');
        input.keypress(function(e) {
            if (e.which == 13) {
                var text = input.val();
                if (text.length == 0) {
                    return;
                }
                sendChat(text, user_id);
            }
        });
        $('#submit').click(function(e) {
            e.preventDefault();
            var text = input.val();
            if (text.length == 0) {
                return;
            }
            sendChat(text, user_id);
        });

    });

    function sendChat(text, user_id) {
        var dataStringg = 'user_id=' + user_id + '&text=' + text;
        $.ajax({
            type: "POST",
            url: "/messenger/send-help",
            data: dataStringg,
            success: function(data) {
                var cont = $('#chats');
                var form = $('.chat-form', cont);
                var input = $('input', form);
                input.val('');
                var trimmed = $.trim(data);
                $("#general_content").append(trimmed);
                var getLastPostPos = function() {
                    var height = 0;
                    cont.find("li.out, li.in").each(function() {
                        height = height + $("#general_content").outerHeight();
                    });

                    return height;
                }

                cont.find('.scroller').slimScroll({
                    scrollTo: getLastPostPos()
                });
            }
        });
    }

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

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

                $("#general_content").append(trimmed);
                var getLastPostPos = function() {
                    var height = 0;
                    cont.find("li.out, li.in").each(function() {
                        height = height + $("#general_content").outerHeight();
                    });

                    return height;
                }

                cont.find('.scroller').slimScroll({
                    scrollTo: getLastPostPos()
                });
                setTimeout(startRefresh, 30000);
            }
        });

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

}();