
// Used by SdEcTree to open toggle links and embedded links within the toggled
// html (via the syndetics' function '__doLinkPostBack')

var SdEcOnDemand = {};		// public  global identifiers
var $_EcOnDemand = {};		// private global identifiers

SdCommon.require(SdCommon      );	// this 'sd-ec-tree' file needs to be reveiwed and/or rewritten
SdCommon.require(SdEcAjaxLoader);

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

SdEcOnDemand.ajaxResponseHandlerForToggledUrl = function (xmlHttp, args)
{
	var  xml = xmlHttp.responseXML;
	if (!xml) return;

	var articles = xml.getElementsByTagName("article");

	var articleNode = articles[0];

	var kids = articleNode.childNodes;

	var tuple = {};

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

		var txt =  kid.firstChild;
		if (!txt) continue;

		var txt =  kid.firstChild;
		if (!txt) continue;

		var name = kid.nodeName ;
		var text = txt.nodeValue;

		name = SdCommon.trim(name);	
		text = SdCommon.trim(text);	

		tuple[name] = text;
	}

	var title  = tuple[   "title"];
	var author = tuple[  "author"];
	var text   = tuple["fullText"];
	text = $_EcOnDemand.removeHrTags(text, "<br>");

	var novelist_unique_id = args["uid"];
	var elementId = args["element-id"];
	var divNode = document.getElementById(elementId);
//	Element.insert(divNode,text);
	divNode.innerHTML= text;

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

	// <xsl:attribute name="href">javascript:SdEcTree.toggle(<xsl:value-of select="ui"/>);</xsl:attribute>

	var url = "http://search.ebscohost.com/login.aspx?direct=true&db=neh&tg=UI&an=" + novelist_unique_id + "&site=novp-live";
	    url = "javascript:$_EcOnDemand.openLink('" + url + "')";
//	    url = "javascript:window.open('" + url + "'" +  ", '')";		// works -- but loses old window!

	var lnkNode = document.createElement("a");

	lnkNode.setAttribute("href",url);
	var txtNode = document.createTextNode("see original article");
	lnkNode.appendChild(txtNode);

	divNode.appendChild(lnkNode);
}

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

$_EcOnDemand.openLink = function (url)
{
	window.open(url, "");
}

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

$_EcOnDemand.removeHrTags = function (text, replacement)
{
	var indexOfLeftBrace  = text.indexOf("<hr");
	if (indexOfLeftBrace < 0) return text;

	var indexOfRightBrace = text.indexOf(">", indexOfLeftBrace);
	if (indexOfRightBrace < 0) return text;

	var prefix = text.substring(0, indexOfLeftBrace                       );
//	var middle = text.substring(   indexOfLeftBrace+3, indexOfRightBrace  );
	var suffix = text.substring(                       indexOfRightBrace+1);

	text = prefix + (replacement ? replacement : "") + suffix;

	var result = $_EcOnDemand.removeHrTags(text);
	return result;
}

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

function __doLinkPostBack(first, second, third)
{
	// http://nr-beta-eit.ebscohost.com//Services/NovelistSelect.asmx/GetFullTextContent?prof=demo.eit.sirsi&pwd=ebs2898&authType=&ipprof=&ui=427137&numrecs=2

	// http://search.ebscohost.com/login.aspx?direct=true&db=neh&tg=UI&an=097834&site=novp-live

	var msg = "__doLinkPostBack('" + first  + "', '" + second + "', '" + third + "')";
	SdCommon.log(msg);

	var indexOfPlus = second.indexOf("+");
	var indexOfBar  = second.indexOf("|",indexOfPlus+1);
	var number = second.slice(indexOfPlus+1, indexOfBar);

	var url = "http://search.ebscohost.com/login.aspx?direct=true&db=neh&tg=UI&an=" + number + "&site=novp-live";
	SdCommon.lnk("user selected: ", url);
//	alert("see log -- " + "user selected: " + url);

//	window.location = url;

	window.open(url, ""); 
}

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

