/*
 * 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;
}

/*
 * Construct the HTML for the study guides panel
 */
function getStudyGuidesHtml(year) {
	var studyGuidesUrl = "http://www.studyguides.net.au/shop/textlist.cgi?SCLEN=06&SUBJECT1=STUDY&SYEAR=" + year;
	var link = "<a href=\"" + studyGuidesUrl + "\">Study Guides for Year " + year + "</a>";
	var html = "<p>Our study guides have moved!</p><p>" + link + "</p>";
	return html;
}

/*
 * Displays a PDF lnk within the PDF panel for the currently
 * selected school and year.
 */
function showPdfDownloadPanel(school,year) {
	var panel = $("#messagePanel");
	var html = getPdfDownloadHtml(school,year);
	$(panel).append(html);
}

function showStudyGuidesPanel(year) {
	var panel = $("#messagePanel");
	var html = getStudyGuidesHtml(year);
	$(panel).append(html);
}

/*
 * 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 == "STUDY") {
			showStudyGuidesPanel(year);
			$("form").remove();
		}
		else if (school == "MORI") {
			showPdfDownloadPanel(school,year);
		}
		
		//////////////////////////////////////////////////////
		//
		//  SPECIAL BOOK HANDLING FOR 2012
		//
		//////////////////////////////////////////////////////
		
		// Disallow the selection of certain books that are also part of a pack.
		var packPairs = [
		                 {pack:'9781442593916', book:'9781442504776'}, // New Signpost 7 Mathematics
		                 {pack:'9781740851626', book:'9781740851619'}, // Italian Ecco! Uno
		                 {pack:'9781742162355', book:'9781742161174'}, // Core Science Stage 4
		                 {pack:'9781442593923', book:'9781442506930'}, // New Signpost 8 Mathematics
		                 {pack:'9781442593923', book:'9781442506930'}, // New Signpost 8 Mathematics Enhanced
		                 {pack:'9781742162362', book:'9780730339519'}, // Core Science Stage 5 Text
		                 {pack:'9780733963476', book:'9780733969072'}, // Access to 5.1 Maths 9
		                 {pack:'9781740819909', book:'9781442506961'}, // New Signpost 9 Mathematics Enhanced 5.2
		                 {pack:'9781740819916', book:'9781442506992'}, // New Signpost 9 Mathematics Enhanced 5.3
		                 {pack:'9780733958137', book:'9780733969119'}, // Access to 5.1 Maths 10
		                 {pack:'9781740819886', book:'9781442507029'}, // New Signpost 10 Mathematics Enhanced 5.2
		                 {pack:'9781740819893', book:'9781442507050'}, // New Signpost 10 Mathematics Enhanced 5.3
		                 {pack:'9780333393420', book:'9780340849361'}, // A Midsummer Night's Dream
		                 {pack:'9780333485934', book:'9780340888087'}  //Much Ado About Nothing
		                 ];

		for (var i=0; i<packPairs.length; i++) {
			
			var packId = packPairs[i].pack;
			var bookId = packPairs[i].book;
			
			(function(packId, bookId) {
				
				// If the pack is selected then disallow the selection of the individual book
				var pack = $("input[itemno='" + packId + "']");
				var book = $("input[itemno='" + bookId + "']");
				
				if (pack && book) {
					pack.change(function(){
						var packSelected = pack.get(0).checked;
						if (packSelected) {
							book.removeAttr('checked');
							book.attr('disabled','disabled');
						}
						else {
							book.removeAttr('disabled');
						}
					});
				}
				
			})(packId, bookId);
			
		}
		
		// Geoactive 1 3rd Ed. & Jacaranda Atlas 7th Ed. Pack
		var geoPlusAtlas = $("input[itemno='9781742463322']");
		var geo = $("input[itemno='9781742160061']");
		var atlas = $("input[itemno='9781742461267']");
		if(geoPlusAtlas && geo && atlas) {
			var geoPlusAtlasHandler = function() {
				console.log('change');
				geoPlusAtlas.removeAttr('disabled');
				geo.removeAttr('disabled');
				atlas.removeAttr('disabled');
				var geoPlusAtlasSelected = geoPlusAtlas.get(0).checked;
				if (geoPlusAtlasSelected) {
					geo.removeAttr('checked').attr('disabled','disabled');
					atlas.removeAttr('checked').attr('disabled','disabled');
				}
			};
			geoPlusAtlas.change(geoPlusAtlasHandler);
			geo.change(geoPlusAtlasHandler);
			atlas.change(geoPlusAtlasHandler);
		}
		
		
	}
});

