/*****************************************************************************
 *
 * File     : JsList.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	function:	JsList
*
*	purpose:	Constructor
*
*************************************************************************/
function JsList(uiElement)
{
	// method assignments
	this.init = JsList.init;
	this.attachEvent = JsList.attachEvent;
	this.detachEvent = JsList.detachEvent;
	this.addItem = JsList.addItem;
	this.getSelectedIndex = JsList.getSelectedIndex;
	this.getSelectedItem = JsList.getSelectedItem;
	this.getSelectedIndices = JsList.getSelectedIndices;
	this.getSelectedItems = JsList.getSelectedItems;
	this.removeSelectedItems = JsList.removeSelectedItems;
	this.getSize = JsList.getSize;
	this.moveItemUp = JsList.moveItemUp;
	this.moveItemDown = JsList.moveItemDown;
	this.removeItem = JsList.removeItem;
	this.removeAllItems = JsList.removeAllItems;
	this.removeItemAt = JsList.removeItemAt;
	this.getItems = JsList.getItems;
	this.swapItems = JsList.swapItems;
	this.selectItem = JsList.selectItem;
	this.indexOf = JsList.indexOf;
	this.disable = JsList.disable;
	this.enable = JsList.enable;

	// instance field declarations
	this._uiElement;
	this._items;
	
	// initialization
	this.init(uiElement);
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initializes the globals
*
*************************************************************************/
function JsList.init(uiElement)
{
	this._uiElement = uiElement;
	
	var optionsUIs = selectNodes(this._uiElement, "option");

	this._items = new Collection();
	for (var index = 0; index < optionsUIs.getSize(); index++)
	{
		var optionUI = optionsUIs.get(index);
		var value = optionUI.getAttribute("value");
		this._items.add(value);
	}
}

/************************************************************************
*
*	function:	attachEvent
*
*	purpose:	
*
*************************************************************************/
function JsList.attachEvent(eventName, handler)
{
	this._uiElement.attachEvent(eventName, handler);
}

/************************************************************************
*
*	function:	detachEvent
*
*	purpose:	
*
*************************************************************************/
function JsList.detachEvent(eventName, handler)
{
	this._uiElement.detachEvent(eventName, handler);
}

/************************************************************************
*
*	function:	getSelectedIndex
*
*	purpose:	
*
*************************************************************************/
function JsList.getSelectedIndex()
{
	return this._uiElement.selectedIndex;
}

/************************************************************************
*
*	function:	getSelectedIndices
*
*	purpose:	
*
*************************************************************************/
function JsList.getSelectedIndices()
{
	var result = new Array();
	var ii = 0;
	for (var index = 0; index < this._uiElement.options.length; index++)
	{
		if (this._uiElement.options[index].selected == true) result[ii++] = index;
	}
	return result;
}

/************************************************************************
*
*	function:	indexOf
*
*	purpose:	
*
*************************************************************************/
function JsList.indexOf(item)
{
	return this._items.indexOf(item);
}

/************************************************************************
*
*	function:	getSelectedItem
*
*	purpose:	
*
*************************************************************************/
function JsList.getSelectedItem()
{
	var index = this.getSelectedIndex();
	if (index == -1) return null;
	var selected = this._items.get(index);
	return selected;
}

/************************************************************************
*
*	function:	getSelectedItems
*
*	purpose:	
*
*************************************************************************/
function JsList.getSelectedItems()
{
	var result = new Collection();
	for (var index = 0; index < this._uiElement.options.length; index++)
	{
		if (this._uiElement.options[index].selected == true) result.add(this._items.get(index));
	}
	return result;
}

/************************************************************************
*
*	function:	getSize
*
*	purpose:	
*
*************************************************************************/
function JsList.getSize()
{
	return this._uiElement.options.length;
}

/************************************************************************
*
*	function:	addItem
*
*	purpose:	
*
*************************************************************************/
function JsList.addItem(item)
{
	var newOption = document.createElement("option");
	this._uiElement.options.add(newOption);
	newOption.innerText = item.toString();
	newOption.value = item;
	this._items.add(item);
}


/************************************************************************
*
*	function:	removeItemAt
*
*	purpose:	
*
*************************************************************************/
function JsList.removeItemAt(index)
{
	this._uiElement.options.remove(index);
	this._items.removeAt(index);
}

/************************************************************************
*
*	function:	removeItem
*
*	purpose:	
*
*************************************************************************/
function JsList.removeItem(item)
{
	var index = this._items.indexOf(item);
	this._uiElement.options.remove(index);
	this._items.remove(item);
}

/************************************************************************
*
*	function:	removeAllItems
*
*	purpose:	
*
*************************************************************************/
function JsList.removeAllItems()
{
	this._items.removeAll();
	this._uiElement.innerHTML = "";
}

/************************************************************************
*
*	function:	removeSelectedItems
*
*	purpose:	
*
*************************************************************************/
function JsList.removeSelectedItems()
{
	for (var index = this._uiElement.options.length - 1; index >= 0 ; index--)
	{
		if (this._uiElement.options[index].selected == true) this.removeItemAt(index);
	}
}

/************************************************************************
*
*	function:	getItems
*
*	purpose:	
*
*************************************************************************/
function JsList.getItems()
{
	return this._items;
}

/************************************************************************
*
*	function:	moveItemUp
*
*	purpose:	
*
*************************************************************************/
function JsList.moveItemUp(index)
{
	var size = this.getSize();
	if (size < 2) return;

	if (index < 1) return;

	var previous = index - 1;
	
	this.swapItems(previous, index);
}

/************************************************************************
*
*	function:	moveItemDown
*
*	purpose:	
*
*************************************************************************/
function JsList.moveItemDown(index)
{
	var size = this.getSize();
	if (size < 2) return;

	if (index == -1) return;
	if (index == size - 1) return;

	var next = index + 1;
	
	this.swapItems(index, next);
}

/************************************************************************
*
*	function:	swapItems
*
*	purpose:	
*
*************************************************************************/
function JsList.swapItems(previousIndex, nextIndex)
{
	var previousItem = this._uiElement.item(previousIndex);
	var nextItem = this._uiElement.item(nextIndex);
	previousItem.swapNode(nextItem);
	
	this._items.swap(previousIndex, nextIndex);
}

/************************************************************************
*
*	function:	selectItem
*
*	purpose:	
*
*************************************************************************/
function JsList.selectItem(item)
{
	var index = this._items.indexOf(item);
	this._uiElement.selectedIndex = index;
}

/************************************************************************
*
*	function:	disable
*
*	purpose:	Disable the UI element
*
*************************************************************************/
function JsList.disable() {
	this._uiElement.disabled = true;	
}

/************************************************************************
*
*	function:	enabled
*
*	purpose:	Enable the UI element
*
*************************************************************************/
function JsList.enable() {
	this._uiElement.disabled = false;	
}
