//A QUEUE this ones are alwais usefull

function Queue::onAdd(%this)
{
	echo("Queue added");
	%this.nodeStart = "";
	%this.nodeEnd = "";
	%this.count = 0;
}

function Queue::onRemove(%this)
{
   //todo or is there garbage colection?
}

//Returns a node that can be used by Queue::deNode
function Queue::addEnd(%this, %data)
{
	if (%this.nodeEnd !$= "")
	{
		echo ("not empty");
	    %this.node[%this.count] = new ScriptObject(QueueNode)
		{
			data = %data;
			next = "";
			prev = %this.nodeEnd;
		};
		%this.nodeEnd.setNext(%this.node[%this.count]);
		%this.nodeEnd = %this.node[%this.count];
	}
	else
	{
		echo ("empty");
		%this.node[%this.count] = new ScriptObject(QueueNode)
		{
			data = %data;
			next = "";
			prev = "";
		};
		%this.nodeEnd = %this.node[%this.count];
		%this.nodeStart = %this.node[%this.count];
	}
	%retnode = %this.node[%this.count];
	%this.count++;
	return %retnode;
	
}

//Returns a node that can be used by Queue::deNode
function Queue::addStart(%this, %data)
{
	if (%this.nodeStart !$= "")
	{
	    %this.node[%this.count] = new ScriptObject(QueueNode)
		{
			data = %data;
			next = %this.nodeStart;
			prev ="";
		};
		%this.nodeStart.setPrev(%this.node[%this.count]);
		%this.nodeStart = %this.node[%this.count];
	}
	else
	{
		%this.node[%this.count] = new ScriptObject(QueueNode)
		{
			data = %data;
			next = "";
			prev = "";
		};
		%this.nodeEnd = %this.node[%this.count];
		%this.nodeStart = %this.node[%this.count];

	}
	return %this.node[%this.count++];
}

//remove the end node and returns the nodes data
function Queue::deEnd(%this)
{
   %data = %this.nodeEnd.data;
   %this.nodeEnd.getPrev().setNext("");
   %node = %this.nodeEnd;
   %this.nodeEnd = %this.nodeEnd.getPrev();
   %node.delete();
   return %data;
}

//remove the start node and returns the nodes data
function Queue::deStart(%this)
{
   %data = %this.nodeStart.data;
   %this.nodeStart.getNext().setPrev("");
   %node = %this.nodeStart;
   %this.nodeStart = %this.nodeStart.getNext();
   %node.delete();
   return %data;
}

//remove the node and returns the nodes data
function Queue::deNode(%this,%node)
{
	%data = "";	
	//handle if this node is the only node in the queue.
	if ((%node $= %this.nodeEnd) && (%node $= %this.nodeStart))
	{
		echo ("both");
		%this.nodeEnd = "";
		%this.nodeStart = "";
		%data = %node.getData();
		%node.delete();
	}
	else if (%node $= %this.nodeEnd) //this node is at the end of the queue
	{
		echo ("end");
		%data = %this.deEnd();
	}
	else if (%node $= %this.nodeStart) //this node is at the start of the queue
	{
		echo ("start");
		%data = %this.deStart();
	}
	else //the node is somewhere in the middle
	{
		echo ("middle");
		%data = %node.getData();
		%node.getPrev().setNext(%node.getNext());
		%node.getNext().setPrev(%node.getPrev());
		%node.delete();
	}
	
	return %data;
}


//Nodes used by the queue.

function QueueNode::getNext(%this)
{
	return %this.next;
}

function QueueNode::getPrev(%this)
{
	return %this.prev;
}

function QueueNode::getData(%this)
{
	return %this.data;
}

function QueueNode::setNext(%this,%next)
{
	%this.next = %next;
}

function QueueNode::setPrev(%this,%prev)
{
	%this.next = %prev;
}
