﻿function listbox (_id, _container, _max)
{
	if ("01234567890".indexOf(_id.substr(0, 1)) != -1)
		alert("Invalid ID (" + _id + ") specified for listbox object.")
	
	this.id = _id;
	this.container = _container;
	this.count = 0;
	
	this.keys 	= new Array();
	this.values = new Array();
	
	(isNaN(_max)) ? this.maxIndex = 9 : this.maxIndex = _max;
	
	// the default value is the line height for the default text style of the site.
	
	this.lineHeight = 17;
	this.onchange_scope = null;
	this.onchange_method = null;
	this.onchange_assigned = false;
}

listbox.prototype.findValueIndex = function(key)
{
	
	var idx = null;
	
	for (var i = 0; i < this.keys.length; i++)
	{
		if (this.keys[i] == key)
		{
			idx = i;
		}
	}
	
	return idx;
	
}

listbox.prototype.setValue = function(key, value)
{
	
	var idx = this.findValueIndex(key);
	
	if (idx == null)
	{
		this.keys.push(key);
		idx = this.keys.length - 1;
	} 
	
	// insert to the values array
	
	this.values[idx] = value;
	
	// update the count
	
	this.count = this.keys.length;
	
}

listbox.prototype.dropValue = function(key)
{
	var idx = this.findValueIndex(key);
	
	if (idx != null)
	{
		this.keys.splice(idx, 1);
		this.values.splice(idx, 1);
	}
	
	// update the count
	
	this.count = this.keys.length;
	
}

listbox.prototype.getValue = function(key)
{
	var idx = this.findValueIndex(key);
	
	return this.values[idx];
}

listbox.prototype.sort = function()
{
	// copy the array
	
	this.sorter = new Array();
	
	for (var i = 0; i < this.values.length; i++)
	{
		this.sorter[i] = this.values[i];
	}
	
	// sort it
	
	this.sorter.sort();
	
	// create a temp key array
	
	this.sorter_keys = new Array();
	
	// for each value in new sort, find the position in the old, get it's associated key and insert that into the new position of a nre temp key array.
	
	var i = 0;
	var j = 0;
	
	for (var i = 0; i < this.sorter.length; i++)
	{
		for (var j = 0; j < this.values.length; j++)
		{
			if (this.sorter[i] == this.values[j])
			{
				this.sorter_keys[i] = this.keys[j];
			}
		}
	}
	
	// copy the values array
	
	for (var i = 0; i < this.sorter.length; i++)
	{
		this.values[i] = this.sorter[i];
	}
	
	// copy the keys array
	
	for (var i = 0; i < this.keys.length; i++)
	{
		this.keys[i] = this.sorter_keys[i];
	}
	
	delete this.sorter;
	delete this.sorter_keys;
}

/*

drawing functions

*/

listbox.prototype.resetcontainer = function(key)
{
	for (var i = this.container.childNodes.length - 1; i >= 0; i--)
	{
		this.container.removeChild(this.container.childNodes[i]);
	}
	
}

listbox.prototype.updatecontaineritem = function(key, idx)
{
	var idx = this.findValueIndex(key);
	var d = this.container.childNodes[idx];
	
	d.innerHTML = this.values[idx];
		
}

listbox.prototype.appendcontaineritem = function(key)
{
	var d = document.createElement("div");
	var idx = this.findValueIndex(key);
		
	d.innerHTML = this.values[idx];
		
	this.container.appendChild(d);
}

listbox.prototype.update = function()
{
	this.resetcontainer();
	
	// set scrolling if exceeding max
	
	if (this.count > this.maxIndex)
	{
		this.container.style.height = (this.lineHeight * (this.maxIndex + 1)).toString() + "px";
		this.container.style.overflowY = "scroll";	
	}
	else
	{
		this.container.style.height = "";
		this.container.style.overflow = "none";	
	}
	
	for (v in this.keys)
	{
		this.appendcontaineritem(this.keys[v]);
	}	
	
	try
	{
		this.onchange();
	} 
	catch(e){}
		
}
