// JavaScript Document
// Script for displaying and sorting PDF files downloads
// function to write the information to the page.
function changeText(text2change2, locName) {
	var location = document.getElementById(locName);
	location.innerHTML = text2change2;
}

//  function to select the relevant php document name
function getPhpCode(title) {
	switch (title) {
		case "Builder, The":
			return "builder";
			break;
		case "Father, The":
			return "father";
			break;
		case "Messenger, The":
			return "angel";
			break;
		case "Serpent and the Shadow, The":
			return "shadow";
			break;
		case "24 Beckenridge Drive":
			return "24beck";
			break;
		case "Reunion":
			return "reunion";
			break;
		case "Wolf, The &mdash; The First Tale":
			return "c01-wolf";
			break;
		case "Sorceress, The &mdash; The Second Tale":
			return "c02-sorc";
			break;
		case "Labyrinth, The &mdash; The Third Tale":
			return "c03-laby";
			break;
		case "Huntress &mdash; The Fourth Tale":
			return "c04-hunt";
			break;
		case "1989":
			return "1989";
			break;
		case "Death and Life":
			return "dnl";
			break;
		case "Divine Verdict":
			return "verdict";
			break;
		case "Interview with the Halfling Prince, An":
			return "prince";
			break;
		case "Land of Adventure":
			return "loa";
			break;
		case "Mother Faces the Shaitan":
			return "mfts";
			break;
		}
}

// function to load the  PDF size
function getPdfSize(title) {
	switch (title) {
		case "Builder, The":
			return 337;
			break;
		case "Father, The":
			return 59;
			break;
		case "Messenger, The":
			return 42;
			break;
		case "Serpent and the Shadow, The":
			return 107;
			break;
		case "24 Beckenridge Drive":
			return 387;
			break;
		case "Reunion":
			return 474;
			break;
		case "Wolf, The &mdash; The First Tale":
			return 354;
			break;
		case "Sorceress, The &mdash; The Second Tale":
			return 224;
			break;
		case "Labyrinth, The &mdash; The Third Tale":
			return 267;
			break;
		case "Huntress &mdash; The Fourth Tale":
			return 119;
			break;
		case "1989":
			return 158;
			break;
		case "Death and Life":
			return 28;
			break;
		case "Divine Verdict":
			return 167;
			break;
		case "Interview with the Halfling Prince, An":
			return 269;
			break;
		case "Land of Adventure":
			return 57;
			break;
		case "Mother Faces the Shaitan":
			return 148;
			break;		
	}
}

