
;(function ( $, window, document, undefined ) {

    var pluginName = "queueMonitor",
        defaults = {
//            jabberPresence: -1,
//            jabberStatusText: "",
        };

    function Plugin( element, options ) {
        this.element = element;

        this.options = $.extend( {}, defaults, options );
        this.updates = options;

        this._defaults = defaults;
        this._name = pluginName;

        this.init();
    }

    Plugin.prototype = {

        init: function() {
           this.updateProperties(this.element, this.updates);
        },
        
        updateProperties: function(el, updates) {
            if(updates.currentCalls !== undefined) this.updateCurrentCalls(el, updates);
            if(updates.currentPendingCalls !== undefined) this.updateCurrentPendingCalls(el, updates);
            if(updates.currentAgents !== undefined) this.updateCurrentAgents(el, updates);
            if(updates.agents !== undefined) this.updateFreeAgents(el, updates);
            
            this.updatePhonePresenceStatus(updates);
            this.updateJabberPresenceStatus(updates);
        },
        
        updateCurrentCalls: function (el, updates)
        {
            $(".queue-monitor-currentCalls").text(updates.currentCalls);
        },
        
        updateCurrentPendingCalls: function (el, updates)
        {
            $(".queue-monitor-currentPendingCalls").text(updates.currentPendingCalls);
        },
        
        updateCurrentAgents: function (el, updates)
        {
            $(".queue-monitor-currentAgents").text(updates.currentPendingCalls);
        },
        
        updateFreeAgents: function (el, updates)
        {
            var free = 0;
            $.each(updates.agents, function (i,el) {
                
                
                var agentIsLoggedIn = $("#memberId_"+el.agent_id).hasClass("btn-danger");
                
                if ((el.status === "free" || el.status === "ringing") && agentIsLoggedIn) {
                    
                    free = free+1;
                }
            });
            $(".queue-monitor-freeAgents").text(free);
        }, 
        updatePhonePresenceStatus: function (updates)
        {
            $.each(updates.agents, function (index, element) {
                
                var color = getColorforAgentPhoneStatus(element.status);
                
                $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-phone").removeClass("in").addClass("fade out");
                
                setTimeout(function () {
                    $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-phone").css("color",color);
                    $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-phone").addClass("in").removeClass("out");
                }, 200);
            });
        }
        //TEST FUNCTION !
        ,
        updateJabberPresenceStatus: function (updates)
        {
            $.each(updates.agents, function (index, element) {
                
                var color = getColorforAgentJabberStatus(element.jabberStatus);
                $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-comment").removeClass("in").addClass("fade out");
                
                setTimeout(function () {
                    $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-comment").css("color",color);
                    $("input[type='hidden'][value='"+element.agent_id+"']").parents("tr").find(".icon-comment").addClass("in").removeClass("out");
                }, 200);
            });
        }
    };
    //TEST FUNCTION !
    function getColorforAgentJabberStatus(status) {
        if (status === "free") {
            return "green";
        } else if (status === "busy") {
            return "darkred";
        } else if (status === "ringing") {
            return "green";
        }
        return "gray";
    }
    
    function getColorforAgentPhoneStatus(status) {
        if (status === "free") {
            return "green";
        } else if (status === "busy") {
            return "darkred";
        } else if (status === "ringing") {
            return "orange";
        }
        return "gray";
    }

    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, "plugin_" + pluginName)) {
                $.data(this, "plugin_" + pluginName, new Plugin( this, options ));
            } else {
                return $.data(this, "plugin_" + pluginName).updateProperties(this, options);
            }
        });
    };

})( jQuery, window, document );