
var SdEcChili4 = {};		// public  global identifiers
var $_EcChili4 = {};		// private global identifiers

$_EcChili4.hitsPerPage =  null;	// used to guarentee all divs are created
$_EcChili4.pastHitsEnd = false;	// used to guarentee <ul> matched with </ul>

$_EcChili4.ratingCount = 0;
$_EcChili4.reviewCount = 0;

$_EcChili4.loadHook =  null;
$_EcChili4.pageDone = false;

$_EcChili4.isbnToRating = {};
$_EcChili4.isbnToReview = {};

$_EcChili4.langToCode = null;

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

$_EcChili4.saveInputs = function (elementId)
{
	var  inputs = $_EcChili4["inputs"];
	if (!inputs)
	{
		var type    = SdEcSpecifics.getSpecific(elementId, "cf-type");
		var account = SdEcSpecifics.getSpecific(elementId, "account");
		var profile = SdEcSpecifics.getSpecific(elementId, "profile");
		var script  = SdEcSpecifics.getSpecific(elementId,  "script");
		var defer   = SdEcSpecifics.getSpecific(elementId,   "defer");

		var lang    = SdEcValues.getParameter("language-name");

		$_EcChili4["inputs"] = [type, account, profile, script, defer, lang];
	}
}

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

$_EcChili4.createChiliReviewDiv = function (elementId)
{
	$_EcChili4.saveInputs(elementId);

	var ord  = SdEcSpecifics.getSpecific(elementId,     "ord");
	var isbn = SdEcSpecifics.getSpecific(elementId, "cf-isbn");

	var index = (isbn ? isbn.indexOf(",") : 0);
	if (index > 0) isbn = isbn.substring(0,index);

	var status = $_EcChili4.isbnToReview[isbn];

	var hitsPerPage = $_EcChili4.hitsPerPage;

	if (isbn && status == 'todo')
	{
		var divNodeForReviewFrame = document.createElement("div");

		divNodeForReviewFrame.align = "center";
		divNodeForReviewFrame.setAttribute("class","chili_iframe");		// divNodeForReviewFrame.class = "chili_iframe";
		divNodeForReviewFrame.id    = "chili_review_" + isbn;	

		var style = divNodeForReviewFrame.style;
		style.display = "none";	

		var regionNode = document.getElementById(elementId);

		if (hitsPerPage > 1)
		{
			var txt = document.createTextNode("chili-iframe");
			divNodeForReviewFrame.appendChild(txt);

			regionNode.appendChild(divNodeForReviewFrame);
		}
		else
		{
			var div = document.createElement("div");
			var hi  = document.createTextNode("\u00a0");
			var lo  = document.createTextNode("\u00a0");

			div.appendChild(hi);
			div.appendChild(divNodeForReviewFrame);
			div.appendChild(lo);

			regionNode.appendChild(div);
		}

		$_EcChili4.isbnToReview[isbn] = 'done';
	}

	$_EcChili4.reviewCount += 1;
	$_EcChili4.onLoadHandlerChiliFresh();
}

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

$_EcChili4.createChiliRatingDiv = function (elementId)
{
	$_EcChili4.saveInputs(elementId);

	var ord  = SdEcSpecifics.getSpecific(elementId,     "ord");
	var isbn = SdEcSpecifics.getSpecific(elementId, "cf-isbn");

	var index = (isbn ? isbn.indexOf(",") : 0);
	if (index > 0) isbn = isbn.substring(0,index);

	var status = $_EcChili4.isbnToRating[isbn];

	var hitsPerPage = $_EcChili4.hitsPerPage;

	if (isbn && status == 'todo')
	{
		var divNodeForPsuedoLink  = document.createElement("div");
		divNodeForPsuedoLink.id   = "isbn_"+isbn;

		var regionNode = document.getElementById(elementId);

		if (hitsPerPage > 1)
		{
			// regionNode.appendChild(divNodeForPsuedoLink);
			$_EcChili4.insert(regionNode, divNodeForPsuedoLink);  // kludge for internet explorer bug
		}
		else
		{
/*
			var div = document.createElement("div");
			var hi  = document.createTextNode("\u00a0");
			var lo  = document.createTextNode("\u00a0");

			div.appendChild(hi);
			div.appendChild(divNodeForPsuedoLink);
			div.appendChild(lo);

			regionNode.appendChild(div);
*/
			regionNode.appendChild(divNodeForPsuedoLink);
		}

		$_EcChili4.isbnToRating[isbn] = 'done';
	}

	$_EcChili4.ratingCount += 1;
	$_EcChili4.onLoadHandlerChiliFresh();
}

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

$_EcChili4.eLibraryLanguageToChiliFreshCode = function (language)
{
	var  map = $_EcChili4.langToCode;
	if (!map)
	{
		map = {};
		var codes = SdEcValues.getParameter("chilifresh-languages");
		if (codes)
		{
			// ENGLISH:en_US,SPANISH:es_ES,FRENCH:fr_FR

			var pairs = codes.split(',');
			var count = pairs.length;
			for (var k = 0; k < count; k++)
			{
				var pair = pairs[k];
				var both = pair.split(':');
				if (both.length != 2) continue;
				var lang = both[0];
				var code = both[1];
				map[lang] = code;
			}
		}
		$_EcChili4.langToCode = map;
	}

	var  code = map[language];
	if (!code) code = language;
	return code;
}

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