// actual sorting function
function sortStories(sorttype) {
	var table = "";
	// define titles array
	var title = ["Builder, The", "Father, The", "Messenger, The", "Serpent and the Shadow, The", "24 Beckenridge Drive", "Reunion", "Wolf, The &mdash; The First Tale", "Sorceress, The &mdash; The Second Tale", "Labyrinth, The &mdash; The Third Tale", "Huntress &mdash; The Fourth Tale", "1989", "Death and Life", "Divine Verdict", "Interview with the Halfling Prince, An", "Land of Adventure", "Mother Faces the Shaitan"];

	// define categories
	var biblical = [0, 1, 2, 3];
	var southall = [4, 5];
	var carrock = [6, 7, 8, 9];
	var shorts = [10, 11, 12, 13, 14, 15];

	// select sort type
	if (sorttype == "title") {
		title.sort();
		table = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
        table += " <tr align=\"left\" valign=\"middle\">\n";
        table += "    <td colspan=\"3\"><h2>Titles</h2></td>\n";
        table += "    </tr>\n";
		for (val in title) {
			table += "  <tr align=\"left\" valign=\"middle\">\n";
            table += "    <td>&nbsp;</td>\n";
            table += "   <td align=\"center\"><img src=\"../images/pdf_icon.png\" alt=\"PDF Icon\" width=\"20\" height=\"20\" vspace=\"2\"></td>\n";
            table += "    <td><a href=\"../pdf/" + getPhpCode(title[val]) + ".pdf\">" + title[val] + "</a> <span class=\"ltlinks\">[" + getPhpCode(title[val]) + ".pdf | " + getPdfSize(title[val]) + " KB]</span></td>\n";
            table += "  </tr>\n";
		}
            table += "</table>\n";
	
	} else {
	    table = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
	    // add biblical fiction
        table += " <tr align=\"left\" valign=\"middle\">\n";
        table += "    <td colspan=\"3\"><h2>Biblical Fiction</h2></td>\n";
        table += "    </tr>\n";
		for (val in biblical) {
            table += "  <tr align=\"left\" valign=\"middle\">\n";
            table += "    <td>&nbsp;</td>\n";
            table += "   <td align=\"center\"><img src=\"../images/pdf_icon.png\" alt=\"PDF Icon\" width=\"20\" height=\"20\" vspace=\"2\"></td>\n";
            table += "    <td><a href=\"../pdf/" + getPhpCode(title[biblical[val]]) + ".pdf\">" + title[biblical[val]] + "</a> <span class=\"ltlinks\">[" + getPhpCode(title[biblical[val]]) + ".pdf | " + getPdfSize(title[biblical[val]]) + " KB]</span></td>\n";
            table += "  </tr>\n";
		}
		
	    // add southall stories
        table += " <tr align=\"left\" valign=\"middle\">\n";
        table += "    <td colspan=\"3\"><h2>Stories about Soutall</h2></td>\n";
        table += "    </tr>\n";
		for (val in southall) {
            table += "  <tr align=\"left\" valign=\"middle\">\n";
            table += "    <td>&nbsp;</td>\n";
            table += "   <td align=\"center\"><img src=\"../images/pdf_icon.png\" alt=\"PDF Icon\" width=\"20\" height=\"20\" vspace=\"2\"></td>\n";
            table += "    <td><a href=\"../pdf/" + getPhpCode(title[southall[val]]) + ".pdf\">" + title[southall[val]] + "</a> <span class=\"ltlinks\">[" + getPhpCode(title[southall[val]]) + ".pdf | " + getPdfSize(title[southall[val]]) + " KB]</span></td>\n";
            table += "  </tr>\n";
		}

	    // add carrock stories
        table += " <tr align=\"left\" valign=\"middle\">\n";
        table += "    <td colspan=\"3\"><h2>Tales from Carrock</h2></td>\n";
        table += "    </tr>\n";
		for (val in carrock) {
            table += "  <tr align=\"left\" valign=\"middle\">\n";
            table += "    <td>&nbsp;</td>\n";
            table += "   <td align=\"center\"><img src=\"../images/pdf_icon.png\" alt=\"PDF Icon\" width=\"20\" height=\"20\" vspace=\"2\"></td>\n";
            table += "    <td><a href=\"../pdf/" + getPhpCode(title[carrock[val]]) + ".pdf\">" + title[carrock[val]] + "</a> <span class=\"ltlinks\">[" + getPhpCode(title[carrock[val]]) + ".pdf | " + getPdfSize(title[carrock[val]]) + " KB]</span></td>\n";
            table += "  </tr>\n";
		}

	    // add other short stories
        table += " <tr align=\"left\" valign=\"middle\">\n";
        table += "    <td colspan=\"3\"><h2>Other Short Stories</h2></td>\n";
        table += "    </tr>\n";
		for (val in shorts) {
            table += "  <tr align=\"left\" valign=\"middle\">\n";
            table += "    <td>&nbsp;</td>\n";
            table += "   <td align=\"center\"><img src=\"../images/pdf_icon.png\" alt=\"PDF Icon\" width=\"20\" height=\"20\" vspace=\"2\"></td>\n";
            table += "    <td><a href=\"../pdf/" + getPhpCode(title[shorts[val]]) + ".pdf\">" + title[shorts[val]] + "</a> <span class=\"ltlinks\">[" + getPhpCode(title[shorts[val]]) + ".pdf | " + getPdfSize(title[shorts[val]]) + " KB]</span></td>\n";
            table += "  </tr>\n";
		}

        table += "</table>\n";

	}

	// write out information using changeText()
	changeText(table, "storyPdfs");

}

