window.onload = initPage;

// This function displays the Google Books cover and link based on ISBN
function PGBSisbn(booksInfo) {
	var NOT_AVAILABLE = "Not Available";
	var NOVIEW = "More Information At Google Books";
	var PARTIAL = "Preview Selected Pages At Google Books";
	var FULL = "Full View Available At Google Books";
	var NOLINK = "";

	for(isbn in booksInfo) {
		var bookInfo = booksInfo[isbn];
		var element = document.getElementById(isbn);
		var text = NOT_AVAILABLE;
		var img = "";

		if (bookInfo) {
			// get the url

			if ( bookInfo.preview_url ) {
				element.href  = bookInfo.preview_url;
			}
			else if ( bookInfo.info_url ) {
				element.href = bookInfo.info_url;
			}

			// get the type of info
			if (bookInfo.preview == "full") {
				text = FULL;
			}
			else if (bookInfo.preview == "partial") {
				text = PARTIAL;
			}
			else if (bookInfo.preview == "noview") {
				text = NOVIEW;
			}

			// get the thumbnail
			if ( bookInfo.thumbnail_url ) {
				img = '<img border="0" alt="Google Books Thumnail Image" src="' + bookInfo.thumbnail_url + '">';
			}
		}
		element.innerHTML = img + '<br />' + text;
	}
}


// This function creates the request to fetch the XML
function createRequest() {
	try {
		request = new XMLHttpRequest();
	} 
	catch (tryMS) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (otherMS) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) {
				request = null;
			}
		}
	}
	return request;
}


// This function hides the Digitized Versions stanza if there's no View Available Text Online link
function hideStanza() {

	var tr = document.getElementsByTagName('TR');		// we have to iterate through every TR b/c we can't get to the Intenet Links otherwise
	
	for(i = 0; i < tr.length; i++) {					// for every TR in the document

		var x=tr[i].getElementsByTagName('TH');			// get the TH

		if (x.length == 1 && x[0].innerHTML == 'Digitized Versions:') {	// if the row has 2 columns and the first one has the text of Digitized Versions:

	 		x[0].innerHTML = '';
			
		}

	}
		
}


// This function displays the link based on what the XML request turns up
function displayDetails() {
	
	var showLink = true;

	if (request.readyState == 4) {

		//alert("4");
		
		if (request.status == 200) {
			
		//alert("200");
		
			var responseDocText = request.responseText;
			var responseDoc = request.responseXML;
			
			var itemInformation = responseDoc.getElementsByTagName("itemInformation");
			var googleStatusInformation = responseDoc.getElementsByTagName("googleStatus");
			var hathiAccessInformation = responseDoc.getElementsByTagName("hathiAccess");
			var suppressedInformation = responseDoc.getElementsByTagName("suppressed");

			//alert(responseDocText);
			//alert(responseDoc);
			//alert(itemInformation);
			//alert(googleStatusInformation);
			//alert(hathiAccessInformation);
			//alert(suppressedInformation);
			
			if (itemInformation.length == 0) {
				showLink = false;
			}

			else if (googleStatusInformation.length == 0 && hathiAccessInformation.length > 0) {
				showLink = false;
				for(i = 0; i < hathiAccessInformation.length; i++) {
					var label=hathiAccessInformation[i].childNodes[0].nodeValue;
					//alert(label);
					if (label != 'Search Only') {
						showLink = true;
						//alert("Show This Value " + showLink);
					}
				}
			}
			
			else if (googleStatusInformation.length > 0 && hathiAccessInformation.length == 0) {
				showLink = false;
				for(j = 0; j < googleStatusInformation.length; j++) {
					var label=googleStatusInformation[j].childNodes[0].nodeValue;
					if (label != 'Not Live') {
						showLink = true;
					}
				}
			}
			
			else if (suppressedInformation.length > 0) {
				for(k = 0; k < suppressedInformation.length; k++) {
					var label=suppressedInformation[k].childNodes[0].nodeValue;
					var parent=suppressedInformation[k].parentNode.nodeName;
					if (label == 'yes' && parent == 'bibliographicInformation') {
						showLink = false;
					}
				}
			}
			
		}
		
		else {
			showLink = false;
		}

		// now either show the link or hide it.  Don't show it if it's bibid 6491100
		//if (showLink && (bibID != '6491100')) {
		if (showLink) {
			document.getElementById("bibid").innerHTML = '<a href="http://madcat.library.wisc.edu/bibliograph/wu/' + bibID + '?xslt=http://madcat.library.wisc.edu/xslt/gbse.xslt">View available text online</a>';
		}

		else {
			document.getElementById("bibid").innerHTML = '';
			hideStanza();
		}

	}
	
	else {
	}

}
		

// This function gets the bibid from the record display
function getBibIdInfo(bibID) {

  request = createRequest();

  if (request == null) {
    alert("Unable to create request");
    return;
  }

  var url= "http://madcat.library.wisc.edu/bibliograph/wu/" + escape(bibID);

  //alert(url);
  
  request.open("GET", url, true);
  request.onreadystatechange = displayDetails;
  request.send(null);
}

// This function gets everything started by starting on page load
function initPage() {
	//find the hidden bibid on the page
	if (document.getElementById("bibid")) {
		bibID = document.getElementById("bibid").innerHTML;
		//alert(bibID);
		getBibIdInfo(bibID);
	}
}

