﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../Common/AjaxCommonBase.js" />

Type.registerNamespace("Crown.Web.Ajax.Controls");

// --------------------------------------------------------------------------
// DynamicContentManager Ctrl
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.DynamicContentManager = function(element) 
{
	this._controls = null;
	this._contentReceivedHandler = Function.createDelegate(this, this._onContentReceived);
	Crown.Web.Ajax.Controls.DynamicContentManager.initializeBase(this, [element]);
}
Crown.Web.Ajax.Controls.DynamicContentManager.prototype = 
{
    initialize: function() 
    {
        Crown.Web.Ajax.Controls.DynamicContentManager.callBaseMethod(this, 'initialize');
    },
    dispose: function() 
    {
		Crown.Web.Ajax.Controls.DynamicContentManager.callBaseMethod(this, 'dispose');
    },
    
    // Gets the list of client controls
    get_controls: function()
    {
        // Create an array to hold the client controls if one does not exist
        if (this._controls == null)
            this._controls = [];
        
        // Return the list of client controls
        return this._controls;
    },
    
    // Causes our child controls to update their content
    update: function(contentId)
    {
		var identifiers = [];
		
		// Get the list of child controls to be updated
		var controls = this.get_controls();
		
		// Create an array containing the identifiers of all the child controls to be updated
		for (var index = 0; index < controls.length; index ++)
			Array.add(identifiers, controls[index].get_id());
		
		// Request the content from the server
		this.performCallback(contentId, identifiers, this._contentReceivedHandler);
    },
    
    // Called when we receive client control content from the server
    _onContentReceived: function(content)
    {
		var clientControl = null;
		
		// Get the list of key / value pairs
		var controlContents = Sys.Serialization.JavaScriptSerializer.deserialize(content, false);
		
		// Step through each control and update it's content
		for (var index = 0; index < controlContents.length; index ++)
		{
			if ((clientControl = $find(controlContents[index].key)) == null)
				continue;
				
			clientControl.update(controlContents[index].value);
		}
    }
}
Crown.Web.Ajax.Controls.DynamicContentManager.registerClass('Crown.Web.Ajax.Controls.DynamicContentManager', Crown.Web.Ajax.Controls.AjaxControlBase);
Type.registerNamespace('Crown.Web.Ajax.Controls');Crown.Web.Ajax.Controls.Resource={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();