var statusUpdateInterval = 1000; //Интервал проверки статуса
var checkStatusTID; //Указатель на таймер обнобления статуса
var chatURL = "getManagerStatus.php";// Адрес страницы к которой выполняются асинхронные запросы

var options = { //Параметры вызова ajax
    url: 'getManagerStatus.php',
    type: 'post',
    dataType: 'xml',
    success: readMessages,
    error: ajaxError,
    async: true,
    processData: true,
    timeout: 4000
};

$(function(){
    checkStatus();
});

function openChat(){
    var shift = {
            x: 0,
            y: 0,
            w: 460,
            h: 340
    }
    shift.x = ( screen.width - shift.w ) / 2;
    shift.y = ( screen.height - shift.h ) / 2;
    window.open("http://83.222.195.66/chat/clients/chat.php", "", "left="+shift.x +", top="+shift.y+", width="+shift.w+", height="+shift.h+", status=yes, resizable=no, location=no");
}
	

function checkStatus(){
    var data = '';
    data = {
        mode:  'getManagerStatus'
    }
    options.data = data;
    $.ajax(options);
    clearTimeout(checkStatusTID);
    checkStatusTID = setTimeout(checkStatus, statusUpdateInterval);
}

function readMessages(response){
     modeArray = response.getElementsByTagName("mode");
     switch(modeArray.item(0).firstChild.data){
        case 'getManagerStatus':
            statusArray = response.getElementsByTagName("status");
            status = statusArray.item(0).firstChild.data;
            if(status == 'offline'){
                $('#chat')[0].style.visibility = 'hidden';
            }
            if(status == 'online'){
                $('#chat')[0].style.visibility = 'visible';
            }
            break;
    }
}

function ajaxError(){
    clearTimeout(checkStatusTID);
    checkGetRecipientTID = setTimeout(checkGetRecipientTID, statusUpdateInterval);
}

