// Baynote recommendation for www.abebooks.co.uk
// Last updated: Jul 17, 2008
var bn_currentGuideIdx = -1;
var bn_currentGuideResource;
var bn_currentGuideContainerId;
function bn_showRec(pageType,listSize) {
	baynote_tag.server = "http://abebooks-wwwuk.baynote.net";
	baynote_tag.customerId = "abebooks";
	baynote_tag.code = "wwwuk";
	baynote_tag.type = "guide";
	if (bn_isNotEmpty(baynote_tag.url)) {
	} else if (typeof(bnUrl) != "undefined" && bn_isNotEmpty(bnUrl)) {
		baynote_tag.url = bnUrl;
	} else {
		var updatedUrl = bn_getUpdatedUrl();
		if (bn_isNotEmpty(updatedUrl)) { baynote_tag.url = updatedUrl; }
	}
	if (typeof(bnIsbn) != "undefined" && bn_isNotEmpty(bnIsbn)) {
		baynote_tag.url = "http://www.abebooks.co.uk/products/isbn/"+bnIsbn;
	}
	if (typeof(bnQuery) != "undefined" && bn_isNotEmpty(bnQuery)) {
		baynote_tag.query = bnQuery;
	}
	if (pageType == "home") {
		baynote_tag.guide = "HomeGuide";
		baynote_tag.listSize = 10;
	} else if (pageType == "category") {
		baynote_tag.guide = "CategoryGuide";
		baynote_tag.listSize = 5;
	} else if (pageType == "product") {
		baynote_tag.guide = "ProductGuide";
		baynote_tag.listSize = 5;
	} else if (pageType == "search") {
		baynote_tag.guide = "SearchGuide";
		baynote_tag.listSize = 5;
	} else if (pageType == "basket") {
		baynote_tag.guide = "BasketGuide";
		baynote_tag.listSize = 5;
	} else if (pageType == "thankyou") {
		baynote_tag.guide = "ThankyouGuide";
		baynote_tag.listSize = 5;
	} else if (pageType == "term") {
		baynote_tag.format = "results-terms";
		baynote_tag.guide = "TermGuide";
		baynote_tag.listSize = 15;
	}
	if (listSize != null && listSize > 0) { baynote_tag.listSize = listSize; }
	if (bn_isNotEmpty(baynote_tag.guide)) {
		baynote_tag.show();
		bn_waitForGuide();
	} else {
		baynote_tag.noshow();
	}
}
function bn_waitForGuide() {
	if (bn_isNotEmpty(bnResourceManager)) {
		bn_currentGuideIdx++;
		bn_currentGuideResource = "GLResults" + bn_currentGuideIdx;
		bn_currentGuideContainerId = "bn_guidecontainer" + bn_currentGuideIdx;
		bnResourceManager.waitForResource(bn_currentGuideResource, "bn_guideLoaded(\""+bn_currentGuideContainerId+"\")");
	}
}
function bn_guideLoaded(bn_guideId) {
	bn_updateGuideResults(bn_guideId,1);
	bn_updateFooter(1);
}
function bn_updateGuideResults(bn_guide_id,bn_wait_count) {
	var bn_waitLimit = 20;
	if (bn_wait_count > bn_waitLimit) {
		return;
	} else {
		var guideContainerEle = document.getElementById(bn_guide_id);
		if (guideContainerEle) {
			var guideResultsArr;
			guideResultsArr = bn_getGuideResultsElements(guideContainerEle);
			bn_updateGuideResultsElements(guideResultsArr);
			// call RecBuilder
			if (typeof(BaynoteRecBuilder) != "undefined") {
				BaynoteRecBuilder.moveAndDisplayBaynoteRec();
			}
		} else if (bn_isNotEmpty(bn_guide_id) && bn_wait_count > 0) {
			bn_wait_count++;
			setTimeout("bn_updateGuideResults(\""+bn_guide_id+"\","+bn_wait_count+")", 100);
		}
	}
}
function bn_getGuideResultsElements(parentEle) {
	var result_link_class = "bn_g_result_link";
	var result_img_link_class = "bn_g_result_image_link";
	var childEleArr = parentEle.getElementsByTagName("*");
	var foundEleArr = [];
	for (var i=0; i<childEleArr.length; i++) {
		if(childEleArr[i].className == result_link_class || childEleArr[i].className == result_img_link_class) {
			foundEleArr.push(childEleArr[i]);
		}
	}
	return foundEleArr;
}
function bn_updateGuideResultsElements(resultsArr) {
	var linkSuffix = "/paratrk-16863";
	for (var i=0; i<resultsArr.length; i++) {
		var linkEle = resultsArr[i];
		var linkHref = linkEle.getAttribute("href");
		if (linkHref.indexOf(linkSuffix) > 0) {
		} else if (linkHref.match(/^http:\/\/www\.abebooks\.co\.uk\/products\/isbn\/.*/)) {
			linkEle.setAttribute("href",linkHref+linkSuffix);
		}
	}
}
function bn_updateFooter(bn_waitCount) {
	var bn_waitLimit = 10;
	var bn_footerId = "bn_g_footer_abebooks";
	if(document.getElementById(bn_footerId)) {
		(document.getElementById(bn_footerId)).style.display = "block";
	} else if(bn_waitCount < bn_waitLimit) {
		bn_waitCount++;
		setTimeout("bn_updateFooter("+bn_waitCount+")", 200);
	}
}
function bn_isNotEmpty(name) {
	return (typeof(name) != "undefined") && (name != null) && (name != "");
}
function bn_getUpdatedUrl() {
	var bn_locHref = window.location.href;
	var updatedUrl = null;
	if (bn_locHref.match(/^http:\/\/www\.abebooks\.co\.uk\//)) {
	} else if (bn_locHref.match(/^http:\/\/[^\/]+\.abebooks\.co\.uk\//)) {
		updatedUrl = bn_locHref.replace(/^http:\/\/[^\/]+\.abebooks\.co\.uk\//,"http://www.abebooks.co.uk/");
	}
	return updatedUrl;
}