﻿var ArrayHelper = {

	Remove : function( array, predicate )
	{
		ArrayHelper.RemoveAt( array, ArrayHelper.IndexOf( array, predicate ) );
	},

	RemoveAt : function( array, index )
	{
		var i;
			
		if( array != null )
		{
			if( index >= 0 && index < array.length )
			{
				if( index >= 0 && index < array.length - 1 )
				{
					for(i=index;i<array.length;i++)
						array[i] = array[i + 1];
				}
			
				array.length--;
			}
		}
	},
	
	Find : function( array, predicate )
	{
		var index = ArrayHelper.IndexOf( array, predicate );
		
		if( index != -1 )
			return array[ index ];
		else
			return null;
	},

	IndexOf : function( array, predicate )
	{
		var index = -1;
		
		if( array != null )
		{
			var i = 0;
			
			while( i < array.length && index == -1 )
			{
				if( predicate( array[i] ) )
					index = i;
				
				i++;
			}
		}
		
		return index;
	},
	
	Contains : function( array, predicate )
	{
		return ArrayHelper.IndexOf( array, predicate ) != -1;
	}
}

var Collection = function()
{
	this.list = new Array();
}

Collection.prototype = new function()
{
	this.list = null;
	
	this.Add = Add; function Add( item )
	{
		this.list.push( item );
	}
	
	this.Insert = Insert; function Insert( item, index )
	{
		if(this.list == null)
			this.list = new Array();
		
		this.list.length++;
		
		for( var i = this.list.length - 1; i > index; i-- )
			this.list[i] = this.list[i-1];
		
		this.list[index] = item;
	}
	
	this.AddRange = AddRange; function AddRange()
	{
		for( var i = 0; i < arguments.length; i++ )
			this.Add( arguments[i] );
	}
	
	this.Get = Get; function Get( index )
	{
		return this.list[index];
	}
	
	this.GetCount = GetCount; function GetCount()
	{
		return this.list.length;
	}
	
	this.Count = GetCount;
	
	this.Remove = function( item )
	{
		var index = this.IndexOf( item );
		
		if( index != -1 )
			this.RemoveAt( index );
	}
	
	this.RemoveAt = RemoveAt; function RemoveAt( index )
	{
		ArrayHelper.RemoveAt( this.list, index );
	}
	
	this.RemoveAll = RemoveAll; function RemoveAll()
	{
		this.list = new Array();
	}
	
	this.RemoveFrom = RemoveFrom; function RemoveFrom( start, count )
	{
		for( var i = start; i < this.list.length - count; i++ )
			this.list[ i ] = this.list[ i + count ];
			
		this.list.length -= count;
	}
	
	this.IndexOf = IndexOf; function IndexOf( item )
	{
		return this.indexOf( function(x,y){return x == y}, item );
	}
	
	this.find = find; function find( comparer, value )
	{
		var index = this.indexOf( comparer, value );
		
		if( index != -1 )
			return this.list[index];
		else
			return null;
	}
	
	this.indexOf = indexOf; function indexOf( comparer, value )
	{
		var index = -1;
		
		if( this.list != null )
		{
			var i = 0;
			
			while( i < this.list.length && index == -1 )
			{
				if( comparer( this.list[i], value ) )
					index = i;
				
				i++;
			}
		}
		
		return index;
	}
}

var Dictionary = function( keyComparer )
{
	this.list = new Array();
	
	if( keyComparer )
		this.keyComparer = keyComparer;
	else
		this.keyComparer = function(a,b){return a==b;};
}

Dictionary.prototype = new function()
{
	this.list = null;
	
	this.GetCount = GetCount; function GetCount()
	{
		return this.list.length;
	}
	
	this.GetByIndex = GetByIndex; function GetByIndex( index )
	{
		return this.list[ index ];
	}
	
	this.GetByKey = GetByKey; function GetByKey( key )
	{
		var node = this.GetNodeByKey( key );
		
		if( node != null )
			return node.Value;
		else
			return null;
	}
	
	this.GetNodeByKey = GetNodeByKey; function GetNodeByKey( key )
	{
		var i = 0;
		var r = null;
		
		while( i < this.list.length && r == null )
		{
			if( this.keyComparer( this.list[i].Key, key ) )
				r = this.list[i];
			
			i++;
		}
		
		return r;
	}
	
	this.Add = Add; function Add( key, value )
	{
		this.list.push( new KeyValuePair(key,value) );
	}
	
	this.Remove = function( key )
	{
		var delegate = new pox.Delegate( this, function( item ){ return this.keyComparer( item.Key, key ) } );
		
		ArrayHelper.Remove( this.list, delegate.CreateFunction() );
		
		delegate.Dispose();
	}
	
	function KeyValuePair( key, value )
	{
		this.Key   = key;
		this.Value = value;
	}
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();