function chat()
{
    this.pingTimeout = 4000; // 4 sec
    this.reloadTimeout = 8000;
    this.baseUrl = CURR_URL;

    $.ajaxSetup({dataType : "json"});

    this.initPing = function()
    {
        setInterval(this.ping, this.pingTimeout);
    }

    this.initReload = function()
    {
        setInterval(this.reloadPage, this.reloadTimeout);
    }

    this.ping = function()
    {
        var data = {
            'action': 'ping'
        };

        $.post('/ping.php', data,
            function(data){
                if (data.started) {
                    chat.ahtung();
                    $('#top').parent().before('<tr><td><h1 style="text-align:center;color:#D03C3D;">Çíàéäåíî êîðèñòóâà÷à, ùî î÷³êóº íà ä³àëîã â ÷àò³. Áóäü-ëàñêà, ïåðåéä³òü íà ñòîð³íêó ÷àòà.</h1></td></tr>');
                    if (isChatPage) {
                        window.location.reload();
                    }
                }
                if (chat.dialogLoaded()) {
                    if (data.dialog_id == "") {
                        chat.endDialog();
                    } else {
                        var html = '';
                        $.each(data.messages, function(i, message) {
                            html += chat.formatMessage(message.name, message.time, message.message);
                        });
                        if (html != "") {
                            $('#dialogOutput').html( $('#dialogOutput').html() + html);
                            chat.toBottom();
                        }
                    }
                }
            }
        );
    }

    this.ahtung = function() {
        var oldTitle = document.title;
        var msg = "Óâàãà!";
        var timeoutId = setInterval(function() {
            document.title = document.title == msg ? ' ' : msg;
        }, 1000);
        window.onmousemove = function() {
            clearInterval(timeoutId);
            document.title = oldTitle;
            window.onmousemove = null;
        };
    }

    this.formatMessage = function(name, time, message)
    {
        return '<h1><span>' + name + '</span> ' + time + ':</h1><div>' + message + '</div>';
    }

    this.send = function()
    {
        if ($('#dialogInput').val() != "") {
            var data = {
                'action': 'send',
                'message': $('#dialogInput').val()
            };
            $('#dialogInput').val('');
            $("#sbmt_btn").addClass("btn dsbld");
            $("#sbmt_btn").attr("disabled", "disabled");
            $.post(chat.baseUrl, data,
              function(data){
                var html = chat.formatMessage(data.name, data.time, data.message);
                $('#dialogOutput').html( $('#dialogOutput').html() + html);
                chat.toBottom();
              }
            );
        }
    }

    this.reloadPage = function()
    {
        window.location.reload();
    }

    this.toBottom = function ()
    {
        document.getElementById("dialogOutput").scrollTop=document.getElementById("dialogOutput").scrollHeight;
    }

    this.dialogLoaded = function()
    {
        if (document.getElementById("dialogOutput")) {
            return true;
        }
        return false;
    }

    this.endDialog = function()
    {
//        var data = {
//            'action': 'stop'
//        };
//        $.post('/ping.php', data,
//            function(data){
//            }
//        );
        $('#dialogInput').val("");
        $('#dialogInput').attr("disabled","disabled");

        $("#sbmt_btn").addClass("btn dsbld");
        $("#sbmt_btn").addClass("btn dsbld");

        $('#endLink').hide();
        $('#initLink').show();

        $('#buddyOffline').show();
        $('#buddyOnline').hide();
    }

}
function ctrlEnter(event, formElem)
{
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
        chat.send();
    }
}
document.onkeypress = ctrlEnter;

