//-----------------------------------------------------------------------------
function AjaxRequest(url, parameters, callbackMethod)
{
		this.url = url;
		this.parameters = parameters;
		this.callbackMethod = callbackMethod;
}

var LinkedList = $.inherit(
{
    __constructor : function()
    {
        this._first = null;
        this._last = null;
    },


	add : function(object)
	{
		if (! COMMONS.isDefined(this._first))
		{
			this._first = object;
			this._last = object;
		}
		else
		{
			this._last.next = object;
			this._last = object;
		}
	},

    getFirst : function()
	{
		return this._first;
	},

	removeFirst : function()
	{
		if (! COMMONS.isDefined(this._first)) return;
		if (! COMMONS.isDefined(this._first.next))
		{
			this._first = null;
			this._last = null;
			return;
		}
		this._first = this._first.next;
	},

	removeAll : function()
	{
		this._first = null;
		this._last = null;
	}
} , /* Class to inherit class  */ {/* nothing to do */})


var AjaxRequestList = $.inherit(LinkedList,

{
    __constructor : function(property) {
        this.onAllRequestsCompleted = null;
    },

    ///////////////////////////////////////////////////
    // Inherited methods //////////////////////////////
    ///////////////////////////////////////////////////


    add : function(object)
    {
        return this.__base(object);
    },

    removeFirst : function()
    {
        return this.__base();
    },

    removeAll : function()
    {
        return this.__base();
    },

    getFirst : function() {
        return this.__base();
    },

    ///////////////////////////////////////////////////
    // Inherited methods end //////////////////////////
    ///////////////////////////////////////////////////


    addRequest : function(url, parameters, callbackMethod)
    {
        this.add(new AjaxRequest(url, parameters, callbackMethod));
        if (! COMMONS.isDefined(this._first.next))
        {
            this._makeFirstRequest();
        }
    },

    removeFirstRequest : function()
    {
        this.removeFirst();
        if (! COMMONS.isDefined(this._first))
        {
            this._allRequestsCompleted();
        }
        this._makeFirstRequest();
    },

    _makeFirstRequest : function()
    {
        var ajaxRequest = this._first;
        if (COMMONS.isDefined(ajaxRequest))
        {
            $.ajax({
              type: "GET",
              url: ajaxRequest.url,
              data: ajaxRequest.parameters,
              success: ajaxRequest.callbackMethod

            });

        }
    },

    _allRequestsCompleted : function()
    {
        if (COMMONS.isDefined(this.onAllRequestsCompleted))
        {
            this.onAllRequestsCompleted();
            this.onAllRequestsCompleted = null;
        }
    }
},

{});
function DelayInvoker(start, stop, millis)
{
    this.start = start;
    this.stop = stop;
    this.millis = millis || 1000;
    this.stopMills = COMMONS.toInteger(this.millis / 10);

    this.prcID = undefined;
    this.stopPrcID = undefined;

    this.invoke = function()
	{
		if ( COMMONS.isDefined(this.prcID) )
		{
			window.clearTimeout(this.prcID);
			this.prcID = undefined;
		}
		this.prcID = window.setTimeout(this.start, this.millis);
	};

    this.close = function()
	{
			var processID = this.prcID;
            var stop  = this.stop;
				if ( COMMONS.isDefined(processID) )
				{
					window.clearTimeout(processID);
				}
                stop();
			this.prcID = undefined;
	};
}

var ajaxRequestList = new AjaxRequestList();

var delayInvoker = new DelayInvoker(
        function()
        {
            var div = $('#divProgressBar');
            if (COMMONS.isDefined(div))
            {
                div.show();
            }
        },
        function()
        {
            var div = $('#divProgressBar');
            if (COMMONS.isDefined(div))
            {
                div.hide();
            }
        },
        1000);

function createAjaxRequest(url, parameters, callbackMethod)
{
	if (! COMMONS.isDefined(callbackMethod))
	{
		callbackMethod = defaultCallback;
	}

    if (! COMMONS.isDefined(ajaxRequestList.onAllRequestsCompleted))
    {
        delayInvoker.invoke();
        ajaxRequestList.onAllRequestsCompleted = function()
        {
            delayInvoker.close();
        };
    }

    ajaxRequestList.addRequest(url, parameters, callbackMethod);
}

function defaultCallback(request)
{
	processJsonResponse(request.responseText);
}

function processJsonResponse(responseText)
{
    if (! COMMONS.isDefined(responseText))
    {
        return null;
    }

    try
    {
        var result = $.evalJSON(responseText);
    }
    catch(err)
    {
        ajaxRequestList.removeAll();
        return null;
    }

    ajaxRequestList.removeFirstRequest();
	return result;
}

function processHtmlResponse(responseText)
{
    if (! COMMONS.isDefined(responseText))
    {
        return null;
    }

    ajaxRequestList.removeFirstRequest();
	return responseText;
}

//-----------------------------------------------------------------------------

