
dojo.declare("zsite_arraylist", null, {
	/*private int*/		increment:0,
	/*private int*/		size:0,
	/*private array*/	data:null,

	constructor: function()
	{
		this.increment = 10;
		this.size = 0;
		this.data = new Array(this.increment);
	},

	// reverse the ArrayList
	reverse: function()
	{
		var newData = new Array(this.size);
		for (var i=0; i<this.size; i++)
		{
			newData[i] = this.data[this.size-i-1];
		}
		this.data = newData;
	},
	
	// getCapacity() -- returns the number of elements the vector can hold
	getCapacity: function() 
	{
		return this.data.length;
	},

	// getSize() -- returns the current size of the vector
	getSize: function() 
	{
		return this.size;
	},

	// isEmpty() -- checks to see if the Vector has any elements
	isEmpty: function() 
	{
		return this.getSize() == 0;
	},

	// getLastElement() -- returns the last element
	getLastElement: function() 
	{
		if(this.data[this.getSize() - 1] != null)
		{
	        	return this.data[this.size()-1];
		}
	},

	// getFistElement() -- returns the first element
	getFirstElement: function() 
	{
		if(this.data[0] != null)
		{
			return this.data[0];
		}
		return null;
	},

	// getElementAt() -- returns an element at a specified index
	get: function(/*:int*/i)
	{
		return this.data[i];
	},

	// add() -- adds a element at the end of the Vector
	add: function(obj)
	{
		if(this.getSize() == this.data.length) 
		{
			this.resize();
		}
		this.data[this.size++] = obj;
	},

	// add() -- adds a element at the end of the Vector
	addAll: function(obj)
	{
		for (var i=0; i<obj.getSize(); i++) 
		{
			this.add(obj.get(i));
		}
	},

	remove: function(obj)
	{
		var index = this.indexOf(obj);
		if(index>=0)
			return this.removeElementAt(index);
		return null;
	},

	// insertElementAt() -- inserts an element at a given position
	insertElementAt: function(obj, index) 
	{
		if(this.size == this.capacity) 
		{
			this.resize();
		}
   
		for(var i=this.getSize(); i > index; i--) 
		{
			this.data[i] = this.data[i-1];
		}
		this.data[index] = obj;
		this.size++;
	},

	// removeElementAt() -- removes an element at a specific index
	removeElementAt: function(index)
	{
		var element = this.data[index];

		for(var i=index; i<(this.getSize()-1); i++)
		{
			this.data[i] = this.data[i+1];
		}

		this.data[this.getSize()-1] = null;
		this.size--;
		return element;
	},

	// removeAllElements() -- removes all elements in the Vector
	removeAllElements: function()
	{
		this.size = 0;

		for(var i=0; i<this.data.length; i++) 
		{
			this.data[i] = null;
		}
	},

	// indexOf() -- returns the index of a searched element
	indexOf: function(obj)
	{
		for(var i=0; i<this.getSize(); i++) 
		{
			if(this.data[i] == obj) 
			{
				return i;
			}
		}
		return -1;
	},

	// contains() -- returns true if the element is in the Vector, otherwise false
	contains: function(obj)
	{
		for(var i=0; i<this.getSize(); i++) 
		{
			if(this.data[i] == obj)
			{
				return true;
			}
		}
		return false;
	},

	// resize() -- increases the size of the Vector
	resize: function()
	{
		newData = new Array(this.data.length + this.increment);

		for(var i=0; i< this.data.length; i++) 
		{
			newData[i] = this.data[i];
		}
        	this.data = newData;
	},

	// trimToSize() -- trims the vector down to it's size
	trimToSize: function() 
	{
		var temp = new Array(this.getSize());
   
		for(var i=0; i<this.getSize(); i++) 
		{
			temp[i] = this.data[i];
		}
		this.size = temp.length - 1;
		this.data = temp;
	},

	// sort() - sorts the collection based on a field name - f
	sort: function(f) 
	{
		var i, j;
		var currentValue;
		var currentObj;
		var compareObj;
		var compareValue;

		for(i=1; i<this.getSize();i++) 
		{
			currentObj = this.data[i];
			currentValue = currentObj[f];

			j= i-1;
			compareObj = this.data[j];
			compareValue = compareObj[f];

			while(j >=0 && compareValue > currentValue) 
			{
				this.data[j+1] = this.data[j];
				j--;
				if (j >=0)
				{
					compareObj = this.data[j];
					compareValue = compareObj[f];
				}				
			}	
			this.data[j+1] = currentObj;
		}
	},

	// clone() -- copies the contents of a Vector to another Vector returning the new Vector.
	clone: function() 
	{
		var newArrayList = new zsite_arraylist(this.size);
	
		for(var i=0; i<this.size; i++)
		{
			newArrayList.add(this.data[i]);
		}
	
		return newArrayList;
	},

	// overwriteElementAt() - overwrites the element with an object at the specific index.
	overwriteElementAt: function(obj, index) 
	{
		this.data[index] = obj;
	}
});

zsite_arraylist.EMPTY_LIST = new zsite_arraylist();

