var chatPollCount;
var chatPollTimerID;
var chatPageLoadTimestamp;
var chatLatestTS;
var chatRoomID;
var chatEnabled = false;
var chatHasFocus = true;
chatInit();

attachEventHandlerFunction('blur',  window, pageBlurChat );
attachEventHandlerFunction('focus', window, pageFocusChat );
function pageBlurChat(e) {
    chatHasFocus = false;
}
function pageFocusChat(e) {
    chatHasFocus = true;
    chatReset(true);
}

function chatInit() {
    chatPageLoadTimestamp = new Date(); 
    clearTimeout(chatPollTimerID);
    chatPollCount = 0;
    chatPollTimerID = setTimeout(chatPoll, chatGetPollInterval() );
}
function chatReset(force) {
    force = (typeof force == 'undefined') ? false : force; 
     
    chatPageLoadTimestamp = new Date(); // reset because user is on page
    if (force || (chatGetPollInterval() > 10000) ) {
        clearTimeout(chatPollTimerID);
        chatPollCount = 0;
        chatPollTimerID = setTimeout(chatPoll, 1000 ); // give a short time to process any previous ajax
    } else {
        chatPollCount = 0;
    }
}
function chatPause() {
    clearTimeout(chatPollTimerID);
}
function chatPoll() {
    if (chatLatestTS == -1 || !chatEnabled || !chatHasFocus) return; // chatLatestTS == -1: room is full
    var dtNow = new Date();
    var minutesOnPage = (dtNow.getTime() - chatPageLoadTimestamp.getTime()) / (60*1000);
    if (document.getElementById('chat_messages') && (minutesOnPage < 60) ) { // make sure chat is visible and page not left open
        chatPollCount++;
        processAjax(true,'get','/pages/chat/chat.ajax.php','rid='+chatRoomID+'&ts='+chatLatestTS+'&max_msgs='+chatMaxMsgs);
    }
    chatPollTimerID = setTimeout(chatPoll, chatGetPollInterval() );
}
function chatGetPollInterval() {
    var initialPollIntervalSeconds = 5; //5
    var pollInterval = 1000 * initialPollIntervalSeconds * (1 + Math.pow(chatPollCount/10, 8)); //10,8
    return(pollInterval);
}
function chatPostText() {
    if (chatLatestTS == -1) return; // room is full
    var elmtChatText = document.getElementById('chat_text');
    if(elmtChatText && elmtChatText.value.trim() != '' ) {
        processAjax(true,'get','/pages/chat/chat.ajax.php','text='+encodeURIComponent(elmtChatText.value)+'&rid='+chatRoomID,'chat_img_ajax');
        elmtChatText.value = '';
    }
    chatReset(true);
}
function chatPostName(e) {
    var newName=prompt( txtEdit+':','');
    if(newName!=null) {
        processAjax(true,'get','/pages/chat/chat.ajax.php','name='+encodeURIComponent(newName)+'&rid='+chatRoomID,'chat_img_ajax');
        chatReset(true);
    }
    return(preventDefaultEvent(e));
}
function chatChangeRoom(newRoomID) {
    processAjax(false,'get','/pages/chat/chat.ajax.php', 'change_room=1&max_msgs='+chatMaxMsgs+'&old_rid='+chatRoomID+'&rid='+newRoomID,'chat_img_ajax');
    chatReset(true);
}
function chatUsersView(e) {
    chatPause();
    processAjax(true,'get','/pages/chat/chat.ajax.php','users=1&rid='+chatRoomID,'chat_img_ajax');
    return(preventDefaultEvent(e));
}
function chatUsersReturn(e) {
    chatChangeRoom(chatRoomID);
    return(preventDefaultEvent(e));
}
