var currentBible;
var currentBibleLevel2;

$(document).ready(function() {
	
	// Slide bible panel horizontally on click
	$('#btn_bible_panel').click(function() {
		if ($('#btn_bible_panel').hasClass('panel_open')) {
			$('#bible_panel').animate({left:0}, 400, function(){
				$('#btn_bible_panel').removeClass('panel_open'); 
				updateScrollPanes();
			});
			$('#holder3').animate({left:165,width:464}, 400);
			$('#col3').animate({width:432}, 400);
			$('#holder3 .jScrollPaneContainer').animate({width:464}, 400);
			$('#bible_search_field').animate({left:250,width:390}, 400);
		} else {
			$('#bible_panel').animate({left:-242}, 400, function(){
				$('#btn_bible_panel').addClass('panel_open'); 
				updateScrollPanes();
			});
			$('#holder3').animate({left:-65,width:700}, 400);
			$('#col3').animate({width:650}, 400);
			$('#holder3 .jScrollPaneContainer').animate({width:694}, 400);
			$('#bible_search_field').animate({left:20,width:600}, 400);
		}
		return false;
	});
	
/*	// Toggle radio buttons on click
	$('#label_radio_ot').click(function() {
		$('#radio_ot').attr('checked', true);
	});
	$('#label_radio_nt').click(function() {
		$('#radio_nt').attr('checked', true);
	});*/
	
	// Search the bible
	$('#bible_search').submit(function(){
		var k = $('#bible_search_field').val();
		searchByKeyword(k);
		return false;
	});
	
	// Get book and chapter from URL
	var bookID = -1;
	var chapterID = -1;
	var ch = (get_param('ch') != '') ? parseInt(get_param('ch')) - 1 : 0;
	
	if (get_param('bk') != '') {
		var selectedBook = get_param('bk').toLowerCase();
		
		// Search for book in old testament
		$.each(level1_0, function(entryIndex, entry) {
			
			// Match found in old testament
			if (selectedBook == entry.toString().toLowerCase()) {
				bookID = entryIndex;
				chapterID = ch;
				initBible(0);
			}
		});
		
		// No match in old testament, continue searching in new testament
		if (bookID == -1) {
			$.each(level1_1, function(entryIndex, entry) {
				
				// Match found in new testament
				if (selectedBook == entry.toString().toLowerCase()) {
					bookID = entryIndex;
					chapterID = ch;
					initBible(1);
				}
			});
		}
	}
	
	// No params sent, or no match in either old or new testament. Default to old testament
	if (bookID == -1) {
		bookID = 0;
		chapterID = 0;
		initBible(0);
	}
	
	// Load book and chapter lists
	loadFirstLevel(bookID, chapterID);
	
	// Perform search if parameter is passed along in URL
	var s = get_param('search');
	if (s != '') {
		$('#bible_search_field').val(s);
		searchByKeyword(s);
	}
});

function initBible(bibleType) {
	// No bible type, so event was triggered from select element onchange event
	// Pull bible type from currently selected option value
	if ((bibleType != 0) && (bibleType != 1)) {
		bibleType = $('input[name=tt]:checked').val();
	}
	
	// New Testament
	if (bibleType == 1) {
		currentBible = level1_1;
		currentBibleLevel2 = level2_1;
		
		// Highlight selected option
		$('input[name=tt]:last').attr('checked', 'checked');
	}
	
	// Default to Old Testament
	else {
		currentBible = level1_0;
		currentBibleLevel2 = level2_0;
		
		// Highlight selected option
		$('input[name=tt]:first').attr('checked', 'checked');
	}
}

function loadFirstLevel(x,y) {
	$("#col1 ul").empty();
	var html = '';
	$.each(currentBible, function(entryIndex, entry) {
		if (x == entryIndex) {
			html += '<li class="current_bible"';
		} else {
			html += '<li ';
		}
		html += 'onClick="loadFirstLevel(' + entryIndex + ',0);">' + entry + '</li>';
	});
	$("#col1 ul").html(html);
	
	// Load chapter list into 2nd column
	loadSecondLevel(x,y);
	
	// Highlight current testament
	if ($("#searchForm input#radio_ot:checked").length == 1) {
		$("#label_radio_nt").removeClass('current_testament');
		$('#label_radio_ot').addClass('current_testament');
	} else if ($("#searchForm input#radio_nt:checked").length == 1) {
		$("#label_radio_ot").removeClass('current_testament');
		$('#label_radio_nt').addClass('current_testament');
	}
}

function loadSecondLevel(x, y) {
	$("#col2 ul").empty(); 
	var html = '';
	var chapters = currentBibleLevel2[x];
	$.each(chapters, function(entryIndex, entry) {
		if (y == entryIndex) {
			html += '<li class="current_bible"';
		} else {
			html += '<li ';
		}
		html += 'onClick="loadSecondLevel(' + x + ',' + entryIndex + ');">' + entry + '</li>';
	});
	$("#col2 ul").html(html);
	
	// Load actual chapter into 3rd column
	loadChapterByIndex(x, y);
}

