//////////////////////////////////////////////////////////////////
// Queue of elements
// Hans Dybkjær 2008-06-16
//////////////////////////////////////////////////////////////////
// An array of nodes kept as a queue
function Queue(id, className) 
{
	this.Nodes = []; // The queue
	this.Length = 0; // its length
}


	// Add a node getting it from the document by id
	// In: string id: Unique id of node in document
Queue.prototype.AddNodeById = function(id)
	{
		this.Nodes[this.Length++] = document.getElementById(id);
	}
	// Add a node directly
	// In: object o: An element (from the document)
Queue.prototype.AddNode = function(o)
	{
		this.Nodes[this.Length++] = o;
		
	}
	
	// Move given object to front of queue, moving everybody else one down
	// Runtime O(N)
	// In: object obj: Object to move; obj must exist in Queue
Queue.prototype.Upgrade = function(obj)
	{
		if (this.Length === 0 || this.Nodes[0] == obj)
		{
			return;
		}
		var temp = this.Nodes[0];
		this.Nodes[0] = obj;
		for (var i = 1; i < this.Length; i++)
		{
			var o = this.Nodes[i];
			this.Nodes[i] = temp;
			if (obj == o)
			{
				break;
			}
			temp = o;
		}
	}
	
	// Apply function to every object
	// In: function fun(obj, index, queueLength): Function to apply
Queue.prototype.Apply = function(fun)
	{
		for (var i = 0; i < this.Length; i++)
		{
			var o = this.Nodes[i];
			fun(o, i, this.Length);
		}
	}	
	
		// Get div nodes of given classname below id'ed root node
		// In: string id: Id of root node in subtree to search
		// In: string className: get only nodes of this CSS class
		// Return: Queue: The queue itself, enabling e.g. var queue = new Queue().Fill(id, name)
Queue.prototype.Fill = function(id, className)
	{
		var parent = document.getElementById(id);
		var nodes = parent.getElementsByTagName("div");
		var len = nodes.length;
		for (var i = 0; i < len; i++)
		{
			var o = nodes[i];
			if (o.className == className)
			{
				this.AddNode(o);
			}
		}
		return this;
	}
