﻿

function QueueManager() {

    this.slowAjaxQueue;
    this.fastAjaxQueue;
    this.currentFastProcessItem;
    this.ajaxRunningSlow;
    this.ajaxRunningFast;
    this.currentSlowProcessItem;

    slowAjaxQueue = new Array();
    fastAjaxQueue = new Array();
    ajaxRunningSlow = false;
    ajaxRunningFast = false;

    if (typeof (_queueManager_prototype_called) == 'undefined') {
        _queueManager_prototype_called = true;

        QueueManager.prototype.AddToSlowQueue = AddToSlowQueue;
        QueueManager.prototype.AddToFastQueue = AddToFastQueue;

        QueueManager.prototype.SlowQueueSuccess = SlowQueueSuccess;
        QueueManager.prototype.SuccessFastQueue = SuccessFastQueue;

        QueueManager.prototype.SendSlow = SendSlow;
        QueueManager.prototype.SendFast = SendFast;

        QueueManager.prototype.AjaxProcessingSlow = AjaxProcessingSlow;
        QueueManager.prototype.AjaxProcessingFast = AjaxProcessingFast;
        QueueManager.prototype.IsIdInSlowQueue = IsIdInSlowQueue;
        QueueManager.prototype.IsProcessIdInFastQueue = IsProcessIdInFastQueue;

        QueueManager.prototype.queueSlowProcessStart = queueSlowProcessStart;
        QueueManager.prototype.queueFastProcessStart = queueFastProcessStart;

        QueueManager.prototype.SlowQueueError = SlowQueueError;
        QueueManager.prototype.ErrorFastQueue = ErrorFastQueue;
    }
}

function AddToSlowQueue(processItem) {
    slowAjaxQueue.push(processItem);
    SendSlow();
}

function AddToFastQueue(processItem) {

    if ((fastAjaxQueue.length > 0) && (slowAjaxQueue.length == 0)) {
        slowAjaxQueue.push(processItem);
        SendSlow();
    }
    else {
        fastAjaxQueue.push(processItem);
        SendFast();
    }
}

function SendSlow() {
    if (ajaxRunningSlow == true)
        return;
    ajaxRunningSlow = true;
    AjaxProcessingSlow();
}

function SendFast() {
    if (ajaxRunningFast == true)
        return;
    ajaxRunningFast = true;
    AjaxProcessingFast();
}

function AjaxProcessingSlow(processItem) {

    if (slowAjaxQueue.length > 0) {

        currentSlowProcessItem = slowAjaxQueue[0];
        slowAjaxQueue.shift();

        if (IsIdInSlowQueue(currentSlowProcessItem.id) == false) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "WebServices/" + this.currentSlowProcessItem.ajaxMethod,
                data: "{'queueParameters':" + JSON.stringify(currentSlowProcessItem.parameters) + "}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: true,
                error: SlowQueueError,
                success: SlowQueueSuccess
            });
        }
        else {
            queueSlowProcessStart();
        }
    }
}

function AjaxProcessingFast(processItem) {
    if (fastAjaxQueue.length > 0) {

        currentFastProcessItem = fastAjaxQueue[0];
        fastAjaxQueue.shift();

        if (IsProcessIdInFastQueue(currentFastProcessItem.id) == false) {

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "WebServices/" + currentFastProcessItem.ajaxMethod,
                data: "{'queueParameters':" + JSON.stringify(currentFastProcessItem.parameters) + "}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: true,
                error: ErrorFastQueue,
                success: SuccessFastQueue
            });
        }
        else {
            queueFastProcessStart();
        }
    }
}

function IsIdInSlowQueue(processId) {
    var len = slowAjaxQueue.length;
    for (var i = 0; i < len; i++) {
        if (slowAjaxQueue[i].id == processId)
            return true;
    }
    return false;
}

function IsProcessIdInFastQueue(processId) {
    var len = fastAjaxQueue.length;
    for (var i = 0; i < len; i++) {
        if (fastAjaxQueue[i].id == processId)
            return true;
    }

    return false;
}

function queueSlowProcessStart() {
    if (slowAjaxQueue.length > 0)
        AjaxProcessingSlow();
    else
        this.ajaxRunningSlow = false;
}


function queueFastProcessStart() {
    if (fastAjaxQueue.length > 0)
        AjaxProcessingFast();
    else
        this.ajaxRunningFast = false;
}


// If the slow queue is empty, grab something from the fast quue to use on this channel
function queueSlowProcessStart() {
    if (slowAjaxQueue.length > 0)
        AjaxProcessingSlow();
    else {
        if (fastAjaxQueue.length > 0) {
            var processItem = fastAjaxQueue[0];
            fastAjaxQueue.shift();
            slowAjaxQueue.push(processItem);
            AjaxProcessingSlow();
        }
        else {
            this.ajaxRunningSlow = false;
        }
    }
}

function SlowQueueSuccess(data, status) {

    currentSlowProcessItem.successMethod(data.d);

    queueSlowProcessStart();
}

function SlowQueueError(request, status, error) {
    queueSlowProcessStart();
}

function SuccessFastQueue(data, status) {

    currentFastProcessItem.successMethod(data.d);

    queueFastProcessStart();
}

function ErrorFastQueue(request, status, error) {
    queueFastProcessStart();
}

// This class defines a queue item
function ProcessItem(processId, ajaxMethod, successMethod, parameters) {
    this.id = processId;
    this.ajaxMethod = ajaxMethod;
    this.successMethod = successMethod;
    this.parameters = parameters;
}


