var chatLinkArray = new Array();

// Node ID		= clientId van chatlink in html
// FactsheetGUID	= opleiding guid
// Instelling		= kortenaam instelling = accountid
// Isat			= opleiding id = agentid
// ChatLinkControlId = ID van de gehele chatlink. Gebruikt om unieke id's voor de divs te maken.
function chatLink(nodeId, factsheet, instelling, isat, chatLinkControlId) {
    this.Id = nodeId;
    this.FactsheetGUID = factsheet;
    this.Instelling = instelling;
    this.Isat = isat;
    this.ChatLinkControlId = chatLinkControlId;
}

function SetChatStateForChatButton(elOnline, elOffline, instelling) {

    var returnBool = false;
    $.ajax({
        type: "GET",
        dataType: 'json',
        url: "/proxy/cb_AgentProxy.aspx?i=" + instelling + "&c=getOnlineAgents" + "&random=" + Math.random(), //Math.Random is a work around for IE caching
        data: "",
        success: function (data) {
            $.each(data.getOnlineAgents, function (i, agent) {
                returnBool = ((returnBool) || (agent.status == 'online'));
            });

            if (returnBool) {
                $(elOnline).show();
                $(elOffline).hide();
            }
            else {
                $(elOnline).hide();
                $(elOffline).show();
            }
        }, //success function
        error: function (xhr, textStatus, errorThrown) {
            $(elOnline).hide();
            $(elOffline).show();
        } //error function
    });   //ajax
}

//Requests status and sets elements by "#id" or ".classname" for instelling
function SetChatState(chatLinkClientId, instelling) {

    var returnBool = false;
    $.ajax({
        type: "GET",
        dataType: 'json',
        url: "/proxy/cb_AgentProxy.aspx?i=" + instelling + "&c=getOnlineAgents" + "&random=" + Math.random(),  //Math.Random is a work around for IE caching
        data: "",
        success: function (data) {
            $.each(data.getOnlineAgents, function (i, agent) {
                returnBool = ((returnBool) || (agent.status == 'online'));
            });

            if (returnBool) {
                SetChatStateOnline(chatLinkClientId);
            }
            else {
                SetChatStateOffline(chatLinkClientId, 'Chat inactief', 'Chat inactief');
            }
        }, //success function
        error: function (xhr, textStatus, errorThrown) {
            SetChatStateOffline(chatLinkClientId, 'Chat inactief', 'Chat inactief');
        } //error function
    });    //ajax
}

function SetChatStatePending(chatLinkClientId) {
    altTekst = 'Even geduld s.v.p.';
    $(chatLinkClientId).attr('title', altTekst);
    $(chatLinkClientId).text(altTekst);

    // Reset classes and set the new two classes.
    $(chatLinkClientId).attr('class', 'btnStartChat chatDisabled');
}

function SetChatStateOnline(chatLinkClientId) {
    altTekst = 'Chat nu!';

    $(chatLinkClientId).attr('title', altTekst);
    $(chatLinkClientId).text(altTekst);

    // Reset classes and set the correct class.
    $(chatLinkClientId).attr('class', 'btnStartChat');
}

function SetChatStateOffline(chatLinkClientId, tekst, altTekst) {

    $(chatLinkClientId).attr('title', altTekst);
    $(chatLinkClientId).text(tekst);

    $(chatLinkClientId).removeAttr('href');

    // Reset classes and set the new two classes.
    $(chatLinkClientId).attr('class', 'btnStartChat chatDisabled');
}

//fun
function parseCobrowserLinks() {

    for (var i = 0; i < chatLinkArray.length; i++) {
        var singleChatLink = chatLinkArray[i];
        var chatLinkClientId = "#" + singleChatLink.Id;
        SetChatStatePending(chatLinkClientId);
    }

    for (var i = 0; i < chatLinkArray.length; i++) {
        var singleChatLink = chatLinkArray[i];
        setChatUrl(singleChatLink);
        var chatLinkClientId = "#" + singleChatLink.Id;
        SetChatState(chatLinkClientId, singleChatLink.Instelling);
    }
}

// Find the chat-link and set it's URL.
function setChatUrl(chatlink) {
    var el = document.getElementById(chatlink.Id);
    if (el) {
        var link = getChatUrl(chatlink);
        el.href = link;
    }
}

// Builds the URL to the chat, based on wheter the user is logged in.
function getChatUrl(chatlink) {
    var factSheetUrl = ChatConfig.FactsheetUrl + "?ovid=" + chatlink.FactsheetGUID + "&od=true&chat=true"
    var returnvalue = "";
    if (ChatConfig.IsLoggedIn) {
        returnvalue = factSheetUrl;
    }
    else {
        returnvalue = ChatConfig.RedirectPage;
        returnvalue += '?ref=' + escape(factSheetUrl);
    }
    return returnvalue;
}
