﻿Type.registerNamespace("Infragistics.Web.UI");


$IG.CollectionsManager = function(control, collections)
{
	///<summary>
	/// For Internal Use Only.
	/// An object that manages all ObjectCollections of an Infragistics.Web.UI.Control.
	///</summary>
	this._control = control;
	this._collections = collections;
	this._count = collections ? collections.length : 0;
	this._itemCollections = [];
	this._clientStateManagers = [];
	this._items = [];
	this._itemCount = [];
	this._uiBehaviors = []; 
	for(var i = 0; i < this._count; i++)
	{
		this._itemCount[i] = 0;
		this._items[i] = {};
		this._clientStateManagers[i] = new $IG.CollectionClientStateManager(collections[i]);       
	}
}

$IG.CollectionsManager.prototype =
{
	get_collection: function(index)
	{
		///<summary>
		/// Gets the ObjectCollection at the specified index.
		///</summary>    
		return this._collections ? this._collections[index] : null;
	},

	get_count: function()
	{
		///<summary>
		/// Gets the amount of Collections that belong to the control.
		///</summary>    
		return this._count;
	},

	get_allTransactionLists: function()
	{
		///<summary>
		/// Returns all json Transaction Lists of all collections in a server friendly format.
		///</summary>    
		var state = [];
		for (var i = 0; i < this._count; i++)
			state[i] = this.get_transactionList(i);
		return state;
	},

	get_transactionList: function(index)
	{
		///<summary>
		/// Returns the json Transaction List of the collection at the specified index.
		///</summary>    
		return this._clientStateManagers[index].get_transactionList()
	},

	register_collection: function(index, collectionType, noBehavior)
	{
		///<summary>
		/// Registers a collection with the CollectionManager.
		///</summary>    
		var collection = this._itemCollections[index] = new collectionType(this._control, this._clientStateManagers[index], index, this);
		return collection;
	},

	registerUIBehaviors: function(collection)
	{
		///<summary>
		/// Creates a UIBehaviors object for the specified collection. 
		///</summary>    
		var index = collection._index;
		this._uiBehaviors[index] = new $IG.UIBehaviorsObject(this._control, collection)
	},

	getItemCount: function(index)
	{
		///<summary>
		/// Retunrs the number of items in the collection at the specified index. 
		///</summary>    
		return this._itemCount[index];
	},

	getUIBehaviorsObj: function(index)
	{
		///<summary>
		/// Returns the UIBehaviors object of the collection at the specified index.
		///</summary>    
		return this._uiBehaviors[index];
	},

	addObject: function(index, adr, object)
	{
		///<summary>
		/// Adds the specified item to the collection at the specified index.
		///</summary>    
		this._items[index][adr] = object;
		var uiBehaviorObj = this._uiBehaviors[index];
		if (uiBehaviorObj)
		{
			if (object._getFlags().getSelected())
				uiBehaviorObj.select(object);
		}
		this._itemCount[index]++;
	},

	getObject: function(index, adr)
	{
		///<summary>
		/// Returns the item at the specified address from the collection at the specified index.
		///</summary>    
		return this._items[index][adr];
	},

	getServerCollection: function(vse)
	{
		///<summary>
		/// Returns a server friendly array of all ClientState for all collections.
		///</summary>    
		if (vse)
		{
			var collections = [];
			var index = this._collections ? this._collections.length : 0;
			while (index-- > 0)
			{
				collections[index] = {};
				var csm = this._clientStateManagers[index];
				for (var adr in this._collections[index])
					collections[index][adr] = csm.get_serverProps(adr);
			}
			return collections;
		}
		return null;
	},

	dispose: function()
	{
		///<summary>
		/// Disposes of all objects created by the Collection manager.
		///</summary>
		if (!this._itemCollections)
			return;
		var count = this._itemCollections.length;
		for (var i = 0; i < count; i++)
		{
			if (this._uiBehaviors[i])
				this._uiBehaviors[i].dispose();
			this._itemCollections[i].dispose();
			var item = this._items[i]
			for (var adr in item)
			{
				
				
				
				var obj = item[adr];
				if (obj && obj.dispose)
					obj.dispose();
			}
		}
		this._control = null;
		this._collections = null;
		this._itemCollections = null;
		this._clientStateManagers = null;
		this._items = null;
		this._itemCount = null;
		this._uiBehaviors = null;
	}
};

