/* topsbooks.js: Javascript routines to enliven TOPScience book pages */

var button_index = -1;	// count of buy line buttons found on this page

/* Compute current file name from url of this page */
function bare_file_name (url) {
	var fnstart = url.lastIndexOf("/") + 1;
	var fnfinis = url.lastIndexOf (".");
	return (url.substring (fnstart, fnfinis));
};


/* Display book cover at left of main content */
function show_book_cover (my_file_name) {
      var thumb_url = '../book_covers/' + my_file_name + 'Thumb.jpg';
      var full_url = '../book_covers/' + my_file_name + 'Full.jpg';
	  var html_text = '';
      html_text += ('<p>Book Cover: <a href="#" onclick="javascript:enlarge_book_cover(\'' + full_url + '\')">Enlarge</a></p>');
      html_text += ('<img src="' + thumb_url + '" alt="sample book cover" width="100" style="border:thin solid #000" />');
      html_text += ('<p>Sample Activity: <a href="#Activity" onClick="switch_tab_panel(0)">View</a></p>');
	  return (html_text);
};


/* Display enlarged book cover in new window */
function enlarge_book_cover(url) {
	window.open (url, 'Enlarged Book Cover', 'width=500,height=660,resizeable=yes,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no');
};

/* Make HTML to show lesson format illustration and enlarged image in new window */
function show_lesson (my_file_name, alt_text, width, height) {
		/* compose thumbnail and full size image URLs from standard naming patterns */
		var thumb_url = '../images/lesson_formats/' + my_file_name + '_Thumb.jpg';
		var full_url = '../images/lesson_formats/' + my_file_name + '_Full.jpg';
		/* compose and return a link to the javascript function below which will open a window and show the full image on demand */
		var html_text = '<img src="' + thumb_url + '" alt="' + alt_text + '" width="' + width + '" height="' + height + '" onClick="javascript:enlarge_lesson (\'' + full_url + '\' , \'' + alt_text + '\', '+ width + ')" />';
		return (html_text);
};

/* Display enlarged lesson format in a new window */
function enlarge_lesson (url, title, width) {
	
	if (width < 800) {
		var lesson_window = window.open (url, 'Enlarged_Lesson', 'width=800,height=1000,resizeable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
	} else {
		var lesson_window = window.open (url, 'Enlarged_Lesson', 'width=1200,height=1000,resizeable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
	};
	lesson_window.focus();
	
};


/* Display a buy line form for each product */
function buy_line (product_code, product_label) {
	button_index += 1;
	var myForm = document.addItem
	var product_line = '';
	product_line += '<div class="addtocart">'; 
	product_line += '<input type="text" name="units" id="units" size="2" maxlength="2" value="">';
	product_line += '<input type="button" value="add to cart" onclick="javascript:house_keep(' + myForm + ', ' + button_index + '); myForm.submit(); return(true);">';
	product_line += '<input type="hidden" name="Product" value="' + product_code + '">\n';
	product_line += '</div>\n';
	product_line += '<label for="product">' + product_label + '</label>\n';	
	return (product_line);
}

/* Ensure user has entered a count of units */
function house_keep (button_index) {
	/* scan form elements to match units field index with button index */
	var units_index = -1;
	var myForm = document.addItem;
	for (i=0; i<myForm.elements.length; i++) {
		if (myForm.elements[i].name == 'units') {
			units_index += 1;	
			if (units_index == button_index && myForm.elements[i].value == '') myForm.elements[i].value = "1";
		}
	}	
	return (true);
}

/* Load the shopping cart page, with a return link to this page */
function load_shopping () {
	var cart_url = 'https://secure.webvalence.com/ecommerce/testversion/basketCode.lasso';
	var query_string = '?request=show&merchant=topscience&kiosk=newbooks&goback=' + location.href;
	window.location.href = cart_url + query_string;
	return;
}

/* Display an appropriate Tab Panel based on URL */
function show_tab_panel() {
	  var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
	  // get the current URL
	  var url = window.location.toString();
	  //get search parameters
	   url.match(/\?(.+)$/);
	   var params = RegExp.$1;
	   if (params != '') {
		  var pair = params.split('=');
		  var name = pair[0];
		  if (name == 'page') {
			var pageno = parseInt (pair[1]);
		    TabbedPanels1.showPanel(pageno);
		  }
	  }
  }

/* Display requested Tab Panel based on given pageno */
function switch_tab_panel(pageno) {
	var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
	TabbedPanels1.showPanel(pageno);
  }



