//-----------------------------------------------------------------------
// Copyright (C) wwwRoot.cn, All rights reserved.
//-----------------------------------------------------------------------
// Root.List.js

List = function(){}
List.prototype = new Array();
List.prototype.constructor = List;
List.prototype.count = 0;
List.prototype.caller = null;
List.prototype.add =
	function(item)
	{
		if(this.onbeforeadd != null) this.onbeforeadd.call(this, item); 
		this.count ++;
		this[this.count - 1] = item;
		if(this.onafteradd != null) this.onafteradd.call(this); 
	}
List.prototype.insert =
	function(index, item)
	{
		if(index == null) index = 0;
		if(this.onbeforinsert != null) this.onbeforeinsert.call(this, index, item); 
		if(index < this.count)
		{
			this.count ++;			
			for(var i = this.count - 1; i >= index; i--)
			{
				this[i] = this[i-1];
			}			
			this[index] = item;
		}
		else
		{
			this.add(item);
		}
		if(this.onafterinsert != null) this.onafterinsert.call(this, index); 
	}
List.prototype.remove = 
	function(item)
	{
		for(var i=0; i<this.count; i++)
		{
			if(this[i] == item)
			{
				break;
			}
		}
		if(i < this.count)	
		{
			if(this.onbeforeremove != null) this.onbeforeremove.call(this, i); 
			for(var j=i; j<this.count-1; j++)
			{
				this[j] = this[j+1];
			}
			this[this.count-1] = null;
			this.count --;
			if(this.onafterremove != null) this.onafterremove.call(this); 
		}			
	}
List.prototype.removeAt = 
	function(index)
	{
		if(index == null || index > this.count - 1) index = this.count;
		if(index < 0) index = 0;
		if(index < this.count)
		{
			if(this.onbeforeremove != null) this.onbeforeremove.call(this, index); 
			for(var i=index; i<this.count-1; i++)
			{
				this[i] = this[i+1];
			}
			this[this.count-1] = null;
			this.count --;
			if(this.onafterremove != null) this.onafterremove.call(this); 
		}
	}
List.prototype.clear = 
	function()
	{
		if(this.onbeforeclear != null) this.onbeforeclear.call(this); 
		for(var i=0; i<this.count; i++)
		{
			this[i] = null;
		}
		this.count = 0;	
		if(this.onafterclear != null) this.onafterclear.call(this); 
	}
List.prototype.indexOf = 
	function(item)
	{
		var index = -1;
		for(var i=0; i<this.count; i++)
		{
			if(this[i] == item)
			{
				index = i;
				break;
			}
		}
		return index;
	}
List.prototype.lastIndexOf =
	function(item)
	{
		var index = -1;
		for(var i=this.count-1; i>=0; i--)
		{
			if(this[i] == item)
			{
				index = i;
				break;
			}
		}
		return index;
	}
List.prototype.contains = 
	function(item)
	{
		var isContains = false;
		for(var i=0; i<this.count; i++)
		{
			if(item == this[i])
			{
				isContains = true;
				break;
			}
		}
		return isContains;
	}		
List.prototype.reverse = 
	function()
	{
		var item;
		var half = Math.floor(this.count/2);
		for(var i=0; i<half; i++)
		{
			item = this[i];
			this[i] = this[this.count-i-1];
			this[this.count-i-1] = item;
		}
	}
List.prototype.onbeforeadd = null;
List.prototype.onafteradd = null;
List.prototype.onbeforeinsert = null;
List.prototype.onafterinsert = null;
List.prototype.onbeforeremove = null;
List.prototype.onafterremove = null;
List.prototype.onbeforeclear = null;
List.prototype.onafterclear = null;