registerNamespace("Phoenix.Widgets");

/*
	registerThisWidget()
	
	1. Register this widget's namespace identifier
	2. Register the widget in Phoenix.Widgets._InstantiatedControls, so its presence will be discoverable by other controls/general javascript on any page it's included in.
*/

Phoenix.Widgets.registerThisWidget = function (fullyqualifiedidentifier, controlobj)
{
	// Given the desired fully-qualified namespace identifier as a text string ("x.y.z"), register this control's namespace object (along with any necessary parent identifiers)
	var widgetindexonpage = 0;
	
	registerNamespace(fullyqualifiedidentifier);	// I can't think how or why anyone would be registering a widget unless they'd already defined the namespace for it (even implicitly), but it won't hurt to check just in case

	if(typeof(controlobj) != "undefined")
	{
		// Define list of instantiated controls to allow inter-widget communication
		if(typeof(Phoenix.Widgets._InstantiatedControls) == "undefined")	// If not already done, define the array to hold all instantiated controls on page
			Phoenix.Widgets._InstantiatedControls = new Array();
			
		widgetindexonpage = Phoenix.Widgets._InstantiatedControls.length;
		Phoenix.Widgets._InstantiatedControls.push(controlobj);					// and append this control to it
	}

	// Now we need a reference to the control's newly-created namespace *object* ("z", in the example above)
	var nsParts = fullyqualifiedidentifier.split(".");
	var root = window;
	for(var i=0; i<nsParts.length; i++)
		root = root[nsParts[i]];
	
	// Now, save the passed-in Javascript object reference (IMPORTANT: this is the difference between registerThisWidget() and registerThisControl()) that's a reference to the HTML element object this widget represents.
	//root.rootElement = rootelementobjref;
	
	return(widgetindexonpage);	// Finally, return this widget's index number in the global widgets array (Phoenix.Widgets._InstantiatedControls) in case it ever needs to refer to itself from the window scope (eg, from an asynchronous callback like setTimeout() in IE)
};




// Run on window.onload, to initialise widgets ONCE THE DOM HAS LAID THEM OUT CORRECTLY.
// Initialising them synchronously when they're defined in code means different browsers may return misleading values for their dimensions (eg, if the page must be re-flowed when later content is loaded, the value returned while initialising will become incorrect), or simply return "0".

Phoenix.Widgets.initWidgets = function ()
{
	if(!namespaceDefined("Phoenix.Widgets._InstantiatedControls"))	// Could have used requireNamespace() here, but if no controls defiend we want it to exit quietly, not throw a noisy exception
		return(false);	// No controls defined on page, so exit silently
	for(i in Phoenix.Widgets._InstantiatedControls)
	{
		if(Phoenix.Widgets._InstantiatedControls[i]._render)
			Phoenix.Widgets._InstantiatedControls[i]._render();
	}
	
	return(Phoenix.Widgets._InstantiatedControls.length);
}

window.onload = Phoenix.Widgets.initWidgets;















// Work around IE's lack of additional parameter-passing in asynchronous callback functions like setTimeout/setInterval, etc by saving staet (including "this", if necessary) to a global array:
Phoenix.Widgets.saveLocalState = function (uniqueid, state)
{
	if(!namespaceDefined("Phoenix.Widgets._SavedState"))
		registerNamespace("Phoenix.Widgets._SavedState");
		
	Phoenix.Widgets._SavedState[uniqueid] = state;
};


Phoenix.Widgets.getLocalState = function (uniqueid)
{
	if(namespaceDefined("Phoenix.Widgets._SavedState"))
		return(Phoenix.Widgets._SavedState[uniqueid]);
	
	return(null);
};