
var SdEcPageInterface = {};		// public  global identifiers
var $_EcPageInterface = {};		// private global identifiers

$_EcPageInterface.deferred = new Array();

$_EcPageInterface.queue = {};	// queue of args for previously requested xml documents - do not re-request xml
$_EcPageInterface.cache = {};	// cache of          previously    loaded xml documents - do not re-load    xml

$_EcPageInterface.ddt = false;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcPageInterface.signalEvent = function (event, args)
{
	SdEcEvents.signalEvent(event, args);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcPageInterface.populateContentRegion = function (contentRegionId, contentDefinitionXmlFileUrl, defer)
{
	if (!contentDefinitionXmlFileUrl) return;

	defer = (defer && defer != '0') ? true : false;

	if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + (defer ? "on-load" : "in-line") + "]");

	if (!contentRegionId            ) return;		// SdCommon.error("call to 'populate-content-region' missing 'content-region-id'");	
	if (!contentDefinitionXmlFileUrl) return;		// SdCommon.error("call to 'populate-content-region' missing 'content-definition-xml-file-url'");	

	SdEcValues.setVariable("this-content-definition-file-url", contentDefinitionXmlFileUrl);
	var parameters = SdEcValues.getParameters(document);

	if (defer)
	{
		SdEcProgress.incTodo(1);

		var args = {
			"contentRegionId"             :             contentRegionId,
			"contentDefinitionXmlFileUrl" : contentDefinitionXmlFileUrl,
			"parameters"                  :                  parameters
		};

		$_EcPageInterface.deferred[$_EcPageInterface.deferred.length] = args;
	}
	else
	{
		$_EcPageInterface.doPopulateContentRegion(contentRegionId, contentDefinitionXmlFileUrl, parameters, "  now  ");
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


$_EcPageInterface.onLoadHandlerForDeferredCalls = function ()
{
	var  list = $_EcPageInterface.deferred;
	if (!list) return;

	var count = list.length;
	for (var k = 0; k < count; k++)
	{
		var args = list[k];

		var contentRegionId             = args["contentRegionId"            ];
		var contentDefinitionXmlFileUrl = args["contentDefinitionXmlFileUrl"];
		var parameters                  = args["parameters"                 ];

		$_EcPageInterface.doPopulateContentRegion(contentRegionId, contentDefinitionXmlFileUrl, parameters, " later ");

		SdEcProgress.incDone(1);
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_EcPageInterface.doPopulateContentRegion = function (contentRegionId, contentDefinitionXmlFileUrl, parameters, stage)
{
	if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + stage + "]");

	// we will load the xml file containing "content-definitions" via ajax; but only once
	// -- so we queue up the requests (i.e. args) -- processing the same response for each
	// request in a "response handler" as though it were loaded separately via ajax ...

	var argsForResponseHandler = {
		"content-region-id"               : contentRegionId,	
		"content-definition-xml-file-url" : contentDefinitionXmlFileUrl,
		"parameters"                      : parameters
	};

	var xmlHttp = $_EcPageInterface.cache[contentDefinitionXmlFileUrl];         // cache of responses
	if (xmlHttp)
	{
		// xml file was previously loaded -- call the handler as though it was just loaded

		if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + " cache " + "]");

		$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFile(xmlHttp, argsForResponseHandler);
	}
	else
	{
		// xml file not previously loaded -- but maybe the request was sent and response is pending

		var queue = $_EcPageInterface.queue[contentDefinitionXmlFileUrl];    // queue of requests
		if (queue)
		{
			// request was previously sent -- do not request again -- but queue up the args

			if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + " queue " + "]");

			queue[queue.length] = argsForResponseHandler;			
		}
		else
		{
			// request not previously sent -- do an 'ajax' request -- and queue up the args 

			queue = new Array();
			$_EcPageInterface.queue[contentDefinitionXmlFileUrl] = queue;

			if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + "request" + "]");

			queue[queue.length] = argsForResponseHandler;
			SdEcAjaxLoader.ajaxRequest(
				contentDefinitionXmlFileUrl,
				$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFileUsingList, 
				argsForResponseHandler,
				"content-definition-file", "dfn"
			);
		}
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFileUsingList = function (xmlHttp, args)
{
	var contentDefinitionXmlFileUrl = args["content-definition-xml-file-url"];
	var list  = $_EcPageInterface.queue[contentDefinitionXmlFileUrl];
	var count = list.length;
	for (var k = 0; k < count; k++)
	{
		var argsForResponseHandler = list[k];

		var contentRegionId = argsForResponseHandler["content-region-id"];
		if ($_EcPageInterface.ddt) SdCommon.log("populating '" + contentRegionId + "' [" + "respons" + "]");

		$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFile(xmlHttp, argsForResponseHandler);
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcPageInterface.loadSecondaryContent = function (elementId, key, contentDefinitionXmlFileUrl, ref)
{
	SdEcValues.setVariable("key", key);
	SdEcValues.setVariable("this-content-definition-file-url", contentDefinitionXmlFileUrl);
	var parameters = SdEcValues.getParameters(document);

	var argsForResponseHandler = {
		"content-region-id"               : elementId,
		"content-definition-xml-file-url" : contentDefinitionXmlFileUrl,
		"parameters"                      : parameters
	};

	var xmlHttp = $_EcPageInterface.cache[contentDefinitionXmlFileUrl];

	$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFile(xmlHttp, argsForResponseHandler, ref);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_EcPageInterface.ajaxResponseHandlerForContentDefinitionXmlFile = function (xmlHttp, args, ref)
{
	var contentRegionId             = args["content-region-id"              ];
	var contentDefinitionXmlFileUrl = args["content-definition-xml-file-url"];
	var parameters                  = args["parameters"                     ];

	$_EcPageInterface.cache[contentDefinitionXmlFileUrl] = xmlHttp;

	var  xmlDom = (xmlHttp ? xmlHttp.responseXML : null);
	if (!xmlDom) SdCommon.error("missing dom for: ", contentDefinitionXmlFileUrl);

	SdEcEngine.processContentDefinitionXmlFile(xmlDom, contentRegionId, parameters, ref);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.addLoadEvent($_EcPageInterface.onLoadHandlerForDeferredCalls);

