﻿var LINK;
function InitializeChatFunctionality(sips, service, link) {
    LINK = link;
    //if hfChatSips is empty, user is not logged in or this page is not in the appropriate project to allow chats
    if (sips !== '') {
        $.availability({
            'serviceUrl': service,
            'refreshRate': 30000,
            'sips': sips,
            'onAvailable': function () {
                $(".jsChatButton").show();
                if ($.cookie("ShowChatButton") !== "false") {
                    $(".jsChatPopupModal").show();
                }
            },
            'onNotAvailable': function () {
                $(".jsChatButton").hide();
                $(".jsChatPopupModal").hide();
            }
        });
        if ($.cookie("ShowChatButton") !== "false") {
            $(".jsChatPopupModal").addClass("smallChat");
            $(document).oneTime(10000, function () {
                $(".jsChatPopupModal").animate({ height: '+=63' }, 1500);
                $(".jsChatPopupModal").removeClass("smallChat");
            });
            $(document).oneTime(20000, function () {
                $(".jsChatPopupModal").animate({ height: '-=63' }, 1500);
                $(document).oneTime(1500, function () {
                    $(".jsChatPopupModal").addClass("smallChat");
                });
            });
        }
    }
}

$(document).ready(function () {
    $(".jsChatButton").hide();
    $(".jsChatPopupModal").hide();

    $('.jsRemoveChat').click(function () {
        $.cookie("ShowChatButton", "false", { path: '/', expires: 7 });
        $(".jsChatPopupModal").hide();
    });
    //Build Chat popup link
    $(".jsChat").click(function () {
        var url = LINK;
        if (LINK !== '') {
            var newwindow = window.open(url, 'chatclient', 'menubar=no,location=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,height=497,width=427');
            newwindow.focus();
        }
    });
});

(function ($) {
    $.availability = function (options) {
            var settings = {
            'serviceUrl': '',
            'refreshRate': 30000,
            'sips': '',
            'onAvailable': function () { },
            'onNotAvailable': function () { }
        };
        if (options) {
            $.extend(settings, options);
        }
        //execute refreshPresence() every *settings.refreshRate* seconds
        var page = $("html");        
        page.everyTime(settings.refreshRate, function () {
            refreshPresence(settings);
        });
        //force immediate execution (otherwise the system will wait *settings.refreshRate* seconds to execute the first time.)
        refreshPresence(settings);
    };
    function refreshPresence(settings) {
        $.ajax({
            type: 'GET',
            url: settings.serviceUrl + 'IsAnyCreditRepAvailable?sipUris=' + settings.sips + '&r=' + Math.random(),
            dataType: 'jsonp',
            success: function (chatServiceResponse) {
                if (chatServiceResponse.HasError == true) {
                    $.disposeTimer();
                    settings.onNotAvailable.call(this);
                } else if (chatServiceResponse.IsAvailable == true) {
                    settings.onAvailable.call(this);
                } else {
                    settings.onNotAvailable.call(this);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                settings.onNotAvailable.call(this);
            }
        });
    }
    $.disposeTimer = function () {
        var page = $("html");
        page.stopTime();
    }
})(jQuery);

