// Baynote Observer for www.abebooks.co.uk
// Last updated: Sept 14th 2008
function bn_isNotEmpty(checkVar) {
	return (typeof(checkVar) != "undefined") && (checkVar != null) && (checkVar != "");
}
function baynote_setAttrs() {
	baynote_tag.docAttrs.isFirst = "False";	
	var span = document.getElementsByTagName("SPAN");
	if (!span) return;
	
	for (var i = 0; i < span.length; i++) {
		if (!span[i])return;
		if (span[i].id == "biblio-signed") {
				var tempSpanContent = span[i].textContent;
				if(bn_isNotEmpty(tempSpanContent)) {
					baynote_tag.docAttrs.Signed = tempSpanContent;
				}
			} else if (span[i].id == "biblio-edition") {
				var tempSpanContent = span[i].textContent;
				if(bn_isNotEmpty(tempSpanContent)) {
					baynote_tag.docAttrs.Edition = tempSpanContent;
					if(bn_isNotEmpty(baynote_tag.docAttrs.Edition) && baynote_tag.docAttrs.Edition.match("1st|First|first"))
                       baynote_tag.docAttrs.isFirst = "True";
					  }
			} else if (span[i].id == "biblio-publisher") {
				var tempSpanContent = span[i].textContent;
				if(bn_isNotEmpty(tempSpanContent)) {
					baynote_tag.docAttrs.Publisher = tempSpanContent;
				}
			}
			
	}
  }
function bn_unescapeHtml(escapedStr) {
	return escapedStr.replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}
function bn_getUrlParam(name, url) {
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS,"i");
	var results = regex.exec(url);
	if(results == null)
		return "";
	else
		return unescape(results[1]);
}
function bn_getPageAttrs() {
	baynote_tag.docAttrs.hasThumbnail = "False";
	if (typeof(bnPageType) != "undefined" && bn_isNotEmpty(bnPageType)) {
		if(bnPageType.toLowerCase() == "iframe") {
			baynote_tag.iFrame = true;
			baynote_tag.docAttrs.PageType = "category";
		} else {
			baynote_tag.docAttrs.PageType = bnPageType;
		}
	}
	if (typeof(bnTitle) != "undefined" && bn_isNotEmpty(bnTitle)) {
		bnTitle = bn_unescapeHtml(bnTitle);
		baynote_tag.docAttrs.Title = bnTitle;
		baynote_tag.title = bnTitle;
	}
	if (typeof(bnCat) != "undefined" && bn_isNotEmpty(bnCat)) { baynote_tag.docAttrs.Cat = bnCat; }
	if (typeof(bnIsbn) != "undefined" && bn_isNotEmpty(bnIsbn)) {
		baynote_tag.docAttrs.Isbn = bnIsbn;
		baynote_tag.docAttrs.PageType = "product";
		var bn_locationHost = window.location.hostname;
		if (bn_locationHost.match(/[^\/]+\.abebooks\.co\.uk/)) { baynote_tag.url = "http://"+bn_locationHost+"/products/isbn/"+bnIsbn; }
		if (typeof(bnDesc) != "undefined" && bnDesc != null) { baynote_tag.docAttrs.Desc = bn_unescapeHtml(bnDesc); }
		if (typeof(bnAuthor) != "undefined" && bn_isNotEmpty(bnAuthor)) { baynote_tag.docAttrs.Author = bn_unescapeHtml(bnAuthor); }
		if (typeof(bnThumbUrl) != "undefined" && bn_isNotEmpty(bnThumbUrl)) { baynote_tag.docAttrs.ThumbUrl = bnThumbUrl; }
	}
	if (typeof(bnQuery) != "undefined" && bn_isNotEmpty(bnQuery)) {
		baynote_tag.query = bnQuery;
	}
	var bn_locationHref = window.location.href;
	if (bn_isNotEmpty(bn_locationHref) && bn_locationHref.match(/\/servlet\/shopbasketpl\?/i)) {
		baynote_tag.attrs.ignore = true;
	}
}
function bn_getPurchaseAttrs() {
	if (typeof(bnOrderTotal) != "undefined" && bn_isNotEmpty(bnOrderTotal)) baynote_tag.attrs.totalPurchases = parseFloat(bnOrderTotal);
	if (typeof(bnOrderId) != "undefined" && bn_isNotEmpty(bnOrderId)) baynote_tag.attrs.purchaseId = bnOrderId;
	if (typeof(bnPurchaseDetails) != "undefined" && bn_isNotEmpty(bnPurchaseDetails)) baynote_tag.attrs.purchaseDetails = bnPurchaseDetails;
}
function bn_checkShowObserver(pageType) {
	var showObs = true;
	// optional: disable observer for specific pages
	return showObs;
}
function bn_checkPurchasePage() {
	var capturePur = false;
	var pageUrl = window.location.href;
	if (pageUrl.match(/\/servlet\/thankyou/i)) {
		capturePur = true;
	}
	return capturePur;
}
var bn_onClickHandler = function(clickedElement, exitInfo) {
	var clickIsbn = "";
	var clickUrl = "";
	var exitResult = false;
	if(typeof(bnObserver) != 'undefined' && typeof(bnObserver.defaultExitConfirmation) != 'undefined') {
		exitResult = bnObserver.defaultExitConfirmation(clickedElement,exitInfo);
	}
	if(clickedElement) {
		if (bn_isNotEmpty(clickedElement.tagName) && clickedElement.tagName == "IMG") {
			if(clickedElement.parentNode && clickedElement.parentNode.tagName == "A") {
				var linkHref = clickedElement.parentNode.href;
				if(bn_isNotEmpty(linkHref) && linkHref.match(/\/servlet\/shopbasketpl\?.*isbn13/i)) {
					clickIsbn = bn_getUrlParam("isbn13", linkHref);
				}
			}
		}
		if (bn_isNotEmpty(clickIsbn)) {
			clickUrl = "http://www.abebooks.co.uk/products/isbn/"+clickIsbn;
			if(exitInfo != null && typeof(exitInfo.attrs) == "undefined") {
				exitInfo.attrs = new Object();
			}
			exitInfo.attrs.action = "AddToCart";
			exitInfo.attrs.target = clickUrl;
			exitResult = true;
		}
	}
	return exitResult;
}
var bn_locationHref = window.location.href;
if (bn_locationHref.indexOf("https://") == 0) { baynote_tag.server = "https://abebooks-wwwuk.baynote.net"; }
else { baynote_tag.server = "http://abebooks-wwwuk.baynote.net"; }
baynote_tag.customerId = "abebooks";
baynote_tag.code = "wwwuk";
baynote_tag.type = "baynoteObserver";
if (bn_checkShowObserver()) {
	bn_getPageAttrs();
	if(bn_checkPurchasePage()) { bn_getPurchaseAttrs(); }
	baynote_tag.exitConfirmation = bn_onClickHandler;
	baynote_setAttrs();
	baynote_tag.show();
} else {
	baynote_tag.noshow();
}