﻿Type.registerNamespace("Infragistics.Web.UI");

/******************************************Collections Manager**************************/
$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;
    if(collections == null)
        this._count = 0; 
    else
        this._count = this._collections.length
    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[index];
    },
    
    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 = [];
            for(var index in this._collections)
            {
                collections[index] = {};
                var csm = this._clientStateManagers[index];
                for(var adr in this._collections[index])
                     collections[index][adr] = csm.get_serverProps(adr);
            }
            return collections;
        }
        else
            return null;
    
    },
    
    dispose:function()
    {
        ///<summary>
        /// Disposes of all objects created by the Collection manager.
        ///</summary>    
        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)
                item[adr].dispose();
        }    
    }
};

$IG.CollectionsManager.registerClass('Infragistics.Web.UI.CollectionsManager');
/******************************************END Collection Manager**********************/


/******************************************Object Manager**************************/
$IG.ObjectsManager = function(control, objects)
{
    ///<summary>
    /// Manages all objects for a control. 
    ///</summary>    
    this._objects = objects;
    this._control = control;
    this._clientStateManagers = [];
    this._objectCollection = [];
    if(objects == null)
        this._count = 0; 
    else
        this._count = this._objects.length;
}

$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[index];
    },
    
    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>    
        this._clientStateManagers[index] = object._csm;
        this._objectCollection[index] = (object);
        var objectProps = this._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);
        
        this._objects[index] = objectProps; 
    },
    
    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 = [];
        for(var index in this._objects)
        {
            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
            {
                /* NOTE: this means the object is sending information back and forth to the server and never using it*/
                objects[index] = [[[object[0][0]], null, null],[null, null, null]];
            }
             
        }
        return objects;
    
    },
    
    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();
        else
            return null;
    }, 
    
    dispose:function()
    {
        ///<summary>
        /// Disposes of all objects created by the ObjectManager.
        ///</summary>    
        
        /*var count = this._objectCollection.length;
        for(var i = 0; i < count; i++)
        {
            if(this._objectCollection[i])
                this._objectCollection[i].dispose();
        }*/
    }

};

$IG.ObjectsManager.registerClass('Infragistics.Web.UI.ObjectsManager');
/******************************************END Object Manager**********************/
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();