<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$

    ChatterWant.asp

    Een eindgebruiker wil een chat starten
    Als dat lukt wordt de echte chat in Chatter.asp uitgevoerd.
*/

%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="./chat.inc" -->
<%
FCLTHeader.Requires({plugins:["jQuery"], js: ["jquery-ui.js", "./notify.js"]})

var dis_key = getQParamInt("dis_key", -1); // discipline
var wantchannelId = shared.random();
var ins_discipline_omschrijving = get_disc_name(dis_key);

// TODO: als er geen chat_queue.accepting zijn dat direct melden?
Application.Lock();
var chat_queue = JSON.parse(Application(chat_queue_name) || "{}");
Application.UnLock();
%>
<html>
<head>
<%
   FCLTHeader.Generate();
%>
<script>
    if (window.name.match(/^ID.*/)) /* Om de startpagina uit te sluiten als de chat als widget gebruikt wordt */
    {
        FcltMgr.setTitle(L("lcl_chatter_outgoing").format("<%=ins_discipline_omschrijving%>"));
        FcltMgr.startEdit(window); // maakt hem hot
    }

    var hasChatOnline = 0;
    var wantchannelId = "<%=wantchannelId%>";
    var startMessage;
    var timerId;
    var dis_key = <%=dis_key%>;
    function chat_remove()
    {
        chat_doask("", 1); // remove
    }
    function chat_ask(remove)
    {
        var msg = startMessage = $("textarea#initialmsg").val();
        if (msg)
        {
            chat_doask(msg, remove);
        }
    }
    function chat_doask(msg, remove)
    {
        var parms = {
                channelId: wantchannelId,
                message: msg
            };
        if (dis_key > 0)
        {
            parms.dis_key = dis_key;
        }
        if (remove > 0)
        {
            parms.remove = 1;
        }
        $.post( "./ChatAsk.asp",
            parms,
            process_chat_ask);
    }
    function process_chat_ask(data)
    {
        if (data.removed == 1)
        {
            clearInterval(timerId);
            notify.stopwait();
            $("#initialmsg").attr("readonly", false).on("keydown", handleEnter);
            $("span.sendbutton").show();
            $("span.spinnerbutton, span.cancelbutton").hide();
            $("span.spinnerbutton").off("mouseenter");
            $("span.cancelbutton").off("mouseleave");
        }
        else
        {
            timerId = setInterval(sendQueueAlive, 15000);
            notify.wait(window.wantchannelId, null, chat_start);
            $("#initialmsg").attr("readonly", true).off("keydown");
            $("span.sendbutton").hide();
            $("span.spinnerbutton").show();
            $("span.spinnerbutton").on("mouseenter", handleSpinnerMouseenter);
            $("span.cancelbutton").on("mouseleave", handleCancelMouseleave);
        }
    }

    function chat_start(data)
    {   // onze wait channelId gaan we als read channelId hergebruiken
        clearInterval(timerId);
        window.location.href = "../chat/Chatter.asp?chatreadid=" + window.wantchannelId
                        + "&chatsendid=" + data.message
                        + "&message=" + escape(startMessage)
                        + (dis_key > 0 ? "&dis_key=" + dis_key : "");
        if (window.name.match(/^ID.*/)) /* Om de startpagina uit te sluiten als de chat als widget gebruikt wordt */
            FcltMgr.setTitle(L("lcl_chatter_outgoing").format("<%=ins_discipline_omschrijving%>"));
        return true;
    }
<%
    // Laat iedereen weten dat de queue is gewijzigd.
    // Geen schoonheidsprijs, kan dit ook serverside gedaan worden?
    for (acceptchannelId in chat_queue.accepting)
    {
        Response.Write("\nnotify.signal('{0}');".format(acceptchannelId));
    }
%>
    function sendQueueAlive(init)
    {
        $.getJSON( "ChatQueueAlive.asp?channelId=" + wantchannelId
                 , function(data)
                    {   // Kijk opnieuw of er nog medewerkers aanwezig zijn om de chat te beantwoorden.
                        hasChatOnline = data.hasChatOnline;
                        handleChatOnline(init);
                    }
                 );
    }
    function handleEnter(event)
    {
        if (event.keyCode == 13 && !event.shiftKey)
        {
            event.preventDefault();
            chat_ask();
        }
    }
    function handleSpinnerMouseenter(event)
    {
        $(this).hide();
        $("span.cancelbutton").show();
    }
    function handleCancelMouseleave(event)
    {
        $(this).hide();
        $("span.spinnerbutton").show();
    }
    function handleChatOnline(init)
    {
        if (hasChatOnline > 0)
        {
            if (init)
            {
                $("#chatmsg").text("<%=L("lcl_chat_initialmsg")%>");
                $("span.spinnerbutton, span.cancelbutton").hide();
                $("#initialmsg").autogrow().focus().on("keydown", handleEnter);
            }
            else
            {
                $("#chatmsg").text("<%=L("lcl_chat_waiting")%>");
            }
        }
        else
        {
            $("#chatmsg").text("<%=L("lcl_chat_no_listeners")%>");
            $("div.text-wrapper").hide();
        }
        FcltMgr.resized();
    }

    $(function () {
            sendQueueAlive(true);
        });

</script>
</head>
<body>
    <% PAGE_START(); %>

    <%BLOCK_START("chat", L("lcl_chatter_outgoing").format(ins_discipline_omschrijving), { icon: "fa-user-headset", center: true });%>

        <div class="chatdisclaimer"><%=L("lcl_chat_disclaimer").format(ins_discipline_omschrijving)%></div>
        <div class="chatinitialmsg" id="chatmsg"></div>
        <div class="text-wrapper">
            <textarea id="initialmsg" class="fldtxt" placeholder='<%=L("lcl_chat_initialmessage")%>'></textarea>
            <span class="sendbutton" onclick="chat_ask()" title="<%=L("lcl_chat_waiting").format(wantchannelId)%>">
                <%=I("fa-paper-plane fa-lg")%>
            </span>
            <span class="spinnerbutton" title="<%=L("lcl_chat_waiting").format(wantchannelId)%>">
                <%=I("fa-fclt-spinner fa-lg fas")%>
            </span>
            <span class="cancelbutton" onclick="chat_remove()" title="<%=L("lcl_chat_waiting").format(wantchannelId)%>">
                <%=I("fa-times fa-lg")%>
            </span>
        </div>

    <%BLOCK_END();%>
    <% PAGE_END(); %>
</body>
</html>
<% ASPPAGE_END(); %>
