var Script = function() {

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


        $('body').on('click', '.message .name', function(e) {
            e.preventDefault(); // prevent click event

            var name = $(this).text(); // get clicked user's full name
            input.val('@' + name + ':'); // set it into the input field
            App.scrollTo(input); // scroll to input if needed
        });

        var cont = $('#chats');
        var list = $('.chats', cont);
        var form = $('.chat-form', cont);
        var input = $('input', form);
        var btn = $('.btn', form);
        input.keypress(function(e) {
            if (e.which == 13) {

                var user_id = $('#get_user_id').data('id');
                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: "/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(from_id) {
        var id = 0;
        if ($('#general_content').is(':empty')) {
            var dataStringg = 'id=' + id + '&from_id=' + from_id;
        } else {
            id = $("li").last().data('id');
            var from_id = $('#get_user_id').data('id');
            var last_date = $('.date:last').data('id');
            var dataStringg = 'id=' + id + '&last_date=' + last_date + '&from_id=' + from_id;
        }
        if ((from_id === undefined || from_id === null)) {} else {
            $.ajax({
                type: "POST",
                url: "/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, 3500);
                }
            });

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

}();