$IG.CollectionsManager.registerClass('Infragistics.Web.UI.CollectionsManager');




$IG.ObjectsManager = function(control, objects)
{
    ///<summary>
    /// Manages all objects for a control. 
    ///</summary>    
	this._objects = objects;
	this._control = control;
	this._clientStateManagers = [];
	this._objectCollection = [];
	this._count = objects ? objects.length : 0;
}

$IG.ObjectsManager.prototype =
{
	get_objectProps: function(index)
	{
		///<summary>
		/// Returns the json ClientState from the server of the object at the specified index.
		///</summary>    
		return this._objects ? this._objects[index] : null;
	},

	get_count: function()
	{
		///<summary>
		/// Returns the amount of objects on the control. 
		///</summary>    
		return this._count;
	},

	register_object: function(index, object)
	{
		///<summary>
		/// Registers the object at the specified index with the ObjectManager.
		///</summary>    
		var objects = this._objects;
		if (!objects || !object)
			return;
		this._clientStateManagers[index] = object._csm;
		this._objectCollection[index] = (object);
		var objectProps = objects[index];

		objectProps.objectsManager = new $IG.ObjectsManager(object, objectProps[1]);
		objectProps.collectionsManager = new $IG.CollectionsManager(object, objectProps[2]);
		objectProps.registered = true;

		object._createObjects(objectProps.objectsManager);
		object._createCollections(objectProps.collectionsManager);
	},

	get_object: function(index)
	{
		///<summary>
		/// Returns the object at the specifeid index.
		///</summary>    
		return this._objectCollection[index];
	},

	get_allTransactionLists: function()
	{
		///<summary>
		/// Returns all json Transaction Lists in a Server Friendly format for all objects in the object manager.
		///</summary>    
		var state = [];
		for (var i = 0; i < this._count; i++)
			state[i] = this.get_transactionList(i);
		return state;
	},

	get_csm: function(index)
	{
		///<summary>
		/// Returns the ClientStateManager for the object at the specified index. 
		///</summary>    
		return this._clientStateManagers[index];
	},

	getServerObjects: function(vse)
	{
		///<summary>
		/// Returns the ClientState for all objects in a Server friendly format.
		///</summary>    
		var objects = [], index = this._objects ? this._objects.length : 0;
		while (index-- > 0)
		{
			var object = this._objects[index];
			if (object.registered)
			{
				var csm = this._clientStateManagers[index];
				var state = [[csm.get_serverProps(vse), object.objectsManager.getServerObjects(vse), object.collectionsManager.getServerCollection(vse)]];
				state[1] = [csm.get_transactionList(), object.collectionsManager.get_allTransactionLists()];
				state[2] = this._objectCollection[index]._saveAdditionalClientState();
				objects[index] = state;
			}
			else
			{
				

				
				objects[index] = this._getUnRegisteredServerObjects(object);
			}
		}
		return objects;
	},

	_getUnRegisteredServerObjects: function(obj)
	{
		var returnObjects = [];
		var objs = obj[1];
		for (var i = 0; objs && i < objs.length; i++)
		{
			returnObjects.push(this._getUnRegisteredServerObjects(objs[i]));
		}
		return [[obj[0], returnObjects, obj[2]], [null, null], [null]];
	},

	get_transactionList: function(index)
	{
		///<summary>
		/// Returns the json TransactionList for the Object at the specified index.
		///</summary>    
		var csm = this._clientStateManagers[index];
		if (csm)
			return csm.get_transactionList();
		return null;
	},

	dispose: function()
	{
		///<summary>
		/// Disposes of all objects created by the ObjectManager.
		///</summary>
		var items = this._objectCollection;
		if (!items)
			return;
		var i = items.length;
		while (i-- > 0)
		{
			if (items[i] && (!Sys.Component.isInstanceOfType(items[i]) || !Sys.Application._disposing))
				items[i].dispose();
		}
		this._control = null;
		this._objects = null;
		this._clientStateManagers = null;
		this._objectCollection = null;
	}

};

$IG.ObjectsManager.registerClass('Infragistics.Web.UI.ObjectsManager');

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();