
var SdEcBlink2 = {};		// public  global identifiers
var $_EcBlink2 = {};		// private global identifiers

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

$_EcBlink2.toggle = function (ord, key, refUrl, ref)
{
	// <div id="BLINK-I">
	//   <div id="BLINK-V">
	//     <span id="BLINK-T" 										// element
	//       style   = "color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onclick = "javaScriptForCallToToggle"
	//     >
	//	  <span> + </span>
	//     </span>
	//     <span id="BLINK-A"
	//       style       = "text-decoration: none; color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onmouseover = 'this.style.textDecoration="underline"'
	//       onmouseout  = 'this.style.textDecoration="none"'
	//       onclick     = "javaScriptForCallToToggle"
	//     >
	//	  category + ": " + title
	//     </span>
	//     <div id="BLINK-W">&nbsp;</div>								// wNode
	//     <div id="BLINK-C"></div>									// vNode
	//   </div>
	// </div>

	// sign <- toggle-mark & toggle plus-or-minus character (element)

	var element = document.getElementById("BLINK-T" + ord);
	var last = element.lastChild;
	last = last.lastChild;
	var type = last.nodeType;
	var sign = last.nodeValue;
	last.nodeValue = (sign.indexOf('+') >= 0 ? ' - ' : ' + ');

	// put a border around the content (vNode)

	var vNode = document.getElementById("BLINK-C" + ord);
	vNode.style.border="1px solid black";
	var style = vNode.style;
	style.backgroundColor = "#FFFFCC";
	style.padding="5px";

	// display the no-break-space

	var wNode = document.getElementById("BLINK-W" + ord);
//	Element.insert(wNode,"&nbsp;");
	wNode.innerHTML = "&nbsp;";
	wNode.style.display="block";

	var content = document.getElementById("BLINK-C" + ord);

	var dataDivId = "BLINK-D" + ord;
	if (sign == ' - ')
	{
		var dataDiv = document.getElementById(dataDivId);
		content.removeChild(dataDiv);

		vNode.style.border="none";
		var style = vNode.style;
		style.backgroundColor = "#000000";

		var wNode = document.getElementById("BLINK-W" + ord);
		wNode.style.display="none";

// Element.insert(wNode,"&nbsp;");
wNode.innerHTML = "&nbsp;";
wNode.style.display="block";

		var style = vNode.style;
		style.padding="0px";

		return;
	}

	// <div>
	// </div>
	
	var newNode = document.createElement("div");
	newNode.setAttribute("id", dataDivId);

	var articleTitleNode = document.getElementById("BLINK-A" + ord);
	var articleTitleText = (articleTitleNode ? articleTitleNode.lastChild.nodeValue : "");
	var loadingNode = document.createTextNode("  " + "loading" + (articleTitleText ? ': ' : '') + articleTitleText + " ...");
	newNode.appendChild(loadingNode);

	content.appendChild(newNode);	

	SdEcPageInterface.loadSecondaryContent(dataDivId, key, refUrl, ref);
}

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

$_EcBlink2.max_id = 0;

