Файл: chat/js/chat_scripts.js
Строк: 101
<?php
/*
 * PHP+jQuery+MySQL Chat, version 0.2
 * chat_scripts.js
 * http://i-wanna-think.ru/
 *
 * Copyright 2013, Ivan Tkachenko
 * Date: 2013-04-29
 */
 
 $(document).ready(function (){
                            
    // сохраняем стандартный title
    if (document.title!="")
    {
        var old_title = document.title;
    }
    
    $(window).blur(function() 
    {
        // Покидаем вкладку с чатом
        $('#chat_body').attr({'alt':'not_active'});
    });
    
    $(window).focus(function()
    {
        // Возврат к вкладке с чатом
        $('#chat_body').attr({'alt':'active'});
    });
    
    // делаем фокус на поле ввода при загрузке страницы
    if ($("#chat_text_input").size()>0)
    {
        $("#chat_text_input").focus();
    }
    
    
    // отправка сообщения
    function send_message()
    {
        var message_text = $('#chat_text_input').val();
        if (message_text!="")
        {
            $.ajax(
            {
                url: 'functions/chat_scripts.php',
                type: 'POST',
                data: 
                {
                    'action': 'add_message',
                    'message_text': message_text
                },
                dataType: 'json',
                success: function (result) 
                {            
                    $('#chat_text_input').val('');
                    get_chat_messages(); 
                } // конец success
            }); // конец ajax
        }
    }
    
    function get_chat_messages () 
    {    
        if ($('#block').val() == 'no')
        {
            $('#block').val('yes');
            var last_act = $('#last_act').val();
            $.ajax(
            {
                url: 'functions/chat_scripts.php',
                type: 'POST',
                data: 
                {
                    'action': 'get_chat_message',
                    'last_act': last_act
                },
                dataType: 'json',
                success: function (result) 
                {
                    $('#block').val('no');
                    
                    
                    if (result.its_ok == 1)
                    {
                        $('#chat_text_field').append(result.message_code);
                        $('#last_act').val(result.last_act);
                        $('#chat_text_field').scrollTop($('#chat_text_field').scrollTop()+100*$('.chat_post_my, .chat_post_other').size());    
                        
                        
                        if ($('#chat_body').attr('alt') == 'not_active')
                        {
                            // звуковое уведомление о новом сообщении
                            $("#jpId").jPlayer( 
                            {
                                ready: function () 
                                {
                                    $(this).jPlayer("setMedia", 
                                    {
                                        mp3: 'sounds/new_mess.mp3'
                                    }).jPlayer("play");
                                },
                                solution: "html, flash",
                                swfPath: "flash",
                                supplied: "mp3",
                                volume: 1
                            });
                            $("#jpId").jPlayer("play");
                            
                            //уведомление о новом сообщении в неактивной вкладке в title
                            var title_anim = setInterval(function() 
                            {
                                if ($('#chat_body').attr('alt') == 'not_active')
                                {
                                    setTimeout(function() { document.title = "Новое сообщение"; },250);
                                    setTimeout(function() { document.title = "Новое сообщение"; },500);
                                    setTimeout(function() { document.title = "Новое сообщение"; },750);
                                    setTimeout(function() { document.title = "Новое сообщение"; },1000);
                                    setTimeout(function() { document.title = "Новое сообщение"; },1250);
                                    setTimeout(function() { document.title = "Новое сообщение"; },1500);
                                    setTimeout(function() { document.title = "Новое сообщение"; },1750);
                                }
                                else
                                {
                                    clearInterval(title_anim);
                                    document.title = old_title;
                                }
                            }, 2200);
                            ///////////////////////////////////
                        }
                    }    
                    
                    
                    
                    
                    
                } // конец success
            }); // конец ajax    
        }
        
    }
    
    $('#chat_text_input').keydown(function(event)
    {
        if (event.which == 13 && !event.shiftKey)
        {
            event.preventDefault();
            send_message();
        }
    });
    
    $('#chat_button').click(function() 
    {
        send_message();
    });
    
    $('#logout_button').click(function() 
    {
        window.location.href = 'index.php?logout';
    });
    
    
    setInterval(function() 
    {
        get_chat_messages();
    }, 2000);
    
    
    $('#chat_text_field').scrollTop($('#chat_text_field').scrollTop()+1000*$('.chat_post_my, .chat_post_other').size());
    
    
    
}); // конец ready
?>