
/**
 * Functionality needed for the very first stages of loading clm.
 *
 * @author jw5987, jb010906
 */

 
/**
 * Persistant object that can be used by any component to register variables 
 * 	used at runtime.  May also be extended later to marshal event triggering for
 *	inline page code.
 *
 * An instance of the uiResourceManager is instantiated at runtime and is 
 *	globally available.  CLM_RM is a persistant copy of the resource manager; 
 * 	you can also get an instance of the object by calling 
 *	getResourceManager(false)
 *	(true returns a handle tothe persistant version and false returns a new object.
 */
//create global uirm and populate with system variables, etc...
var CLM_RM = new uiResourceManager(true);

function getResourceManager(bReturnGlobalRM)
{
	var oRM = null;
	if (bReturnGlobalRM)
	{
		oRM = CLM_RM;
	}
	else
	{
		oRM = new uiResourceManager(false);
	}

	return oRM;
}

function uiResourceManager(isGlobalInit)
{
	//create property 'name' with value 'value'
	function set(name, value)
	{
		if ((name != null) && (name != ""))
		{
			//alert("native method set: " + name + " = " + value);
			this[name] = value;
		}	
	}
	
	//return value of property 'name'
	function get(name)
	{
		return this[name];
	}
	
	//remove property 'name' from the resource object
	function remove(name)
	{
		delete this[name];
	}
	
	//adds value to the named array 
	function put(arr, value)
	{
		if (!this.get(arr))
		{
			this[arr] = [];
		}
		this[arr].push(value);
	}
	
	//return an array of all property names
	function getKeyNames()
	{
		var arrKeyNames = [];
		var i = 0;
		for (o in this)
		{
			if (typeof(this[o]) != "function")
			{
				//alert("o:\n" + o + " = " + this[o] + "\ntype: " + typeof(o) + " " + typeof(this[o]) + "\nl: " + this.length);
				//arrKeyNames.push(o);
				arrKeyNames[i] = o;
				i++;	
			}
		}
		//alert(arrKeyNames.length);
		return arrKeyNames;	
	}
	
	//return data type of property (not too useful, all come back as string I think)
	function getKeyType(key)
	{
		return typeof(key);
	}
	
	//return the number of properties currently stored in object
	function getCount()
	{
		var count;
		count = 0;
		
		for (o in this)
		{
			if (typeof(this[o]) != "function")
			{
				//alert(typeof(this[o]));
				count += 1; 
			}
		}
			
		return count;
	}
	
	//initialization for global resource object instance
	if (isGlobalInit)
	{
		//properties
		this.module_overlays = [];
	
		//Overlay functionality for global resource object instance
		//add an overlay ref to the array, return the new length (also the pos)
		var addOverlay = function (name)
		{
			return this.module_overlays.push(name);
		};
		var setActiveOverlay = function(obj)
		{
			//place the overlay object in the resource manager
			//use .put() to stack it in an array within the resource manager.
			this.put('activeOverlay', obj);
		};
		var getActiveOverlay = function()
		{
			var overlayArr = this.get('activeOverlay');
			if(overlayArr && overlayArr.length > 0){
				return overlayArr[overlayArr.length - 1];
			}else{
				return null;
			}
		};
		var getOverlayHandle = function(name)
		{
			var o = this.get(name+"_handle");
			if (o)
			{
				return o;
			}
		};
		var removeOverlay = function(name)
		{
			//need to remove all associated resources stored in the rm object
			var index = this.getOverlayIndex(name);
			var removed_overlays = this.module_overlays.splice(index,1);

			//remove activated tabs flags
			this.remove(name+"_tabs");
			
			//remove object handle
			this.remove(name+"_handle");
			
			//reset activeOverlay to the next one in the stack
			if (this.module_overlays.length > 0)
			{
				var overlayArr = this.get('activeOverlay');
				overlayArr.pop();
				this.set('activeOverlay', overlayArr);
			}
			else
			{
				this.setActiveOverlay();
			}
		};
		var listOverlays = function()
		{
			return this.module_overlays;
		};
		var getOverlayIndex = function(name)
		{
			var index = null;
			for (var i=0; i<this.module_overlays.length; i++)
			{
				//alert(i + " of " + module_overlays.length + " = " + module_overlays[i]);
				if (this.module_overlays[i] == name)
				{
					index = i;
				}
			}
			return index;
		};
	}//END GLOBAL INSTANCE INIT
	

	
	function getState()
	{
		var arr = this.getKeyNames();
		var msg = 'CLM_RM Inspector\n=====================================\n';
		
		for (k in arr)
		{
			msg += arr[k] + " = " + this[arr[k]] + "\n";
		}
		alert(msg);
	}
	
	//////////////////////////////////////////////////////
	//internal assignments
	this.set = set;
	this.get = get;
	this.remove = remove;
	this.put = put;
	this.getKeyNames = getKeyNames;
	this.getKeyType = getKeyType;
	this.getCount = getCount;
	this.getState = getState;
	
	//global instance only
	this.addOverlay = addOverlay;
	this.listOverlays = listOverlays;
	this.removeOverlay = removeOverlay;
	this.getOverlayIndex = getOverlayIndex;
	this.getOverlayHandle = getOverlayHandle;
	this.setActiveOverlay = setActiveOverlay;
	this.getActiveOverlay = getActiveOverlay;
	
}
//END RESOURCE MANAGER OBJECT


/**
 * Replace the innerHTML of the element indicated by the id passed in with the text provided.
 */
function replaceInnerHtml(elId, text) {
	var el = document.getElementById(elId);
	if( typeof(el) != 'undefined' && el != null) {
		el.innerHTML = text;
	}
	return;
}