$_EcBlink2.createBlickNode = function (category, title, key, refUrl, ref)
{
	$_EcBlink2.max_id += 1;
	var ord = $_EcBlink2.max_id;

	ref = ( ref ? ref.replace(/\[key\]/g, key) : 0);

	var javaScriptForCallToToggle = "$_EcBlink2.toggle(" + ord + ",'" + key + "','" + refUrl + "','" + ref + "')";

	// <div id="BLINK-I">										// newNode
	// </div>

	var newNode = document.createElement("div");
	newNode.id = "BLINK-I" + ord;

	//   <div id="BLINK-V">										// divNode
	//   </div>

	var divNode = document.createElement("div");
	divNode.id = "BLINK-V" + ord;

	//     <span id="BLINK-T" 										// pomNode
	//       style   = "color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onclick = "javaScriptForCallToToggle"
	//     >
	//	  <span> + </span>										// plsNode
	//     </span>

	var pomNode = document.createElement("span");
	pomNode.id = "BLINK-T" + ord;
	pomNode.setAttribute("style"  , "color:#3F5FFF; cursor:hand; cursor:pointer");
	pomNode.setAttribute("onclick", javaScriptForCallToToggle);
	var plsNode = document.createElement("span");
	var txtNode = document.createTextNode(' + ');
	plsNode.appendChild(txtNode);
	pomNode.appendChild(plsNode);

	//     <span id="BLINK-A" 										// lnkNode
	//       style       = "text-decoration: none; color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onmouseover = 'this.style.textDecoration="underline"'
	//       onmouseout  = 'this.style.textDecoration="none"'
	//       onclick     = "javaScriptForCallToToggle"
	//     >
	//	  category + ": " + title									// txtNode
	//     </span>

	var lnkNode = document.createElement("span");
	lnkNode.id = "BLINK-A" + ord;

// ----- top of old way from sd-tree.js -----
//
//	var style = vNode.style;
//	style.backgroundColor = "#FFFFCC";
//	style.padding="5px";
//
// ----- end of old way from sd-tree.js -----

	lnkNode.setAttribute("style"      ,"text-decoration: none; color:#3F5FFF; cursor:hand; cursor:pointer");
	lnkNode.setAttribute("onmouseover",'this.style.textDecoration="underline"');
	lnkNode.setAttribute("onmouseout" ,'this.style.textDecoration="none"');
	lnkNode.setAttribute("onclick"    , javaScriptForCallToToggle);

	var txtNode = document.createTextNode(category + ": " + title);
	lnkNode.appendChild(txtNode);

	//     <div id="BLINK-W">&nbsp;</div>								// expNode

	var expNode = document.createElement("div");
	expNode.id = "BLINK-W" + ord;
	var spcNode = document.createTextNode("\u00A0");
	expNode.appendChild(spcNode);

	//     <div id="BLINK-C"></div>									// uidNode

	var uidNode = document.createElement("div");
	uidNode.id = "BLINK-C" + ord;

	// ------- assemble the parts ---------

	// <div id="BLINK-I">										// newNode
	//   <div id="BLINK-V">										// divNode
	//     <span id="BLINK-T" 										// pomNode
	//       style   = "color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onclick = "javaScriptForCallToToggle"
	//     >
	//	  <span> + </span>										// plsNode
	//     </span>
	//     <span id="BLINK-A" 										// lnkNode
	//       style       = "text-decoration: none; color:#3F5FFF; cursor:hand; cursor:pointer"
	//       onmouseover = 'this.style.textDecoration="underline"'
	//       onmouseout  = 'this.style.textDecoration="none"'
	//       onclick     = "javaScriptForCallToToggle"
	//     >
	//	  category + ": " + title									// txtNode
	//     </span>
	//     <div id="BLINK-W">&nbsp;</div>								// expNode
	//     <div id="BLINK-C"></div>									// uidNode
	//   </div>
	// </div>

	divNode.appendChild(pomNode);
	divNode.appendChild(lnkNode);
	divNode.appendChild(expNode);
	divNode.appendChild(uidNode);

	newNode.appendChild(divNode);

	return newNode;
}

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

$_EcBlink2.blinkResponseHandlerForEnrichedContent= function (xmlHttp, args)
{
	SdEcResponseHandler.ajaxResponseHandlerForEnrichedContent(xmlHttp, args);

	var elementId = args["element-id"];
	var refUrl    = args["ref-url"   ];
	var ref       = args["ref"       ];

	// <div class="blink">category<a href="url">title</a></div>

	var element = document.getElementById(elementId);

	var divs  = element.childNodes;
	var count = divs.length;
	for (var k = 0; k < count; k++)
	{
		var divNode = divs[k];
		var catNode = divNode.firstChild;		// [text]category[/text]
		var lnkNode = divNode. lastChild;		// <a href="url">title</a>
		var txtNode = lnkNode.firstChild;		// [text]title[/text]

		var catagory = (catNode == lnkNode) ? "" : catNode.nodeValue;
		var title    = txtNode ? txtNode.nodeValue : "not-known";
		var key      = lnkNode.getAttribute("href");

		var newNode = $_EcBlink2.createBlickNode(catagory,title, key, refUrl,ref);
		element.replaceChild(newNode, divNode);
	}
}

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

$_EcBlink2.loadXmlAsynchronouslyAndTranslateViaXslt = function (elementId)
{
	var url  = SdEcSpecifics.getSpecific(elementId, "url" );
	var xsl  = SdEcSpecifics.getSpecific(elementId, "xsl" );
	var rurl = SdEcSpecifics.getSpecific(elementId, "rurl");
	var ref  = SdEcSpecifics.getSpecific(elementId, "ref" );

	var xslDoc = SdEcXdocLoader.fetchXMLDoc(xsl);
	url = SdCommon.proxify(url);

	var ajaxResponseHandlerArgs = {
		"element-id" : elementId,
		"xsl-doc"    :    xslDoc,
		"ref-url"    :      rurl,
		"ref"        :       ref
	};

	SdEcAjaxLoader.ajaxRequest(url, $_EcBlink2.blinkResponseHandlerForEnrichedContent, ajaxResponseHandlerArgs, "blink-first-xml","xml");
}

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

SdEcEngine.registerTypeHandler("blink-2", $_EcBlink2.loadXmlAsynchronouslyAndTranslateViaXslt);

