/* 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("انجيل متی", 28, "Matt"));
Books.push(new Book("انجيل مرقس", 16, "Mark"));
Books.push(new Book("انجيل لوقا", 24, "Luke"));
Books.push(new Book("انجيل یوحنا", 21, "John"));
Books.push(new Book("اعمال رسولان", 28, "Acts"));
Books.push(new Book("روميان", 16, "Rom"));
Books.push(new Book("اول قرنتيان", 16, "1Cor"));
Books.push(new Book("دوم قرنتيان", 13, "2Cor"));
Books.push(new Book("غلاتيان", 6, "Gal"));
Books.push(new Book("افسسيان", 6, "Eph"));
Books.push(new Book("فيلپيان", 4, "Php"));
Books.push(new Book("کولسيان", 4, "Col"));
Books.push(new Book("اول تسالونيکيان", 5, "1Ths"));
Books.push(new Book("دوم تسالونيکيان", 3, "2Ths"));
Books.push(new Book("اول تيموتاوس", 6, "1Tim"));
Books.push(new Book("دوم تيموتاوس", 4, "2Tim"));
Books.push(new Book("تیتوس", 3, "Titus"));
Books.push(new Book("فليمون", 1, "Phm"));
Books.push(new Book("عبرانيان", 13, "Heb"));
Books.push(new Book(" يعقوب ", 5, "Jam"));
Books.push(new Book("رساله اول پترس رسول", 5, "1Pet"));
Books.push(new Book("رساله دوم پترس رسول", 3, "2Pet"));
Books.push(new Book(" اول يوحنا ", 5, "1Jn"));
Books.push(new Book("دوم يوحنا", 1, "2Jn"));
Books.push(new Book(" سوم يوحنا", 1, "3Jn"));
Books.push(new Book(" يهودا", 1, "Jude"));
Books.push(new Book("مکاشفه", 22, "Rev"));

var Numbers = new Array();
Numbers[1] = "۱";
Numbers[2] = "۲";
Numbers[3] = "۳";
Numbers[4] = "۴";
Numbers[5] = "۵";
Numbers[6] = "۶";
Numbers[7] = "۷";
Numbers[8] = "۸";
Numbers[9] = "۹";
Numbers[10] = "۱۰";
Numbers[11] = "۱۱";
Numbers[12] = "۱۲";
Numbers[13] = "۱۳";
Numbers[14] = "۱۴";
Numbers[15] = "۱۵";
Numbers[16] = "۱۶";
Numbers[17] = "۱۷";
Numbers[18] = "۱۸";
Numbers[19] = "۱۹";
Numbers[20] = "۲۰";
Numbers[21] = "۲۱";
Numbers[22] = "۲۲";
Numbers[23] = "۲۳";
Numbers[24] = "۲۴";
Numbers[25] = "۲۵";
Numbers[26] = "۲۶";
Numbers[27] = "۲۷";
Numbers[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 = hRef + bList.options[bList.selectedIndex].book.Filename();
			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);
}	
	