﻿var invitationTimer;

if (window.attachEvent) {
    window.attachEvent("onload", setTimer);
    window.attachEvent("onscroll", scrollInvitations);
}
else {
    window.addEventListener("load", setTimer, false);
    window.addEventListener("scroll", scrollInvitations, false);
}

function setTimer() {
    invitationTimer = setInterval("getInvitations();", 5000);
}

function getInvitations() {
    AjaxChat.InstantMessengerService.GetInvitations(onGetInvitationsComplete);
}

function onGetInvitationsComplete(result) {
    var invitationsDiv = document.getElementById('invitationsDiv');

    if (result == null) {
        invitationsDiv.style.display = 'none';
        return;
    }


    invitationsDiv.innerHTML = '';

    for (var i = 0; i < result.length; i++) {
        var div = document.createElement("div");
        var a = document.createElement("a");
        var accept = document.createElement("a");
        var busy = document.createElement("a");
        var block = document.createElement("a");
        var ignore = document.createElement("a");
        var br = document.createElement("br");
        var fromUserName = result[i].FromUser;
        var sessionId = result[i].ID;

        div.id = "invitation_" + fromUserName;
        div.className = "invitationdetails";
        a.className = "invitaionusername";
        accept.className = 'invitationbutton underline';
        busy.className = 'invitationbutton underline';
        block.className = 'invitationbutton underline';
        ignore.className = 'invitationbutton underline';

        a.innerHTML = result[i].FromUserPhoto + newMessageLabel + fromUserName;
        a.href = "javascript:popup('" + "AjaxChat/ChatWindow.aspx?toUsername=" + escape(fromUserName) + "', 'chat_" + fromUserName + "', 879, 710);";
        a.onclick = function() {
            AjaxChat.InstantMessengerService.SetNew(sessionId, false);
        }
        div.appendChild(a);

        div.appendChild(br);

        accept.innerHTML = acceptLabel;
        accept.href = "javascript:popup('" + "AjaxChat/ChatWindow.aspx?toUsername=" + escape(fromUserName) +
        "', 'chat_" + fromUserName.replace(' ', '___') + "', 790, 710);";
        accept.onclick = function() {
            AjaxChat.InstantMessengerService.SetNew(sessionId, false);
        }
        div.appendChild(accept);

        busy.innerHTML = busyLabel;
        busy.href = "javascript:void(0);";
        busy.onclick = function() {
            AjaxChat.InstantMessengerService.SetBusy(sessionId, fromUserName);
            document.getElementById("invitation_" + fromUserName).style.display = 'none';
        }
        div.appendChild(busy);

        block.innerHTML = blockLabel;
        block.href = "javascript:void(0);";
        block.onclick = function() {
            AjaxChat.InstantMessengerService.BlockUser(sessionId, fromUserName);
            document.getElementById("invitation_" + fromUserName).style.display = 'none';
        }
        div.appendChild(block);

        ignore.innerHTML = ignoreLabel;
        ignore.href = "javascript:void(0);";
        ignore.onclick = function() {
            AjaxChat.InstantMessengerService.Ignore(sessionId, fromUserName);
            document.getElementById("invitation_" + fromUserName).style.display = 'none';
        }
        div.appendChild(ignore);
        
        invitationsDiv.appendChild(div);
    }

    if (result.length > 0) {
        invitationsDiv.style.display = 'block';
    }
    else {
        invitationsDiv.style.display = 'none';
    }
}
function scrollInvitations() {
    
    
    var invitation = document.getElementById('invitationsDiv');

    if (invitation) {
        invitation.style.top = document.documentElement.scrollTop + 'px';
    }
}
