var isbn;
var viewer;

$(document).ready(function() {
	
	/* Add listener to the click method of preview button.
	 */	
	$('#preview_button').click(function() {
		$('#page').hide();
		$('#viewerCanvas').show();
		loadPreview();
		//if ($('#googleViewer').toggle().is(':visible')) {loadPreview()}
	});
	
	$('#viewerToolbar').click(function() {
		$('#viewerCanvas').hide();
		$(this).hide();
		$('#page').show();
	})
	
});

/* Called when the page load.
 * Load the google.books API.
 * show thw button.
 */
function processDynamicLinksResponse(booksInfo) {
	for (id in booksInfo) {
		isbn = id;
		if (isbn != 'ISBN:9781416034278') return;
		if (booksInfo[id] && (booksInfo[id].preview == 'partial' || booksInfo[id].preview == 'full')) {
			google.load("books", "0", {"language": "es", "showLinkChrome": false});
			$('#preview_button').show();
		}
	}
}

/* Called when the preview button is clicked.
 * Instantiate the viewer.
 * Load the desired book.
 */
function loadPreview() {
	viewer = new google.books.DefaultViewer($('#viewerCanvas')[0]);
	viewer.load(isbn, null, loadSucess);
}

/* To syncro the toolbar and the viewer show.
 */	
function loadSucess() {
	$('#viewerToolbar').show();
}

