// list

function List()
	{
	this.recs = new Array();
	this.rec_id = 0;
	}
//--------------------------------------------------
List.prototype.getTotal = function()
	{
	if (this.recs) return this.recs.length;
	return 0;
	}
//--------------------------------------------------
List.prototype.add = function()
	{
	var obj = arguments[0];
	var findIdx = this.find(obj);
	if (findIdx != -1) return this.recs[findIdx].id;

	var total = this.recs.length;
	if (total>0) this.rec_id = parseInt(this.recs[total-1].id) + 1;
	else ++this.rec_id;

	var idx = this.recs.length;
	if (arguments.length == 2)
		{
		var klass = arguments[1];
		this.recs[idx] = {id: this.rec_id, klass:klass, data: obj};
		}
	else
		{
		this.recs[idx] = {id: this.rec_id, data: obj};
		}
	return this.rec_id;
	}
//--------------------------------------------------
List.prototype.addNew = function()
	{
	var obj = arguments[0];
	var total = this.recs.length;
	if (total>0) this.rec_id = parseInt(this.recs[total-1].id) + 1;
	else ++this.rec_id;

	var idx = this.recs.length;
	if (arguments.length == 2)
		{
		var klass = arguments[1];
		this.recs[idx] = {id: this.rec_id, klass:klass, data: obj};
		}
	else
		{
		this.recs[idx] = {id: this.rec_id, data: obj};
		}
	return this.rec_id;
	}

//--------------------------------------------------
List.prototype.id_to_index = function(id)
	{
	for (var idx = 0; idx < this.recs.length; idx++) 
		{
		if (id == this.recs[idx].id) return idx;
		}
	return -1;
	}
//--------------------------------------------------
List.prototype.klass_to_index = function(klass)
	{
	for (var idx = 0; idx < this.recs.length; idx++) 
		{
		if (klass == this.recs[idx].klass) return idx;
		}
	return -1;
	}

//--------------------------------------------------
List.prototype.get = function(id)
	{
	var idx = this.id_to_index(id);
	if (idx != -1) return this.recs[idx].data;
	return false;
	}
//--------------------------------------------------
List.prototype.getbyklass = function(klass)
	{
	var idx = this.klass_to_index(klass);
	if (idx != -1) return this.recs[idx].data;
	return false;
	}
//--------------------------------------------------
List.prototype.getbyidx = function(idx)
	{
	if (idx < (this.recs.length))
		{
		return this.recs[idx].data;	
		}
	return false;
	}
//--------------------------------------------------
List.prototype.find = function(data)
	{
	for (var idx = 0; idx < this.recs.length; idx++) 
		{
		if (data == this.recs[idx].data) return idx;
		}
	return -1;
	}

//--------------------------------------------------
List.prototype.remove = function(id)
	{
	var idx = this.id_to_index(id);
	if (idx != -1) return this.remove_by_index(idx);
	}

//--------------------------------------------------
List.prototype.getremove = function(id)
	{
	var idx = this.id_to_index(id);
	if (idx != -1)
		{
		var data = this.recs[idx].data;
		this.remove_by_index(idx);
		return data;
		}
	return false;
	}

//--------------------------------------------------
List.prototype.remove_by_index = function(from, to)
	{
	var recs = this.recs;
	var rest = recs.slice((to || from) + 1 || recs.length);
	recs.length = from < 0 ? recs.length + from : from;
	return recs.push.apply(recs, rest);
	}

//--------------------------------------------------
List.prototype.remove_by_klass = function(klass)
	{
	var idx;
	var ids = new Array();
	
	for (idx = 0; idx < this.recs.length; idx++) 
		{
		if (klass == this.recs[idx].klass)
			{
			ids[ids.length] = this.recs[idx].id;
			}
		}
	for (idx = 0; idx < ids.length; ++idx)
		{
		this.remove(ids[idx]);
		}
	delete ids;
	}

//--------------------------------------------------
List.prototype.dofunc = function(id, arg)
	{
	var func = this.get(id);
	this.remove(id);
	if (func)
		{
		if (arg) func(arg);
		else func();
		}
	}

//--------------------------------------------------
List.prototype.clear = function()
	{
	this.recs = [];
	this.rec_id = 0;
	}

//--------------------------------------------------
List.prototype.sort = function(func)
	{
	this.recs.sort(func);
	}
