/*
 * We need to display a link to the PDF version of this booklist.
 * Can't do it server side, so read build the link client side.
 * 
 * The PDF files must be stored in the 'pdfFilesUrl' location
 * and must follow a naming convention
 * 		booklist-[school]-[year].pdf.
 * For example, for MORI year 12 the PDF must be named
 * 		booklist-mori-12.pdf
 */

var pdfFilesUrl = "http://www.betterbooks.com.au/files";
var pdfLinkText = "Download Book List Order Form";
var downloadArrowUrl = "http://www.betterbooks.bookweb.com.au/images/blue-arrow.gif";

/*
 * We are on the Text List page if the URL contains textlist.cgi
 */
function isTextListPage() {
	return (document.location.href).indexOf("textlist.cgi") > 0;
}

/*
 * Construct the HTML that is to be displayed within the PDF panel.
 */
function getPdfDownloadHtml(school,year) {
	var pdfUrl = (pdfFilesUrl + "/booklist-"+ school + "-" + year + ".pdf").toLowerCase();
	var pdfLink = "<a href=\"" + pdfUrl + "\"><img src=\"" + downloadArrowUrl + "\" border=\"0\" align=\"absmiddle\"></a>&nbsp;";
	var pdfLink = pdfLink + "<a href=\"" + pdfUrl + "\">" + pdfLinkText + "</a>";
	var pdfHtml = "<p>" + pdfLink + "</p>";
	return pdfHtml;
}

/*
 * Displays a PDF lnk within the PDF panel for the currently
 * selected school and year.
 */
function showPdfDownloadPanel(school,year) {
	var pdfPanel = $("#pdfDownload");
	var pdfHtml = getPdfDownloadHtml(school,year);
	$(pdfPanel).append(pdfHtml);
}

/*
 * This is called then the DOM is ready.
 */
$(document).ready(function(){
	// Only display the PDF link if we are on the booklist page.
	if (isTextListPage()) {
		// Get the school and year from the URL parameters
		var school = $(document).getUrlParam("SUBJECT1");
		var year = $(document).getUrlParam("SYEAR");
		// For the moment, the PDFs are only available for MORI years T to 6 and year 12.
		if (school == "MORI") {
			showPdfDownloadPanel(school,year);
		}
	}
});
