
var SdEcTabs = {};		// public  global identifiers
var $_EcTabs = {};		// private global identifiers

$_EcTabs.pairs = {};		// used in hide/show tab functions

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

SdEcTabs.deTab = function (tabsContainerId, tabId)
{
//	return;

	var list = document.getElementById(tabsContainerId);	if (!list) return;
	var tab  = document.getElementById(tabId);		if (! tab) return;
	var next = tab.nextSibling;			//		if (!next) return;
	var pair = { "tab" : tab, "next" : next};
	$_EcTabs.pairs[tabId] = pair;
	list.removeChild(tab);
}

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

SdEcTabs.enTab = function (tabsContainerId, tabId)
{
//	return;

	var list = document.getElementById(tabsContainerId);	if (!list) return;
	var tab  = document.getElementById(tabId);
//	if (tab) return;

	var pair = $_EcTabs.pairs[tabId];
	if (!pair) return;

	tab = pair["tab"];
	if (!tab) return;

	var next = pair["next"];

	if (next)
	{
		try
		{
			list.insertBefore(tab,next);
		}
		catch (e)
		{
			list.appendChild(tab);
		}
	}
	else
	{
		list.appendChild(tab);
	}
}

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

$_EcTabs.anElementExistsWithTheAttribute = function (tag, attribute, value)
{
	var elements = document.getElementsByTagName(tag);
	elements = SdCommon.filterArrayOfNodesByAttribute(elements, attribute, "==", value);
	return (elements.length > 0);		
}

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

$_EcTabs.wasALookInsidePopulated = function ()
{
	var region = document.getElementById("enriched-content");
	var kids   = region.childNodes;
	var count  = kids.length;

//	SdCommon.log("enriched-content has " + count + " kids");

	var found = false;
	for (var k = 0; k < count; k++)
	{
		var span = kids[k];
		var grandKids = span.childNodes;
		var number    = grandKids.length;
//		SdCommon.log("kid[" + k + "] is '" + span + "' with " + number + " grand kids");
		if (number > 0) { found = true; break } 
	}

	return found;
}

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

$_EcTabs.AllLoadedEventHandler = function ()
{
	var verbose = true;

	if ( $_EcTabs.wasALookInsidePopulated() )
	{
//		if (verbose) SdCommon.log("keep tab: 'a look inside'");
	}
	else
	{
		SdEcTabs.deTab("detail-tabs-container", "detail-tab-2");
//		if (verbose) SdCommon.log("drop tab: 'a look inside'");
	}

	if ( $_EcTabs.anElementExistsWithTheAttribute("div", "klass", "sd-tree-node") )
	{
//		if (verbose) SdCommon.log("keep tab: 'related articles'");
	}
	else
	{
		SdEcTabs.deTab("detail-tabs-container", "detail-tab-4");
//		if (verbose) SdCommon.log("drop tab: 'related articles'");
	}

}

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

SdEcProgress.registerAllLoadedEventHandler($_EcTabs.AllLoadedEventHandler);


