
// Type handler for "indexed-syndetics-xml" -- loads the syndetics "INDEX.XML"
// file on the first call, then queues up subsequent requests to be handled
// when the "INDEX.XML" data is available; then it uses the "INDEX.XML" data
// to load make and render the queued requests ...

SdCommon.require(SdCommon      );
SdCommon.require(SdEcAjaxLoader);
SdCommon.require(SdEcEngine    );
SdCommon.require(SdEcXml       );

var SdEcSyndetics = {};		// public  global identifiers
var $_EcSyndetics = {};		// private global identifiers

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

$_EcSyndetics.loadSyndeticsXmlAsynchronouslyAndTranslateViaXslt = function (elementId)
{
	var availableFileMap = $_EcSyndetics["available-file-map"];
	if (availableFileMap)
	{
		// map exists -- so xml file for syndetics index has been fetched and map was extracted from it

		$_EcSyndetics.loadAvailableSyndeticsXmlAsynchronouslyAndTranslateViaXslt(elementId, availableFileMap)
	}
	else
	{
		// no map exists -- add request to a queue, then make an ajax call to fetch syndetics index

		var request = { "element-id" : elementId };

		var queue = $_EcSyndetics["queue"];
		if (queue)
		{
			//  a queue exists -- so ajax call has already been made -- simply add request to queue
			queue[queue.length] = request;
		}
		else
		{
			// no queue exists -- create queue with request, then make an ajax call to fetch syndetics index

			$_EcSyndetics["queue"] = [ request ];

			// make ajax call to fetch syndetics index

			var idx = SdEcSpecifics.getSpecific(elementId, "idx");

			if (!idx) {
				var id = definition["id"];
				SdCommon.error("configuration error -- no 'idx' attribute in 'content-def' with id '" +  id + "'"
						+ " and definition is " + SdCommon.mapToText(definition), elementId);
				return;
			}
		
			idx = SdCommon.proxify(idx);

			SdEcAjaxLoader.ajaxRequest(idx, $_EcSyndetics.ajaxResponseHandlerForFetchingIndexAsXml, {"element-id" : elementId},
			                           "syndetics-index", "xml", "parse");
		}
	}
}

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

$_EcSyndetics.ajaxResponseHandlerForFetchingIndexAsXml = function (xmlHttp, args)
{
	var  xml = xmlHttp.responseXML;
	if (!xml) return;

	var availableFileMap = $_EcSyndetics.syndeticsIndexXmlToAvailableFileNameMap(xml);
	$_EcSyndetics["available-file-map"] = availableFileMap;

	var queue = $_EcSyndetics["queue"];
	if (!queue) SdCommon.error("missing queue");	

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

		var elementId = request["element-id"];

		$_EcSyndetics.loadAvailableSyndeticsXmlAsynchronouslyAndTranslateViaXslt(elementId, availableFileMap);
	}	
}

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

$_EcSyndetics.loadAvailableSyndeticsXmlAsynchronouslyAndTranslateViaXslt = function (elementId, availableFileMap)
{
	var file = SdEcSpecifics.getSpecific(elementId, "file");

	var  available = availableFileMap[file];
	if (!available) return;

	SdEcXml.loadXmlAsynchronouslyAndTranslateViaXslt(elementId);
}

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

$_EcSyndetics.syndeticsIndexXmlToAvailableFileNameMap = function (xml)
{
	var available = new Array();
	var  roots = xml.getElementsByTagName("INDEX-ROOT");
	if (!roots || roots.length != 1) return available;

	var root = roots[0];
	if (!root) return available;

	var kids = root.childNodes;
	if (!kids) return available;

	var map = {};

	var next = 0;
	var size = kids.length;
	for (var k = 0; k < size; k++)
	{
		var kid = kids[k];
		var txt =  kid.firstChild;
		if (!txt) continue;
		var fileName  = txt.nodeValue;
		map[fileName] = true;
	}

	return map;

}

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

SdEcEngine.registerTypeHandler("indexed-syndetics-xml", $_EcSyndetics.loadSyndeticsXmlAsynchronouslyAndTranslateViaXslt);