$_EcChili4.onLoadHandlerChiliFresh = function ()
{
	var hitsPerPage = $_EcChili4.hitsPerPage;
	if ($_EcChili4.ratingCount < hitsPerPage || $_EcChili4.reviewCount < hitsPerPage || !$_EcChili4.pastHitsEnd) return;

	var elements = document.getElementsByTagName("body");
	var bodyNode = ( (elements && elements.length) ? elements[0] : null );

	var inputs  = $_EcChili4["inputs"];
	var type    = inputs[0];
	var account = inputs[1];
	var profile = inputs[2];
	var script  = inputs[3];
	var defer   = inputs[4];
	var lang    = inputs[5];

	$_EcChili4.prepareInputElement(bodyNode, "chilifresh_type"   ,"chilifresh_type"   ,    type);
	$_EcChili4.prepareInputElement(bodyNode, "chilifresh_account","chilifresh_account", account);
	$_EcChili4.prepareInputElement(bodyNode, "chilifresh_profile","chilifresh_profile", profile);

	if (lang != 'ENGLISH')
	{
		var code = $_EcChili4.eLibraryLanguageToChiliFreshCode(lang);
		$_EcChili4.prepareInputElement(bodyNode, "chilifresh_language","chilifresh_language", code);
	}

	var browser = SdEcVersion.getBrowserName   ();
	var version = SdEcVersion.getBrowserVersion();

	if ( !defer        ) defer =   '0';						// default empty value to 'E' -- all non-empty values are strings
	if (  defer == '0' ) defer = false;						// convert the zero character to false
	if (  defer == 'E' ) defer = (browser == 'Explorer');				// special case: defers only when running in internet-explorer
	if (  defer == '+' ) defer = (browser == 'Explorer') && (version >= 7);	// special case: defers only when running in internet-explorer 7+
	if (  defer == '7' ) defer = (browser == 'Explorer') && (version == 7);	// special case: defers only when running in internet-explorer 7

	if ($_EcChili4.pageDone)
	{
		$_EcChili4.loadChiliData();					// load data now			-- SdCommon.log("loading chili data pg-done");
	}
	else if (defer)
	{
		$_EcChili4.loadHook = $_EcChili4.loadChiliData;		// load data after page is loaded	-- SdCommon.log("loading chili data on-load");
	}
	else
	{
		$_EcChili4.loadChiliData();					// load data now			-- SdCommon.log("loading chili data in-line");
	}
}

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

$_EcChili4.prepareInputElement = function (bodyNode, id, name, value)
{
	var inputNode = document.createElement("input");

	inputNode.setAttribute("id"   ,       id);
	inputNode.setAttribute("name" ,     name);
	inputNode.setAttribute("type" , "hidden");
	inputNode.setAttribute("value",    value);

	$_EcChili4.insert(bodyNode, inputNode);
}

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

$_EcChili4.insert = function (papa, node)  // kludge for internet explorer bug
{
	var first = papa.firstChild;
	if (first)
	{
		papa.insertBefore(node, first);
	}
	else
	{
		papa.appendChild(node);
	}
}

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

$_EcChili4.loadChiliData = function ()
{
	var inputs = $_EcChili4["inputs"];
	var script = inputs[3];

	SdCommon.load_script(script, "chili-script");
}

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

$_EcChili4.onLoadHandlerToLoadChiliData = function ()
{
	$_EcChili4.pageDone = true;

	var loadHook = $_EcChili4.loadHook;
	if (loadHook)
	{
		loadHook();
	}
}

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

$_EcChili4.topOfHitListObserver = function (args)
{
	var list  = args['hit-list-isbn-list'];
	var count = (list ? list.length : 0);

	for (var k = 0; k < count; k++)
	{
		var isbn  = list[k];
		var index = (isbn ? isbn.indexOf(",") : 0);
		if (index > 0) isbn = isbn.substring(0,index);

		$_EcChili4.isbnToRating[isbn] = 'todo';
		$_EcChili4.isbnToReview[isbn] = 'todo';
	}

	$_EcChili4.hitsPerPage = count;
}

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

$_EcChili4.endOfHitListObserver = function (args)
{
	$_EcChili4.pastHitsEnd = true;
	$_EcChili4.onLoadHandlerChiliFresh();
}

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

SdCommon.addLoadEvent($_EcChili4.onLoadHandlerToLoadChiliData);

SdEcEngine.registerTypeHandler("chilifresh-ratings", $_EcChili4.createChiliRatingDiv);
SdEcEngine.registerTypeHandler("chilifresh-reviews", $_EcChili4.createChiliReviewDiv);

SdEcEvents.registerEventObserver("top-of-hit-list" , $_EcChili4.topOfHitListObserver);
SdEcEvents.registerEventObserver("end-of-hit-list" , $_EcChili4.endOfHitListObserver);

