function initChatUI() {
    buddyListDialog.init();
    buddyListDialog.openWindow();
    chatAlerts.init();
    chatDialog.init();
    getMessages(1);
    getAlerts(1);
}

function getMessages(firstRequest)
{
    var data = (firstRequest == 1 ? {REQ_ID: 'GET_CHAT_MSGS', GetNotReaded: '1'} : data = {REQ_ID: 'GET_CHAT_MSGS'});
    $.ajax({type: 'POST', url: '/chat/TakeATourByRequest.php', datatype: 'xml', data: data, success:
        function(data)
        {
            $('Message', data).each(function()
                {
                    d = new Date(), date = d.getFullYear() + '-' + setZero(d.getMonth()+1) + '-' + setZero(d.getDate()) + ' ' + setZero(d.getHours()) + ':' + setZero(d.getMinutes()) + ':' + setZero(d.getSeconds());
                    buddyListDialog.setMessage(this);
                });
            setTimeout('getMessages(0)', 5000);
        },
        error: function(xhr, ajaxOptions, thrownError)
        {
            setTimeout('getMessages(' + firstRequest + ')', 5000);
        }
    });
}

function getAlerts(firstRequest)
{
    var data = (firstRequest == 1 ? {REQ_ID: 'GET_ALERTS', GetNotReaded: '1'} : {REQ_ID: 'GET_ALERTS'});

    // Update buddy list
    buddyListDialog.openWindow();
    $.ajax({type: 'POST', url: '/chat/TakeATourByRequest.php', datatype: 'xml', data: data, success:
        function(data)
        {
            $('alert', data).each(function()
                {
                    var type = $(this).attr('requestname');

                    if ( type == 'GiftClip') {
                        buddyListDialog.setAlert(this);
                    } else {
                        buddyListDialog.setAlert(this);
                    }
                });

            setTimeout(getAlerts, 20000);
        },
        error: function(xhr, ajaxOptions, thrownError)
        {
            setTimeout(getAlerts, 20000);
        }
    });
}

function setZero(data)
{
    if (data < 10) return '0' + data;
    return data;
}

