//*******************************************************************************
//							L A T E S T   B O O K S
//*******************************************************************************

// Variables for cropping synopses longer than 'l' characters long
var l=400;									// number of characters beyond which to crop
var crop=true;								// true=enable, false=disable
//

t=GetCookie("dialup");
var sArr = new Array(); 				// sorted book title array
var bC=0;									// 'valid book' counter
for (i=0;i<book.length;i++) {			// go through book array
	if (book[i][ref]!="") {				// ignore entry if reference is empty
		sArr[bC]=[book[i][name],i];	// just store [book_name] and [book number]
		bC++;									// increment counter
	}
}


function doBooks() {
	var x=(document.bs.numbooks.options[document.bs.numbooks.selectedIndex].value);
	doTable(x);
}
	
function doTable(n) {
	numBks = parseInt(n,10);
	var msg="";
	msg+='<table border="0" cellpadding="5" cellspacing="1" width="100%" align="center">';
	for (x=sArr.length-1;x>sArr.length-numBks-1; x--) {
		var i=sArr[x][1];						// sorted book reference
		if (book[i][ref] != "") {
			msg+='<tr><td valign="top"><a name="'+book[i][ref]+'"><\/a>';
			msg+='<a href="' + root + book[i][image] + '">';
  	    var bI=(t=="1")?"nobook.jpg":book[i][image];
			msg+='<img alt="'+book[i][name]+'" title="'+book[i][name]+'" src="'+root+bI+'" width="125" align="left" hspace="10" vspace="5" style="border:1px solid #ffa500" /><\/a><\/td>';
			msg+='<td valign="top"><p class="sp">Title:&nbsp;<strong>'+book[i][name]+'<\/strong><\/p>';
			var au = "Author";if(booktxt[i][auth].indexOf(",")>0 || booktxt[i][auth].indexOf("&")>0) au="Authors";
			msg+='<p class="sp">'+au+':&nbsp;<strong>'+booktxt[i][auth]+'<\/strong><\/p>';

// Changes to crop synopsis
			msg+='<p class="sp">Synopsis:&nbsp;';
			var abs=booktxt[i][synopsis];
			if(crop && abs.length>l) {									// if crop enabled and length greater than 'l'
				var croptxt=abs.indexOf(" ",l);						// find closest " "
				abs=booktxt[i][synopsis].slice(0,croptxt)+" ";		// crop text to this point
				abs+='<span class="th" id="w'+i+'">'+booktxt[i][synopsis].slice(croptxt)+'&nbsp;</span>';  
																			// rest of text goes within <span>
				abs+='<a href="javascript:doShow(\'w'+i+'\');"><img id="iw'+i+'" src="images/forward.png" class="i2"></a>';
			}
			msg+=abs+'<\/p>';
		
			if(book[i][ISBN]!="")  msg+='<p class="sp">ISBN:&nbsp;'+book[i][ISBN]+'<\/p>';
			if(book[i][price]!="") msg+='<p class="sp">Price:&nbsp;'+book[i][price]+'<\/p>';
			if(book[i][cat]!="") {
				for (z=0;z<catArr.length;z++) {
					if(catArr[z][0]==book[i][cat]) {cT=catArr[z][1];break;}
				}
				msg+='<p class="sp">Category:&nbsp;'+cT+'<\/p>';
			}
			msg+='<p class="sp">Available:&nbsp;'+booktxt[i][available]+'<\/p>';
			msg+='<p class="sp">Contact:&nbsp;'+booktxt[i][contact]+'<\/p>';
  		 	msg+='<\/td><\/tr>';
			msg+='<tr valign="middle"><td colspan="3" align="center" height="20" style="border-bottom:1px dotted #888888;"><a href="javascript:history.go(-1)" target="_top"><img alt="back" border="0" height="20" src="images/back.png" width="20" /><\/a> <a href="#top"><img alt="up" border="0" height="20" src="images/up.png" width="20" /><\/a><\/td><\/tr>';
		}
	}
	msg+='<\/table>';
	getObject("bookTable").innerHTML=msg;
}

function doShow(lid) {
	iid="i"+lid+"";												// image id is <span> id with "i" prefix
	var s=getObject(lid); var k=getObject(iid); 
	if(s.className=='sh') {
		s.className='th';k.src="images/forward.png";		// hide croppped text with expand icon
	} else {
		s.className='sh';k.src="images/back.png";			// show croppped text with collapse icon
	}
}	


//*******************************************************************************
//							B O O K   C O V E R S
//*******************************************************************************

 function bkShow(i) {
 	var msg = '';
	msg += '<p class="cp"><a href="javascript:unshow()"><img src="images/fileclose.png" alt="close" class="cImg"></a>Title:&nbsp;<strong>'+book[i][name]+'</strong></p>';
	var au = "Author";if(booktxt[i][auth].indexOf(",")>0 || booktxt[i][auth].indexOf("&")>0) au="Authors";
	msg+='<p class="cp">'+au+':&nbsp;<strong>'+booktxt[i][auth]+'</strong></p>';

// Changes to crop synopsis
	msg+='<p class="cp">Synopsis:&nbsp;';
	var abs=booktxt[i][synopsis];
	if(crop && abs.length>l) {									// if crop enabled and length greater than 'l'
		var croptxt=abs.indexOf(" ",l);						// find closest " "
		abs=booktxt[i][synopsis].slice(0,croptxt)+" ";		// crop text to this point
		abs+='<span class="th" id="w'+i+'">'+booktxt[i][synopsis].slice(croptxt)+'&nbsp;</span>';  
																			// rest of text goes within <span>
		abs+='<a href="javascript:doShow(\'w'+i+'\');"><img id="iw'+i+'" src="images/forward.png" class="i2"></a>';
	}
	msg+=abs+'<\/p>';
	if(book[i][ISBN]!="")  msg+='<p class="cp">ISBN:&nbsp;'+book[i][ISBN]+'</p>';
	if(book[i][price]!="") msg+='<p class="cp">Price:&nbsp;'+book[i][price]+'</p>';
	msg+='<p class="cp">Available:&nbsp;'+booktxt[i][available]+'</p>';
	msg+='<p class="cp">Contact:&nbsp;'+booktxt[i][contact]+'</p>';
//	alert(msg);
	var t=getObject("bif");
	y=document.body.scrollTop;					// get top of window if scrolled
	t.style.top=200+y+"px";						// position down from this window's top
	t.innerHTML=msg;							// place contact info.
	t.style.display = "block";					// and display it
}
function unshow() {
	getObject("bif").style.display = "none";					// and hide it
}
