// gbs_wb.js -- created by Bob Duncan, Lafayette College.
// Uses WebBridge to get a record's standard numbers to Google Book Search.
// Data returned by GBS is used to show a link back to GBS if/when 
// GBS has a record for the book, and a cover image (if desired).
// Version: 03-16-2010

function compose_GBS() {
	gbsContainer = document.getElementById("gbs");
	var gbsnumbers = [];
	// Make sure the WB form is on the page: 
	if (!document.gbs117_form) {}
		else {
		// It's there, now see if there's a select element in the form:
		var formtest = document.gbs117_form.ISBNgbs_OCLCgbs_LCCNgbs;
		if (formtest) {
		// If so, put each option value into the array:
			var theoptions = document.gbs117_form.ISBNgbs_OCLCgbs_LCCNgbs.options;
			var howmany = theoptions.length;
			for (var i = 0; i < howmany; i++) {
				gbsnumbers[i] = theoptions[i].innerHTML.replace(/\s/,"");
			}
		}
		// If there's no select element in the form, grab the single value:
		else {
			for (var i = 0; i < 1; i++) {
				gbsnumbers[i] = gbsContainer.innerHTML;
			}
		}
		// Create the URL we send to GBS:
		var api_url = "http://books.google.com/books?jscmd=viewapi&callback=insert_GBS&bibkeys=" + gbsnumbers.join(",");
	}
	// Construct an inline javascript for GBS:
	var scriptLine = "%3Cscript src='" + api_url + "' type='text/javascript'%3E%3C/script%3E";
	// Insert the line of script into the page so it returns the GSBookInfo object
	document.write(unescape(scriptLine));
}

// Take the info returned from GBS and do stuff with it:

function insert_GBS(GBSBookInfo) {

	for (bib_key in GBSBookInfo) {
	// Get GBS data from the JSON object returned
		var bookInfo = GBSBookInfo[bib_key];
		if (!bookInfo){}
		else {
			var gbsimg ='<img src="/screens/gbs_preview_button1.gif" alt="" style="margin-right:5px;" />';
		// Set text and link values depending on viewablity:
			if (bookInfo.preview == "full") { 
				var urlGBS = bookInfo.preview_url;
				var linkGBS = gbsimg;
				var textGBS =""; 
			}
			if (bookInfo.preview == "partial") { 
				var urlGBS = bookInfo.preview_url;
				var linkGBS = gbsimg;
				var textGBS = ""; 
			}
				if (bookInfo.preview == "noview") { 
				var urlGBS = bookInfo.info_url;
				var linkGBS = ""; 
				var textGBS = "";
			}
		// Start cover image block.
		// Get rid of block if not using cover image.
			// Get the thumbnail image and make it bigger.
			if (bookInfo.thumbnail_url) {
				var bigthumb = bookInfo.thumbnail_url.replace(/&edge=curl/g,"").replace(/zoom=5/,"zoom=1");
				var imgcode = '<img src="' + bigthumb + '"' + 'alt="Cover image" style="border: 1px grey solid"><br /><br />';
			}
			else {
				var imgcode ="";
			}
		// End cover image block.
	
		// Rewrite the HTML inside the "gbs" <div> element.
		// Get rid of "imgcode + " if not using cover image.
			gbsContainer.innerHTML = imgcode + '<a href="' + urlGBS + '" target="_blank">' + linkGBS  + '</a>' + textGBS;

		// Turn on the display of the GBS link and image
			gbsContainer.style.display = 'block';
		}
	}
}

