

if(!window.RadAjaxPanelNamespace)
{
	window.RadAjaxPanelNamespace = {};
}

RadAjaxPanelNamespace.RadAjaxPanel = function(objectData)
{
	var oldInstance = window[objectData.ClientID];
	if (oldInstance != null && typeof(oldInstance.Dispose) == "function")
	{
		window.setTimeout(function(){oldInstance.Dispose()}, 100); 
	}
	
	try
	{
		if (typeof(document.readyState) == "undefined" || 
			document.readyState == "complete" || 
			window.opera)
		{
			this._constructor(objectData);
		}
		else if (window.addEventListener && navigator.userAgent.indexOf("Safari") != -1)
		{
			var thisObject = this;
			var eventHandler = function()
			{
				thisObject._constructor(objectData);
			};
			
			window.addEventListener("load", eventHandler, true);
		}
		else
		{
			var thisObject = this;
			RadAjaxNamespace.EventManager.Add(window,"load", function()
				{
					thisObject._constructor(objectData);
					thisObject = null;
				}, objectData.ClientID);
		}
	}
	catch(e)
	{
		RadAjaxNamespace.OnError(e, objectData.ClientID)
	}
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype = new RadAjaxNamespace.RadAjaxControl();

//Dummy property to identify the AJAX Panel - Safari doesn't support isPrototypeOf :-(
RadAjaxPanelNamespace.RadAjaxPanel.prototype.IsAjaxPanel = true;

RadAjaxPanelNamespace.RadAjaxPanel.prototype._constructor = function (objectData)
{
	try
	{
		for (var member in objectData)
		{
			if(member == "ClientEvents")
			{
				var clientEvents = objectData[member]
				for (var clientEvent in clientEvents)
				{
					if (typeof(clientEvents[clientEvent]) != "string")
					continue;
					
					if (clientEvents[clientEvent] != "")
					{
						var handlerString = clientEvents[clientEvent];

						if (handlerString.indexOf("(") != -1)
						{
						
							// function call
							this[clientEvent] = handlerString;
						}
						else
						{
							// function reference
							this[clientEvent] = eval(handlerString);
						}
					}
					else
					{
						this[clientEvent] = null;
					}
				}
				continue;
			}

			this[member] = objectData[member];
		}

		var control = document.getElementById(this.ClientID);
		if (control == null)
			return;

		var postDataValue = document.getElementById(this.ClientID + "PostDataValue");
        if(postDataValue == null)
        {
			control = null;
            return;   
        }
        
        postDataValue.value = "";

		var activeElement = document.getElementById(objectData.ActiveElementID);
		if(activeElement != null && activeElement.focus != null)
		{
			var activeElementId = this;
			window.setTimeout(function(){ try{document.getElementById(activeElementId).focus();}catch(e){}},200);
		}
		activeElement = null;
		control = null;
	
		this.ConfigureLoadingPanelSettings();
	}
	catch(e)
	{
		RadAjaxNamespace.OnError(e, objectData.ClientID)
	}
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.ConfigureLoadingPanelSettings = function()
{
	this.AjaxSettings = 
		[ 
			{
				InitControlID : this.ClientID,
				UpdatedControls : 
					[
						{
							ControlID : this.ClientID,
							PanelID : this.LoadingPanelID
						}
					]
			} 
		];
		
	this.PostbackControlIDServer = this.ClientID;
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.GetAjaxSetting = function(clientID)
{
	return this.AjaxSettings[0];
}

RadAjaxPanelNamespace.RadAjaxPanel.prototype.AjaxRequestWithTarget = function (eventTarget, eventArgument)
{
	this.AsyncRequest(eventTarget, eventArgument);
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.AjaxRequest = function (eventArgument)
{
	this.AjaxRequestWithTarget(this.UniqueID, eventArgument);
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.AsyncRequest = function (eventTarget, eventArgument, e)
{
    this.PrepareForAsyncRequest(eventTarget);

	RadAjaxNamespace.AsyncRequest(eventTarget, eventArgument, this.ClientID, e);
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.Dispose = function()
{
	if (this.disposed == true)
		return;
	this.disposed = true;
	
	try
	{
		//DESHEV: leaks
	    //RadAjaxNamespace.DestroyTree(this.Control);
		RadAjaxNamespace.EventManager.CleanUpByClientID(this.ClientID);
		for (var member in this)
		{
			this[member] = null;
			delete this[member];
		}
	}
	catch(e)
	{
		//
	}
};

RadAjaxPanelNamespace.AsyncRequest = function(eventTarget, eventArgument, clientID, e)
{
	var instance = window[clientID];
	if(instance != null && typeof(instance.AsyncRequest) == "function")
	{
	    instance.AsyncRequest(eventTarget, eventArgument, e);
	}
};

RadAjaxPanelNamespace.AsyncRequestWithOptions = function(options, clientID, e)
{
	var instance = window[clientID];
	if(instance != null && typeof(instance.AsyncRequestWithOptions) == "function")
	{
	    instance.AsyncRequestWithOptions(options, e);
	}
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.AsyncRequestWithOptions = function(options, e)
{
    this.PrepareForAsyncRequest(options.eventTarget);
	RadAjaxNamespace.AsyncRequestWithOptions(options, this.ClientID, e);
};

RadAjaxPanelNamespace.RadAjaxPanel.prototype.PrepareActiveElement = function(eventTarget)
{
	var activeElement = document.activeElement;
	if (!activeElement)
	{
		var clientID = eventTarget.split("$").join(":");
		activeElement = document.getElementById(clientID);
	}
	
	if(activeElement && activeElement.id)
	{   
	    var postDataValue = document.getElementById(this.ClientID + "PostDataValue");
	    if(postDataValue)
	    {
	        postDataValue.value = this.ClientID + ",ActiveElement," + activeElement.id + ";";
	    }
	}
}

RadAjaxPanelNamespace.RadAjaxPanel.prototype.PrepareForAsyncRequest = function(eventTarget)
{
	this.PrepareActiveElement(eventTarget);	
};

//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
    if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
    {
        Sys.Application.notifyScriptLoaded();
    }
}
//END_ATLAS_NOTIFY

