/**
 * Contains the class Sitebox.Framework.Ajax.Cache.
 *
 * @copyright		2000-2006 CARE Internet Services B.V. All Rights Reserved
 * @internal		$Workfile: Cache.js$
 * @internal		$Date: 03-08-07 08:19:15 AM$
 * @author			$Author: robert$
 * @version			$Revision: 1$
 *
 * $NoKeyword$
 */


/**
 * Static class for the Ajax Cache.
 */
Sitebox.Framework.Ajax.Cache = {

	/**
	 * Array of items in the cache.
	 */
	_items: new Array(),

	/**
	 * Adds a request to the cache.
	 *
	 * @param	string			url
	 * @param	string			eTag
	 * @param	XMLHttpRequest	response
	 */
	addRequest: function(url, eTag, response)
	{
		/* check if this url already exists. */
		for (var i = 0; i < this._items.length; i++)
		{
			/* if so, save the new data and return. */
			if (this._items[i].url == url)
			{
				this._items[i].eTag = eTag;
				this._items[i].response = response;
				return;
			}
		}

		/* if not yet cached, append request to the list. */
		this._items.push({'url': url, 'eTag': eTag, 'response': response});
	}, // function addRequest

	/**
	 * Returns the XMLHttpRequest if cached, otherwise null.
	 *
	 * @param	string			url
	 *
	 * @return	XMLHttpRequest
	 */
	getRequest: function(url)
	{
		/* loop through cache to check if the url was saved to the cache. */
		for (var i = 0; i < this._items.length; i++)
		{
			/* if the url matches, it was cached. */
			if (this._items[i].url == url)
			{
				return this._items[i].response;
			}
		}
		return null;
	}, // function getRequest

	/**
	 * Returns the ETag for the cached response, or null if not found.
	 *
	 * @param	string			url
	 *
	 * @return	string
	 */
	getETag: function(url)
	{
		/* loop through cache to check if the url was saved to the cache. */
		for (var i = 0; i < this._items.length; i++)
		{
			/* if the url matches, it was cached. */
			if (this._items[i].url == url)
			{
				return this._items[i].eTag;
			}
		}
		return null;
	}, // function getETag

	/**
	 * Clears the entire cache.
	 */
	clear: function()
	{
		this._items.length = 0;
	} // function clear

}; // class Sitebox.Framework.Ajax.Cache