﻿/*
Creates the JavaScript "namespace" com.capitolmedia and its members.
*/

//=============================================================================

// Ensure the 'com.capitolmedia' namespace exists and is an object.
var com;
if (com) {
	if (typeof com != "object") {
		throw new Error("Cannot create JavaScript namespace object 'com' because the symbol is already defined and is not an object.");
	}
}
else {
	com = {};
}

// Ensure the 'com.capitolmedia' namespace exists and is an object.
if (com.captiolmedia) {
	if (typeof com.capitolmedia != "object") {
		throw new Error("Cannot create JavaScript namespace object 'com' because the symbol is already defined and is not an object.");
	}
}
else {
	com.capitolmedia = {};
}

//=============================================================================

/**
Anonymous initialization function for com.capitolmedia namespace.
*/
(
	function() {

		// The (private) namespace map.
		var namespaces;

		/**
		Creates a namespace object and adds it to the namespace map.
		@param fqname: string
			The fully-qualified name being declared.
		@return
			The namespace object.
		*/
		com.capitolmedia.DeclareNamespace = function(fqname) {

			// Ensure the namespaces map is initialized.
			if (!namespaces) {
				namespaces = {};
			}

			// If the namespace already exists in the map...
			var obj = namespaces[fqname];
			if (obj) {
				return obj;
			}

			// If the fully-qualified name is invalid...
			if (fqname.length > 0 && 
				(fqname.charAt(0) == '.' || fqname.charAt(fqname.length - 1) == '.' || fqname.indexOf("..") != -1)) {
				throw new Error("Attempt to declare the illegal namespace: " + fqname);
			}

			// Start from the global namespace.
			obj = window;

			// For each part of the fully-qualified name...
			var parts = fqname.split('.');
			fqname = "";
			for (var i = 0; i < parts.length; ++i) {

				// If this element of the namespace doesn't exist yet...
				var part = parts[i];
				if (!obj[part]) {
					// Create it
					obj[part] = {};
				}

				// Continue from this element of the namespace.
				obj = obj[part];

				// Rebuild the fully qualified name, one element at a time.
				if (fqname.length > 0) {
					fqname += ".";
				}
				fqname += part;

				// If this namespace is not already in the map...
				if (!namespaces[fqname]) {
					// Add it.
					namespaces[fqname] = obj;
				}
			}

			return obj;
		}
		
		/**
		Whether a namespace is registered in the namespace map.
		@param fqname: string
			The fully-qualified name to be tested.
		@return
			True if the fully-qualified name is in the namespace map.
		*/
		com.capitolmedia.IsNamespaceDeclared = function(fqname) {
			return namespaces[fqname] != null;
		}
		
		// Declare the global namespace.
		com.capitolmedia.DeclareNamespace("");

		// Declare the com.capitolmedia namespace itself.
		com.capitolmedia.DeclareNamespace("com.capitolmedia");
	}
)(); // Declared and immediately invoked.

