/* Queue.js - a function for creating an efficient queue in JavaScript
*
* The author of this program, Safalra (Stephen Morley), irrevocably releases
* all rights to this program, with the intention of it becoming part of the
* public domain. Because this program is released into the public domain, it
* comes with no warranty either expressed or implied, to the extent permitted
* by law.
*
* For more public domain JavaScript code by the same author, visit:
*
* http://www.safalra.com/web-design/javascript/
*/


function Queue()
{
	var _queue = [];  // the list of elements, initialised to the empty array

	this.queue = function() { return _queue; }

	this.removeAt = function(index)
	{
		if (index == 0)
		{
			_queue = _queue.slice(1);
		}
		else if (index == _queue.length - 1)
		{
			_queue = _queue.slice(0, _queue.length - 1);
		}
		else
		{
			_queue = [].concat(_queue.slice(0, index - 1), _queue.slice(index + 1));
		}
	}

	this.length = function()
	{
		return _queue.length;
	}

	this.isEmpty = function()
	{
		// return true if the queue is empty, and false otherwise
		return (_queue.length == 0);
	}

	this.enqueue = function(element)
	{
		_queue.push(element);
	}

	this.contains = function(value)
	{
		var result = false;

		for (var i = 0; i < _queue.length; i++)
		{
			if (_queue[i] == value)
			{
				result = true;
				break;
			}
		}

		return result;
	}

	this.containsID = function(id)
	{
		var result = false;

		for (var i = 0; i < _queue.length; i++)
		{
			if (_queue[i].id == id)
			{
				result = true;
				break;
			}
		}

		return result;
	}
}
