﻿function WebForm_CallbackComplete() 
{
    for (var i = 0; i < __pendingCallbacks.length; i++) {
        var callbackObject = __pendingCallbacks[i];
        if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
            __pendingCallbacks[i] = null;
            WebForm_ExecuteCallback(callbackObject);
            if (!callbackObject.async) {
                __synchronousCallBackIndex = -1;
            }
            
            var callbackFrameID = "__CALLBACKFRAME" + i;
            var xmlRequestFrame = document.getElementById(callbackFrameID);
            if (xmlRequestFrame) {
                xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
            }
        }
    }
}

Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.ControlItemContainer = function (element)
{
	Telerik.Web.UI.ControlItemContainer.initializeBase(this, [element]);
	
	this._childControlsCreated = false;
	this._enabled = true;
	this._log = new Telerik.Web.UI.ChangeLog();
	this._enableClientStatePersistence = false;
	this._eventMap = new Telerik.Web.UI.EventMap();
	this._attributes = new Telerik.Web.UI.AttributeCollection(this);
	this._children = null;
}

Telerik.Web.UI.ControlItemContainer.prototype =
{
	initialize : function ()
	{
		Telerik.Web.UI.ControlItemContainer.callBaseMethod(this, 'initialize');

		this._ensureChildControls();
		this._log.initialize();
		this._initializeEventMap();
	},
	
	dispose : function ()
	{	
		this._eventMap.dispose();

		for (var i = 0; i < this._getChildren().get_count(); i++)
		{
			this._getChildren().getItem(i)._dispose();
		}

		Telerik.Web.UI.ControlItemContainer.callBaseMethod(this, 'dispose');
	},
	
	trackChanges : function ()
	{
		this._enableClientStatePersistence = true;
	},
	
	set_enabled: function(value)
	{
		this._enabled = value;
	},
	
	get_enabled: function()
	{
		return this._enabled;
	},
	
	commitChanges : function ()
	{
		this.updateClientState();
		this._enableClientStatePersistence = false;
	},
	
	get_attributes : function ()
	{
		return this._attributes;
	},
	
	set_attributes : function (value)
	{
		this._attributes._load(value);
	},
	
	
	/*------------- Private Methods -----------*/
	_initializeEventMap : function()
	{
		this._eventMap.initialize(this);
	},

	_getChildren : function ()
	{
		this._ensureChildControls();
		return this._children;
	},
	
	_extractErrorMessage : function (error)
	{
		if (error.get_message)
		{
			// AJAX Exception
			return error.get_message();
		}
		else
		{		
			// ASP.NET callback return error messages in the following form:
			// <exception message><eventValidationLength>|<eventValidationString> e.g.
			// Object reference not set to an instance of an object.56|/wEWAwLM2oLGBwLs0bLrBgLs0fbZDC3VMN/DL8xzHgo9Pw1ztfesraLy
			return error.replace(/(\d*\|.*)/,"");
		}
	},

	_notifyPropertyChanged : function (propertyName, propertyValue)
	{
		
	},
	
	_childInserting : function (index, item, parent)
	{
		
	},
	
	_childInserted : function (index, item, parent)
	{
		if (!parent._childControlsCreated) return;
		if (!parent.get_element()) return;
		
		itemElement = item._createDomElement();
		var parentListElement = parent.get_childListElement();
		
		if (!parentListElement) 
			parentListElement = parent._createChildListElement();
		
		var nextItem = item.get_nextSibling();
		var nextItemElement = nextItem ? nextItem.get_element() : null;
		parent.get_childListElement().insertBefore(itemElement, nextItemElement);
		
		if (!item.get_element()) 
		{
			item.set_element(itemElement);
			item._initializeRenderedItem();
		}
		else 
		{
			item.set_element(itemElement);
		}
	},
	
	_childrenCleared : function (parent)
	{
		for(var i = 0; i < parent._getChildren().get_count(); i++)
		{
			parent._getChildren().getItem(i)._dispose();
		}
		
		var childListElement = parent.get_childListElement();
		if(childListElement)
			childListElement.innerHTML = "";
	},
	
	_childRemoving : function (child)
	{
		this._logRemoving(child);
	},
	
	_childRemoved : function (item, parent)
	{
		item._dispose();
	},

	_createChildListElement : function()
	{
		throw Error.notImplemeneted();
	},

	_createDomElement : function()
	{
		throw Error.notImplemented();
	},

	_getControl : function ()
	{
		return this;
	},
	
	_logInserted : function (item)
	{
		if (!item.get_parent()._childControlsCreated || !this._enableClientStatePersistence) return;
		
		this._log.logInsert(item);
		
		var children = item._getAllItems();
		for (var i = 0; i < children.length; i++)					
		{
			this._log.logInsert(children[i]);
		}
	},
	
	_logRemoving : function (item)
	{
		if (this._enableClientStatePersistence)
			this._log.logDelete(item);
	},
	
	_logClearing : function (item)
	{
		if (this._enableClientStatePersistence)
			this._log.logClear(item);
	},
	
	_itemPropertyChanged : function (item, property, value)
	{
		if (this._enableClientStatePersistence)
			this._log.logPropertyChanged(item, property, value);
	},
	
	_ensureChildControls : function ()
	{
		if (!this._childControlsCreated)
		{		
			this._createChildControls();
			this._childControlsCreated = true;
		}
	},
	
	_extractItemFromDomElement : function (element)
	{
        this._ensureChildControls();
		while (element && element.nodeType !== 9)
		{
			if (element._item && this._verifyChildType(element._itemTypeName))
				return element._item;
				
			element = element.parentNode;
		}
		return null;
	},
	
	_verifyChildType : function (targetTypeName)
	{
		return targetTypeName === this._childTypeName;
	},
	
	_getAllItems : function ()
	{
		var allItems = [];
		for (var i = 0; i < this._getChildren().get_count(); i++)
		{
			var item = this._getChildren().getItem(i);
			Array.add(allItems, item);
			Array.addRange(allItems, item._getAllItems());
		}
		
		return allItems;
	},
	
	_findItemByText : function (text)
	{
		var allItems = this._getAllItems();
		for (var i = 0; i < allItems.length; i++)
		{
			if (allItems[i].get_text() == text)
				return allItems[i];
		}
		return null;
	},
	
	_findItemByValue : function (value)
	{
		var allItems = this._getAllItems();
		for (var i = 0; i < allItems.length; i++)
		{
			if (allItems[i].get_value() == value)
				return allItems[i];
		}
		return null;
	},
	
	_findItemByAttribute : function(attributeName, value)
	{
		var allItems = this._getAllItems();
		for (var i = 0; i < allItems.length; i++)
		{
			if (allItems[i].get_attributes().getAttribute(attributeName) == value)
				return allItems[i];
		}
		return null;
	},
	
	_findItemByHierarchicalIndex : function (index)
	{
		var currentItem = null;
		var container = this;
		var indexes = index.split(":");
		for (var i = 0; i < indexes.length; i++)
		{
			var currentIndex = parseInt(indexes[i]);
			if (container._getChildren().get_count() <= currentIndex)
				return null;

			currentItem = container._getChildren().getItem(currentIndex);
			container = currentItem;
		}

		return currentItem;
	}
}

Telerik.Web.UI.ControlItemContainer.registerClass("Telerik.Web.UI.ControlItemContainer", Telerik.Web.UI.RadWebControl);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();