
//-----------------------------------------------------------
//
//		Toolbar For FrameWork
//
//		Renders a XAPDataList
//
//		Requires : CoreLib.js, Base.js
//
//-----------------------------------------------------------

function XAPDataList(){}
XAPDataList.prototype					= new Base();
XAPDataList.prototype.constructor		= XAPDataList();
XAPDataList.prototype.ClassName			= "XAPDataList";
XAPDataList.prototype.ePane				= null;
XAPDataList.prototype.DataSource		= null;
XAPDataList.prototype.aSelectedItems	= [];
XAPDataList.prototype.oSelectedItem		= null;
XAPDataList.prototype.DataTextField		= "";
XAPDataList.prototype.DataIdField		= "";
XAPDataList.prototype.MultiSelect		= false;
XAPDataList.prototype.Disabled			= false;


XAPDataList.prototype.Events			= [];

XAPDataList.prototype.Init				= function ()
{
	
}	

XAPDataList.prototype.DataBind			= function ()
{
	if( this.ePane ){ this.ParentElement.removeChild( this.ePane ); }
	
	this.ePane = this.NewObj( "div", {className:'XAPDataList'}, this.ParentElement, "" );
	
	for( var i = 0; i < this.DataSource.length; i++  )
	{
		var Text = "";
		var Id	 = "";
		if( this.DataSource.constructor ==  Array )
		{
			Text	= this.DataSource[i][0];
			Id		= this.DataSource[i][1];
		}
		else
		{
			Text	= this.DataSource[i].getElementsByTagName(this.DataTextField)[0].text;
			Id		= this.DataSource[i].getElementsByTagName(this.DataIdField)[0].text;
		}
		
		var oItem				= new XAPListItem(Text, Id);
		oItem.Parent			= this;
		oItem.ParentElement		= this.ePane;
		this.Controls.Add( oItem );
	}
}

XAPDataList.prototype.onItemSelected	= function ( oItem )
{

	if( !this.MultiSelect )
	{
		if(this.oSelectedItem && !this.MultiSelect)
		{
			this.oSelectedItem.Deselect();
		}
		this.oSelectedItem = oItem;
	}
	else
	{
		var lNew = true;
		for( var i = 0; i < this.aSelectedItems.length; i++ )
		{
			if( this.aSelectedItems[i] == oItem )
			{
				oItem.Deselect();
				lNew = false;
				this.aSelectedItems.removeItem( oItem );
			}
		}
		if( lNew )
		{
			this.aSelectedItems[this.aSelectedItems.length] = oItem;
		}
	}
}

XAPDataList.prototype.Append	= function ( cText, cItemID )
{
	var oItem				= new XAPListItem(cText, cItemID);
	oItem.Parent			= this;
	oItem.ParentElement		= this.ePane;
	this.Controls.Add( oItem );
}

XAPDataList.prototype.Remove	= function ( oItem )
{
	if( !oItem ){ return false; }
	if( !this.MultiSelect )
	{
		if( this.oSelectedItem == oItem )
		{
			this.oSelectedItem.Deselect();
			this.oSelectedItem = null;
		}
	}
	else
	{
		for( var i = 0; i < this.aSelectedItems.length; i++ )
		{
			if( this.aSelectedItems[i] == oItem )
			{
				oItem.Deselect();
				this.aSelectedItems.removeItem( oItem );
			}
		}
	}
	
	var lExist = false;
	for( var i = 0; i < this.Controls.length; i++ )
	{	
		if ( this.Controls[i] == oItem )
		{	
			lExist	= true;
		}
	}
		
	if( !lExist ) { return false; }

	this.Controls.removeItem( oItem );
	this.ePane.removeChild(oItem.oItem);
	
	return true;
}

XAPDataList.prototype.Disable	= function ( )
{
	this.ePane.style.filter = "alpha(opacity=30)";
	this.Disabled = true;
}
XAPDataList.prototype.Enable	= function ( )
{
	this.ePane.style.filter = "alpha(opacity=100)";
	this.Disabled = false;
}