/*****************************************************************************
 *
 * File     : Collection.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	function:	Collection
*
*	purpose:	Constructor
*
*************************************************************************/
function Collection()
{
	// method assignments
	this.init= Collection.init;
	this.add = Collection.add;
	this.addAll = Collection.addAll;
	this.swap = Collection.swap;
	this.insertAt = Collection.insertAt;
	this.toArray = Collection.toArray;
	this.getSize = Collection.getSize;
	this.get = Collection.get;
	this.remove = Collection.remove;
	this.removeAt = Collection.removeAt;
	this.removeAll = Collection.removeAll;
	this.contains = Collection.contains;
	this.indexOf = Collection.indexOf;

	// instance field declarations
	this._theArray;

	// initialization
	this.init();
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initialization
*
*************************************************************************/
function Collection.init()
{
	// The Array type is in effect sort of a Map. It doesn't need the growth handling
	this._theArray = new Array();
}

/************************************************************************
*
*	function:	get
*
*	purpose:	
*
*************************************************************************/
function Collection.get(index)
{
	return this._theArray[index];
}

/************************************************************************
*
*	function:	add
*
*	purpose:	
*
*************************************************************************/
function Collection.add(object)
{
	this._theArray[this._theArray.length] = object;
}

/************************************************************************
*
*	function:	addAll
*
*	purpose:	
*
*************************************************************************/
function Collection.addAll(collection)
{
	for (var index = 0; index < collection.getSize(); index++)
	{
		this.add(collection.get(index));
	}
}

/************************************************************************
*
*	function:	insertAt
*
*	purpose:	
*
*************************************************************************/
function Collection.insertAt(object, targetIndex)
{
	if (targetIndex > this._theArray.length) throw "Index out of bounds: " + targetIndex;
	var newArray = new Array();
	for (var index = 0; index < targetIndex; index++)
	{
		newArray[index] = this._theArray[index];
	}

	newArray[targetIndex] = object;

	for (var index = targetIndex; index < this._theArray.length; index++)
	{
		newArray[index + 1] = this._theArray[index];
	}
	
	this._theArray = newArray;
}

/************************************************************************
*
*	function:	remove
*
*	purpose:	
*
*************************************************************************/
function Collection.remove(object)
{
	var indexOf = this.indexOf(object);
	if (indexOf == -1) return;
	
	for (var index = indexOf; index < this._theArray.length - 1; index++)
	{
		this._theArray[index] = this._theArray[index + 1];
	}

	this._theArray[this._theArray.length - 1] = null;
	this._theArray.length = this._theArray.length - 1;
}

/************************************************************************
*
*	function:	removeAt
*
*	purpose:	
*
*************************************************************************/
function Collection.removeAt(index)
{
	if (index < 0 || index >= this._theArray.length) return;
	var object = this.get(index);
	this.remove(object);
	return object;
}

/************************************************************************
*
*	function:	removeAll
*
*	purpose:	
*
*************************************************************************/
function Collection.removeAll()
{
	this._theArray.length = 0;
}

/************************************************************************
*
*	function:	swap
*
*	purpose:	
*
*************************************************************************/
function Collection.swap(previousIndex, nextIndex)
{
	if (previousIndex >= nextIndex) throw "Indices are reversed - cannot swap.";
	if (previousIndex < 0)  throw "Index is negative.";
	if (nextIndex > this.getSize())  throw "Index out of bounds.";
	
	var nextItem = this.removeAt(nextIndex);
	var previousItem = this.removeAt(previousIndex);

	this.insertAt(nextItem, previousIndex);
	this.insertAt(previousItem, nextIndex);
}

/************************************************************************
*
*	function:	contains
*
*	purpose:	
*
*************************************************************************/
function Collection.contains(object)
{
	if (this.indexOf(object) > -1) return true;
	return false;
}

/************************************************************************
*
*	function:	indexOf
*
*	purpose:	
*
*************************************************************************/
function Collection.indexOf(object)
{
	for (var index = 0; index < this._theArray.length; index++)
	{
		if (object == this._theArray[index]) return index;
	}
	return -1;
}

/************************************************************************
*
*	function:	getSize
*
*	purpose:	
*
*************************************************************************/
function Collection.getSize()
{
	return this._theArray.length;
}

/************************************************************************
*
*	function:	toArray
*
*	purpose:	
*
*************************************************************************/
function Collection.toArray()
{
	return  this._theArray;
}
