/**
 * Contains the class Sitebox.Framework.Ajax.Cache.
 *
 * @copyright	CARE Internet Services B.V. All Rights Reserved
 * @internal	$Id: Cache.js 465 2008-12-24 14:37:25Z niek $
 * @internal	$Date: 2008-12-24 15:37:25 +0100 (Wed, 24 Dec 2008) $
 * @author		$Author: niek $
 * @version		$Revision: 465 $
 */

/**
 * 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