﻿Type.registerNamespace("Infragistics.Web.UI");

/******************************************Collections Manager**************************/
$IG.CollectionsManager = function(control, collections)
{
    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)
    {
        return this._collections[index];
    },
    
    get_count:function()
    {
        return this._count;
    },
    
    get_allTransactionLists:function()
    {
        var state = [];
        for(var i = 0; i < this._count; i++)
	        state[i] = this.get_transactionList(i);
	    return state;
    },
    
    get_transactionList:function(index)
    {
        return this._clientStateManagers[index].get_transactionList()
    },
    
    register_collection:function(index, collectionType, noBehavior)
    {
        var collection = this._itemCollections[index] = new collectionType(this._control, this._clientStateManagers[index], index, this);
        return collection;
    },
    
    registerUIBehaviors:function(collection)
    {
        var index = collection._index;
        this._uiBehaviors[index] = new $IG.UIBehaviorsObject(this._control, collection)
    },
    
    getItemCount:function(index)
    {
        return this._itemCount[index];
    },
    
    getUIBehaviorsObj:function(index)
    {
        return this._uiBehaviors[index];
    },
    
    addObject:function(index, adr, object)
    {
        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)
    {
        return this._items[index][adr];
    }, 
    
    getServerCollection:function(vse)
    {
        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()
    {
        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)
{
    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)
    {
        return this._objects[index];
    },
    
    get_count:function()
    {
        return this._count;
    },
    
    register_object:function(index, object)
    {
        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)
    {
        return this._objectCollection[index];
    },
    
    get_allTransactionLists:function()
    {
        var state = [];
        for(var i = 0; i < this._count; i++)
	        state[i] = this.get_transactionList(i);
	    return state;
    },
    
    get_csm:function(index)
    {
        return this._clientStateManagers[index];
    },
    
    getServerObjects:function(vse)
    {
        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)
    {
        var csm = this._clientStateManagers[index];
        if(csm)
            return csm.get_transactionList();
        else
            return null;
    }, 
    
    dispose:function()
    {
        /*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();