﻿/*

Minimum required HTML for this class:

<div id="[container_id]">
	<div id="[list_id]">
	</div>
</div>

Optional HTML:

<div id="[container_id]">
	Some text displaying the count of: <span id="counter"></span> item(s) in the list.
	<div id="[list_id]">
	<span id="zeroCaption">Text displayed when there are 0 items in the list. This will be hidden when items exist. MUST be a span.</span>
	</div>
</div>

*/

List.instances = new Array;

/*

@e = the HTML DOM element of the list container ([container_id] in the HTML notes above)

*/

function List(e)
{
	this.container = e;
	this.list = this.container.getElementsByTagName("div")[0]; //get the first div - the list itself
	this.sort_enabled = true;

	// find the counter 
	
	this.HTMLCounter = this.findHTMLCounter();
	
	// add it to the collection
	
	List.instances[List.instances.length] = this;
	
	// initialize the counter
	
	this.updateHTMLCounter();
	
	return this;
}

List.findInstanceByName = function(s)
{
	for (var i = 0; i < List.instances.length; i++)
		if (List.instances[i].container.id == s)
			return List.instances[i];
}

List.prototype.findHTMLCounter = function()
{
	for (var e in this.container.getElementsByTagName("span"))
		if (this.container.getElementsByTagName("span")[e].id == "counter")
			return this.container.getElementsByTagName("span")[e];
}

List.prototype.updateHTMLCounter = function()
{
	try
	{
		this.HTMLCounter.removeChild(this.HTMLCounter.firstChild);
	} catch(e){}	
	
	var count;
	
	count = this.list.getElementsByTagName("div").length;
	
	try
	{
		this.HTMLCounter.appendChild(document.createTextNode(count.toString()));
	} catch(e){}
}

List.prototype.addItem = function(value, caption)
{
	var d = document.createElement("div");
		d.id = this.list.id + "_item_" + value.toString();
		d.className = "listItem";
		
	/*
	var c = document.createElement("input");
		c.id = this.list.id + "_input_" + value;
		c.setAttribute("type", "checkbox");
		c.setAttribute("name", this.list.id);
		c.setAttribute("value", value);
		
		d.appendChild(c);
	
	
	*/
	
	var s = document.createElement("span");
		s.appendChild(document.createTextNode(caption));
	
		d.appendChild(s);
	
	this.list.appendChild(d);
	
	// update the counter
	
	this.updateHTMLCounter();
	
	// sort
	if (this.sort_enabled)
		this.sort();
}

List.prototype.sort = function()
{
	var a = new Array();
	var e = this.list.getElementsByTagName("div");
	var x;
	
	// collect the elements
	
	for (var i = e.length - 1; i >= 0; i--)
	{
		if (e.item(i).textContent) // FF
			x = new Array (e.item(i).getElementsByTagName("span")[0].textContent.toLowerCase(), e.item(i));
		else // IE
			x = new Array (e.item(i).getElementsByTagName("span")[0].innerText.toLowerCase(), e.item(i));
			
		// remove it
		
		this.list.removeChild(e.item(i));
		
		a.push(x);
	}
	
	// sort it
	
	a.sort();
	
	// reinsert the items in order

	for (var i = 0; i < a.length; i++)
	{
		this.list.appendChild(a[i][1]);
	}
}

List.prototype.removeItem = function(value)
{
	var e = this.list.getElementsByTagName("div");
	
	for (var i = 0; i < e.length; i++)
		if (e[i].id == this.list.id + "_item_" + value.toString())
			this.list.removeChild(e[i]);
			
	// update the counter
	
	this.updateHTMLCounter();
}

List.prototype.enableSort = function()
{
	this.sort_enabled = true;	
}

List.prototype.disableSort = function()
{
	this.sort_enabled = false	
}

List.prototype.makeIterator = function()
{
	var Iterator = function( lst )
	{
		this.collection = lst;
		this.reset();
	};
	Iterator.prototype = {};
	Iterator.prototype.reset = function() 
	{
		this.i = -1;
	};
	Iterator.prototype.next = function()
	{
		var itemNodes = this.collection.list.childNodes;
		var j;

		for (j = this.i+1; j < itemNodes.length && (itemNodes[j].nodeName != "DIV" || itemNodes[j].className != "listItem"); ++j);
		this.i = j;

		if (this.i >= itemNodes.length) {
			delete this.value
			delete this.caption
			this.i = itemNodes.length;
			return false;
		}
		
		this.value = itemNodes[this.i].id.split("_");
		this.value = this.value[this.value.length-1];
		this.caption = itemNodes[this.i].children[0].childNodes[0].nodeValue;
		return true;
	};
	
	return new Iterator(this);
}

