var pageUrl = location.href;

var lastChatInviteId;
var lastChatInviteRoomId;

var mainRequest;
var nmi;

function showPrivateChatNotification(inviterName, inviterId, invitationId, chatRoomId, senderProfileUrl) {
    var notification = document.createElement("div");
    notification.className = "chatinvitebox";
    notification.setAttribute("id", "chatinvitebox");
    var prompt = document.createElement("div");
    prompt.className = "chatinviteprompt";
    prompt.setAttribute("id", "chatinviteprompt");
    
    var options = document.createElement("div");
    options.className = "chatinviteoptions";
    options.setAttribute("id", "chatinviteoptions");
    prompt.appendChild(document.createTextNode("You have a private chat request from "));
    
    var senderLink = document.createElement("a");
    senderLink.href = senderProfileUrl;
    senderLink.appendChild(document.createTextNode(inviterName));
    prompt.appendChild(senderLink);    
    notification.appendChild(prompt);
    
    var acceptBtn = document.createElement("input");
    acceptBtn.setAttribute("type", "button");
    acceptBtn.setAttribute("value", "Accept");
    acceptBtn.onclick = acceptPrivateChat;
    options.appendChild(acceptBtn);
    var rejectBtn = document.createElement("input");
    rejectBtn.setAttribute("type", "button");
    rejectBtn.setAttribute("value", "Ignore");
    rejectBtn.onclick = rejectPrivateChat;    
    options.appendChild(rejectBtn);
    notification.appendChild(options);
    
    var navBox = document.getElementById("navbox");
    navBox.appendChild(notification);    
}

function hidePrivateChatNotification() {
    var notification = document.getElementById("chatinvitebox");
    if (notification != null) {
        notification.parentNode.removeChild(notification);
    }
}

function processResponse() {
    if (mainRequest.readyState == 4) {
        if (mainRequest.status == 200) {
            if (mainRequest.responseText.length > 0) {  
                var response = parseJSON(mainRequest.responseText);
                var newMessageCount = parseInt(response.newMessageCount);
                var unreadMessageCount = parseInt(response.unreadMessageCount);
                var n = document.createTextNode(unreadMessageCount);
                nmi.removeChild(nmi.childNodes[0]);
                nmi.appendChild(n);
                if (newMessageCount > 0) {
                    if (pageUrl.indexOf("messages.php") != -1 || pageUrl.indexOf("messages.php?view=outbox") != -1) {
                        location.reload(true);
                    }
                }

                if ("lastChatInvite" in response) {                       
                    var notification = document.getElementById("chatinvitebox");
                    if (notification == null || response.lastChatInvite.inviteId != lastChatInviteId) {
                        lastChatInviteId = response.lastChatInvite.inviteId;
                        lastChatInviteRoomId = response.lastChatInvite.chatRoomId;
                        senderName = response.lastChatInvite.senderName;
                        senderId = response.lastChatInvite.senderId;                        
                        senderProfileUrl = response.lastChatInvite.senderProfileUrl; 
                        showPrivateChatNotification(senderName, senderId, lastChatInviteId, lastChatInviteRoomId, senderProfileUrl);
                    }
                } else {
                    hidePrivateChatNotification();
                }
            }
        }
    }
}

function checkin() {
    mainRequest.open('POST', checkinUrl, true);
    mainRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    mainRequest.onreadystatechange = processResponse;
    mainRequest.send("action=checkin");
}

function initCheckin() {
    nmi = document.getElementById('newmessageindicator');
    mainRequest = getXHR(); 
    checkin();
    window.setInterval(checkin, 10000);
}

if (pageUrl.indexOf("chatroom.php") == -1) {
    window.onload = initCheckin;
}

function acceptPrivateChat() {
    var request = getXHR();
    request.open('POST', checkinUrl, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                if (request.responseText.length > 0) {  
                    var response = parseJSON(request.responseText);                    
                    request = null;
                    window.location = response.chatURL + '?id=' + lastChatInviteRoomId;
                }
            }
        }
    }
    request.send("action=acceptinvite&inviteid=" + lastChatInviteId);
}

function rejectPrivateChat() {
    var request = getXHR();
    request.open('POST', checkinUrl, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                hidePrivateChatNotification();     
                request = null;
            }
        }
    }
    request.send("action=rejectinvite&inviteid=" + lastChatInviteId);
}


