/* List of books */
// Globals for the objects we will create or reference
var controlDiv, bList, cList, btnMP, btnRA, label;
var hRef = "http://audio.sadayezindagi.com/audio/voice/DDB/";

// Create an array of books...iterated through when we create
// the book dropdown list
var Books = new Array();
Books.push(new Book("Matthew", 28, "Matt"));
Books.push(new Book("Mark", 16, "Mark"));
Books.push(new Book("Luke", 24, "Luke"));
Books.push(new Book("John", 21, "John"));
Books.push(new Book("Acts", 28, "Acts"));
Books.push(new Book("Romans", 16, "Rom"));
Books.push(new Book("1st Corinthians", 16, "1Cor"));
Books.push(new Book("2nd Corinthians", 13, "2Cor"));
Books.push(new Book("Galatians", 6, "Gal"));
Books.push(new Book("Ephesians", 6, "Eph"));
Books.push(new Book("Philipians", 4, "Php"));
Books.push(new Book("Colossians", 4, "Col"));
Books.push(new Book("1st Thessalonians", 5, "1Ths"));
Books.push(new Book("2nd Thessalonians", 3, "2Ths"));
Books.push(new Book("1st Timothy", 6, "1Tim"));
Books.push(new Book("2nd Timothy", 4, "2Tim"));
Books.push(new Book("Titus", 3, "Titus"));
Books.push(new Book("Philemon", 1, "Phm"));
Books.push(new Book("Hebrews", 13, "Heb"));
Books.push(new Book("James", 5, "Jam"));
Books.push(new Book("1st Peter", 5, "1Pet"));
Books.push(new Book("2nd Peter", 3, "2Pet"));
Books.push(new Book("1st John", 5, "1Jn"));
Books.push(new Book("2nd John", 1, "2Jn"));
Books.push(new Book("3rd John", 1, "3Jn"));
Books.push(new Book("Jude", 1, "Jude"));
Books.push(new Book("Revelation", 22, "Rev"));

var Numbers = new Array();
Numbers[1] = "1";
Numbers[2] = "2";
Numbers[3] = "3";
Numbers[4] = "4";
Numbers[5] = "5";
Numbers[6] = "6";
Numbers[7] = "7";
Numbers[8] = "8";
Numbers[9] = "9";
Numbers[10] = "10";
Numbers[11] = "11";
Numbers[12] = "12";
Numbers[13] = "13";
Numbers[14] = "14";
Numbers[15] = "15";
Numbers[16] = "16";
Numbers[17] = "17";
Numbers[18] = "18";
Numbers[19] = "19";
Numbers[20] = "20";
Numbers[21] = "21";
Numbers[22] = "22";
Numbers[23] = "23";
Numbers[24] = "24";
Numbers[25] = "25";
Numbers[26] = "26";
Numbers[27] = "27";
Numbers[28] = "28";

// Create a select list on the book titles
// Since we assign the book as a property of the option
// we don't need to track the index
function BookSelect() {
	var sel = document.createElement("SELECT");
	var i;

	// ID for CSS
	sel.id = "books";	
	
	// Add the options
	for (i = 0; i < Books.length; i++) {
		var opt = document.createElement("OPTION");
		
		// By assigning the Book as a property of the option we can always retreive it
		// from here on out we can use opt.book.....
		opt.book = Books[i];
		opt.value = i;
		opt.appendChild(document.createTextNode(opt.book.Name()));
		
		// The onchange for the book - overrides the empty method in the book object
		opt.book.onchange = function(e) {
			e = fixE(e);
			label.innerHTML = e.target.Filename();
		}
		
		sel.appendChild(opt);
	}
	
	// On change for the books select list
	sel.onchange = function(e) {
		e = fixE(e);

		// Get the new chapter select list object
		var newCList = e.target.options[e.target.selectedIndex].book.render();
		// Replace the old chapter select list object in the controlDiv with the new one
		controlDiv.replaceChild(newCList, cList);
		// Assign the new chapter select list object to the global variable
		cList = newCList;
		// Update the label
		// Note that each chapter select list maintains it's own index
		// so when we go back to a book we are on the chapter we left off at
		label.innerHTML = e.target.options[e.target.selectedIndex].book.Filename();
	}
	
	return sel;
}

// Initialize and render the object first time through
function init(id) {
	// controlDiv is the container that hold all the controls
	controlDiv = document.getElementById(id);
	
	// bList is the books select list
	bList = new BookSelect();
	
	// cList is the current chapter select list
	cList = bList.options[0].book.render();
	
	// Since the button is in the middle of the changing object it's
	// easiest to render it in code
	btnMP = document.createElement("BUTTON");
	btnMP.appendChild(document.createTextNode("Media Player"));
	btnMP.onclick = function() {
		try {
			document.location.href = "http://www.sadayezindagi.com/playmp3.jsp?url=DDB/" + bList.options[bList.selectedIndex].book.Filename();
		} catch (e) {}
	}
	
	// create the Real Audio button
	btnRA = document.createElement("BUTTON");
	btnRA.appendChild(document.createTextNode("Real Audio"));
	btnRA.onclick = function() {
		try {
			document.location.href = "http://www.sadayezindagi.com/playrealaudio.jsp?url=DDB/" + bList.options[bList.selectedIndex].book.Filename();
		} catch (e) {}
	}
	
	// Create the label object that will hold the filename
	label = document.createElement("SPAN");
	label.innerHTML = bList.options[0].book.Filename();
	

	// Render the objects in order (controlDiv is already part of the body)	
	controlDiv.appendChild(bList);
	controlDiv.appendChild(cList);
	controlDiv.appendChild(btnRA);
	controlDiv.appendChild(btnMP);
	controlDiv.appendChild(document.createElement("BR"));
//	controlDiv.appendChild(document.createTextNode("Selected File: "));
//	controlDiv.appendChild(label);
}	
	
if (window.onload) {
	var oldLoad = window.onload;
	window.onload = function(e) {
		oldLoad(e);
		init("control");
	}
} else {
	window.onload = function(e) {
		init("control");
	}
}
