/**
 *	baynote_showRecommendations(type, keywords)
 *
 *	Show the recommendations.
 *
 *	Parameter
 *	---------
 *	type - String The name of the AutoGuide to be shown.
 *	keywords - String Optional set of keywords that can be used to 
 *				as a context.
 *
 *	Parameter
 *	---------
 *	none
 *
 *	Returns
 *	-------
 *	none
 */
function baynote_showRecommendations (type) {
	baynote_tag.server = "http://barewalls-www.baynote.net";
	baynote_tag.customerId = "barewalls";
	baynote_tag.code = "www";
	baynote_tag.type = "guide";
	baynote_tag.guide = type;
	baynote_tag.listSize = 3;
	baynote_tag.show();
	bnResourceManager.waitForResource("GLResults0", "baynote_recsLoaded(1)");
}

/**
 *	baynote_recsLoaded()
 *
 *	Called when the recommendations are returned from the service.  Further
 *	it waits until the recommendations are actually put in the page.
 *
 *	Parameter
 *	---------
 *	none
 *
 *	Returns
 *	-------
 *	none
 */
function baynote_recsLoaded(waitCount) {
	var waitLimit = 10;
	if(waitCount > waitLimit) return;
	if (baynote_recsReady(0)) {
		baynote_frameResults();
	} else {
		waitCount++;
		setTimeout("baynote_recsLoaded(" + waitCount + ")", 200);
	}
}

/**
 *	baynote_recsReady(recNum)
 *
 *	Check if the recommendations are in the Dom.
 *
 *	Parameter
 *	---------
 *	recNum - Integer Zero indexed number of the recommendations set.
 *
 *	Returns
 *	-------
 *	Boolean - true if the recommendations are in, false if they are not.
 */
function baynote_recsReady(recNum) {
	// If there were no results, check to see if the secondary recs are available
	var recDiv = document.getElementById("bn_placeholder" + recNum);
	if (!recDiv) return;
	var innerDivs = recDiv.getElementsByTagName("div");
	if (!innerDivs) return;
	if (innerDivs.length == 0) return false;
	else return true;
}

/**
 *	baynote_frameResults()
 *
 *	Change the recommended product links to point to be framed.
 *
 *	Parameter
 *	---------
 *	none
 *
 *	Returns
 *	-------
 *	none
 */
function baynote_frameResults() {
	var textLinks = baynote_getElementsByClass("bn_g_result_link");
	if (!textLinks || (textLinks.length == 0)) return;
	var imageLinks = baynote_getElementsByClass("bn_g_result_image_link");
	if (!imageLinks || (imageLinks.length == 0)) return;
	for (var i = 0; i < textLinks.length; i++) {
		var href = textLinks[i].getAttribute("href");
		if (!href) continue;
		href = href + "&S=61&SP=61602,61105&ns=framer";
		textLinks[i].setAttribute("href", href);
		imageLinks[i].setAttribute("href", href);
	}
}

/**
 *	baynote_getElementsByClass(searchClass, node, tag)
 *
 *	Get an array of elements with the specified class name.  The search
 *	can be limited to a specific node or within only specific tag name.
 *
 *	Parameter
 *	---------
 *	searchClass - String The name of the class to search for.
 *	node - Node The node within which to limit the search.
 *	tag - String The name of a tag to limit the search.
 *
 *	Returns
 *	-------
 *	Array of Nodes - Nodes that have the class name specified.
 */
function baynote_getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