function loadChapterByIndex(x, y) {
	$('#bible_loader').fadeIn(200);
	ajax_proxy({
		'name': currentBible[x],
		'chapter': currentBibleLevel2[x][y],
		'cnv_url': 'http://churchfinder.ichristianlife.com/prod/churchfinder.ichristianlife.com/bible/bible_chapter.php5'
	}, {
		dataType: 'json',
		type: 'POST',
		async: 'false',
		success: function(data) {
			var str = '<span class="chapter_title">'+ currentBible[x] + ' ' + currentBibleLevel2[x][y] + '</span>';
			$.each(data, function(i, item) {
				str += '<sup>' + item.line + '</sup>' + item.passage;
			});
			$('#col3 ul').html(str);
			$('#bible_loader').fadeOut(200);
			updateScrollPanes();
		}
	});
}

function searchByKeyword(k) {
	$('#bible_loader').fadeIn(200);
	ajax_proxy({
		'keywd': k,
		'cnv_url': 'http://churchfinder.ichristianlife.com/prod/churchfinder.ichristianlife.com/bible/bible_search.php5'
	}, {
		dataType: "json",
		type: 'POST',
		success: function(data) {
			var str = '';
			if (data == null) {
				str = 'No results found...';
			} else {
				$.each(data, function(i, item) {
					itm = item;
					var bookID = -1;
					
					// Search for match in old testament
					for (var j=0; j<level1_0.length; j++) {
						
						// Match found in old testament
						if (item.book == level1_0[j][0]) {
							bookID = j;
							str += '<span class="searchresult">' + item.passage + '<br/>';
							str += '<li onClick="initBible(0); loadFirstLevel(' + bookID + ',' + (item.chapter - 1) + ');">';
							str += 'View Chapter (' + item.book  + ' ' + item.chapter + ':' + item.line + ')</li></span>';
							break;
						}
					}
					
					// Search for match in new testament (only if not match found in old testament)
					if (bookID == -1) {
						for (var j=0; j<level1_1.length; j++) {
							
							// Match found in new testament
							if (item.book == level1_1[j][0]) {
								bookID = j;
								str += '<span class="searchresult">' + item.passage + '<br/>';
								str += '<li onClick="initBible(1); loadFirstLevel(' + bookID + ',' + (item.chapter - 1) + ');">';
								str += 'View Chapter (' + item.book  + ' ' + item.chapter + ':' + item.line + ')</li></span>';
								break;
							}
						}
					}
				});
			}
			$('#col3 ul').html(str);
			$('#bible_loader').fadeOut(200);
			updateScrollPanes();
		}
	});
}

function updateScrollPanes() {
	$('#col1, #col2, #col3').jScrollPane({scrollbarWidth:22, scrollbarMargin:10, showArrows:true});
	if (!$('#btn_bible_panel').hasClass('panel_open')) {
		$('#col3').width(432);
	}
}


var level1_0 = new Array(
	['Genesis'],
	['Exodus'],
	['Leviticus'],
	['Numbers'],
	['Deuteronomy'],
	['Joshua'],
	['Judges'],
	['Ruth'],
	['1 Samuel'],
	['2 Samuel'],
	['1 Kings'],
	['2 Kings'],
	['1 Chronicles'],
	['2 Chronicles'],
	['Ezra'],
	['Nehemiah'],
	['Esther'],
	['Job'],
	['Psalms'],
	['Proverbs'],
	['Ecclesiastes'],
	['Solomon'],
	['Isaiah'],
	['Jeremiah'],
	['Lamentations'],
	['Ezekiel'],
	['Daniel'],
	['Hosea'],
	['Joel'],
	['Amos'],
	['Obadiah'],
	['Jonah'],
	['Micah'],
	['Nahum'],
	['Habakkuk'],
	['Zephaniah'],
	['Haggai'],
	['Zechariah'],
	['Malachi']
);

var level2_0 = new Array(   
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21],
	[ 1 , 2 , 3 , 4],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103 , 104 , 105 , 106 , 107 , 108 , 109 , 110 , 111 , 112 , 113 , 114 , 115 , 116 , 117 , 118 , 119 , 120 , 121 , 122 , 123 , 124 , 125 , 126 , 127 , 128 , 129 , 130 , 131 , 132 , 133 , 134 , 135 , 136 , 137 , 138 , 139 , 140 , 141 , 142 , 143 , 144 , 145 , 146 , 147 , 148 , 149 , 150],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52],
	[ 1 , 2 , 3 , 4 , 5],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14],
	[ 1 , 2 , 3],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9],
	[ 1],
	[ 1 , 2 , 3 , 4],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7],
	[ 1 , 2 , 3],
	[ 1 , 2 , 3],
	[ 1 , 2 , 3],
	[ 1 , 2],
	[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14],
	[ 1 , 2 , 3 , 4]

);

var level1_1 = new Array(
	['Matthew'],
	['Mark'],
	['Luke'],
	['John'],
	['Acts'],
	['Romans'],
	['1 Corinthians'],
	['2 Corinthians'],
	['Galatians'],
	['Ephesians'],
	['Philippians'],
	['Colossians'],
	['1 Thessalonians'],
	['2 Thessalonians'],
	['1 Timothy'],
	['2 Timothy'],
	['Titus'],
	['Philemon'],
	['Hebrews'],
	['James'],
	['1 Peter'],
	['2 Peter'],
	['1 John'],
	['2 John'],
	['3 John'],
	['Jude'],
	['Revelation']
);

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