﻿var g_verse = 0;

function Page_Init() {
try{
	clearList($('#lstBibles'));

	addToList($('#lstBibles'), 'American Standard Version', 'BibleASV');
	addToList($('#lstBibles'), 'Basic English', 'BibleBasicEnglish');
	addToList($('#lstBibles'), 'Darby', 'BibleDarby');
	addToList($('#lstBibles'), 'King James Version', 'BibleKJV');
	addToList($('#lstBibles'), 'World English Bible', 'BibleWeb');
	addToList($('#lstBibles'), 'Webster', 'BibleWebster');
	addToList($('#lstBibles'), 'Young\'s Literal Translation', 'BibleYLT');

	$('divPrint').hide(0);

	fillBooks();

	$('#tabs').html('');
	$('#tabBody').html('');

	$('#mnuHome').css({ opacity: 0.25 });
	}catch(e)
	{
		alert('this is an error:' + e.toString());
	}
}

function showHideTabs(index) {
	$('.tab').removeClass('tabHighlight');
	$('.tabBody').hide(0);

	if (!$('#tab' + index).hasClass('tabHighlight')) {
		$('#tab' + index).addClass('tabHighlight');
	}

	$('#tabBody' + index).show();
}

function fillBooks() {
	BibleStudy.WebServices.wsBibleStudy.GetBooks($('#lstBibles').val(), onGetBooksComplete, onError);
}

function onGetBooksComplete(result) {
	clearList($('#lstBooks'));

	for (i = 0; i < result.length; i++) {
		addToList($('#lstBooks'), result[i].BookName, result[i].BookID);
	}

	fillChapters();
}

function fillChapters() {
	BibleStudy.WebServices.wsBibleStudy.GetChapters($('#lstBibles').val(), $('#lstBooks').val(), onGetChaptersComplete, onError);
}

function onGetChaptersComplete(result) {
	clearList($('#lstChapters'));

	for (i = 0; i < result.length; i++) {
		addToList($('#lstChapters'), result[i].ChapterNumber, result[i].ChapterNumber);
	}

	fillVerses();
}

function fillVerses() {
	BibleStudy.WebServices.wsBibleStudy.GetVerses($('#lstBibles').val(), $('#lstBooks').val(), $('#lstChapters').val(), onGetVersesComplete, onError);
}

function onGetVersesComplete(result) {
	$('#divVerse').html('');

	for (i = 0; i < result.length; i++) {
		$('#divVerse').append('<sup style="font-size: 9; color: #696969; font-weight: bold;">' + result[i].VerseNumber + '.</sup> ' + '<a id="verse' + result[i].VerseNumber + '" onclick="getContext(' + result[i].VerseNumber + ', this);" style="cursor: pointer;">' + result[i].VerseText + '</a>');

		if (i == 0) {
			getContext(result[i].VerseNumber, document.getElementById('verse' + result[i].VerseNumber));
		}
	}
}

function getContext(verse, a) {
	g_verse = verse;

	BibleStudy.WebServices.wsBibleStudy.GetContext($('#lstBooks').val(), $('#lstChapters').val(), g_verse, onGetContextComplete, onError, a);
}

function onGetContextComplete(result, a) {
	createDefaultTabs();

	var elements = $('#divVerse a');

	for (i = 0; i < elements.length; i++) {
		elements[i].style.color = '#696969';
	}

	if (a != null) {
		a.style.color = 'red';
	}

	var defaultTab = 'All';
	var user = getUserFromCookie();

	if (result.length > 0) {
		for (i = 0; i < result.length; i++) {

			if (i > 0) {
				$('#tabBodyAll').append('<br /><br />');
			}

			$('#tabBodyAll').append('<b>' + result[i].User.UserName + '</b>:<br />' + result[i].Text);

			if (user.UserID > 0) {
				if (user.UserID == result[i].User.UserID) {
					defaultTab = i.toString();

					createEditableTabs(i, result[i].User.UserName, result[i].Text)
				}
				else {
					createTabs(i, result[i].User.UserName, result[i].Text);
				}
			}
			else {
				createTabs(i, result[i].User.UserName, result[i].Text);
			}
		}
	}

	if (defaultTab == 'All') {
		if (user.UserID > 0) {
			defaultTab = result.length;
			createEditableTabs(result.length, user.UserName, '');
		}
	}

	showHideTabs(defaultTab);
}

function createDefaultTabs() {
	$('#tabs').html('<div id="tabAll" onclick="showHideTabs(\'All\');" class="tab"><b>All</b></div>');

	$('#tabBody').html('<div id="tabBodyAll" class="tabBody"></div>');
}

function createEditableTabs(index, header, context) {
	$('#tabs').append('<div id="tab' + index + '" onclick="showHideTabs(\'' + index + '\');" class="tab">' + header + '</div>');
	$('#tabBody').append('<div id="tabBody' + index + '" class="tabBody"><textarea id="txttab" class="txtTab tinymce">' + context + '</textarea></div>');
	
	$('#txttab').tinymce({
		// Location of TinyMCE script
		//script_url: 'Scripts/tiny_mce.js',

		// General options
		theme: "advanced",
		//plugins: "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
		plugins: "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

		// Theme options
//		theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
//		theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
//		theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
//		theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_buttons1: "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo",
		theme_advanced_buttons2: "link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor, tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,advhr",
		theme_advanced_buttons3: "",
		theme_advanced_buttons4: "",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_statusbar_location: "bottom",
		theme_advanced_resizing: false,

		// Example content CSS (should be your site CSS)
		content_css: "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url: "lists/template_list.js",
		external_link_list_url: "lists/link_list.js",
		external_image_list_url: "lists/image_list.js",
		media_external_list_url: "lists/media_list.js",
	});

	$('#tabBody' + index).css({ overflow: 'hidden' });
	$('#tabBody' + index).hide(0);
}

function createTabs(index, header, context) {
	$('#tabs').append('<div id="tab' + index + '" onclick="showHideTabs(\'' + index + '\');" class="tab">' + header + '</div>');

	$('#tabBody').append('<div id="tabBody' + index + '" class="tabBody">' + context + '</div>');

	$('#tabBody' + index).hide(0);
}

function print_page() {
	$('#divPrint').show();
	$('#divPrintShadow').show();

	$('#divPrint').html('<h1>' + $('#lstBibles option:selected').text() + '</h1><h2>' + $('#lstBooks option:selected').text() + '</h2><h3>Chapter ' + $('#lstChapters').val() + '</h3>');
	$('#divPrint').append($('#divVerse').html());

	$('#divPrint a').css({ color: '#000000', fontSize: 18 });
	$('#divPrint sup').css({ color: '#000000' });

	var point = {};
	point.height = $('#divPage').height();
	point.left = (document.body.clientWidth / 2) - ($('#divPage').width() / 2);
	point.width = $('#divPage').width();

	$('#divPrint').css({ left: point.left, top: 20, width: point.width - 20 });
	$('#divPrintShadow').css({ left: point.left + 10, top: 30, height: $('#divPrint').height() + 20, width: $('#divPrint').width() + 20 });

	var printed = window.print();

	$('#divPrint').html('');
	$('#divPrint').hide(0);
	$('#divPrintShadow').hide(0);
}

function delete_page() {
	if (confirm('Are you sure you want to delete this item?')) {
		var user = getUserFromCookie();

		BibleStudy.WebServices.wsBibleStudy.DeleteContext($('#lstBooks').val(), $('#lstChapters').val(), g_verse, user.UserID, onDeleteComplete, onError);
	}
}

function onDeleteComplete(result) {
	if (result.length > 0) {
		alert(result);
	}
	else {
		getContext(g_verse, document.getElementById('verse' + g_verse));

		var height = 100;
		var width = 400;

		var top = $('#tabBody').offset().top + ($('#tabBody').height() / 2) - (height / 2);
		var left = $('#tabBody').offset().left + ($('#tabBody').width() / 2) - (width / 2);

		showEventMessage(top, left, 'Delete Context', 'Delete completed successfully.', 'bin_closed.png', 2000);
	}
}

function save_page() {
	var user = getUserFromCookie();

	BibleStudy.WebServices.wsBibleStudy.SaveContext($('#lstBooks').val(), $('#lstChapters').val(), g_verse, user.UserID, $('#txttab').html(), onSaveComplete, onError);
}

function onSaveComplete(result) {
	if (result.length > 0) {
		alert(result);
	}
	else {
		getContext(g_verse, document.getElementById('verse' + g_verse));

		var height = 100;
		var width = 400;

		var top = $('#tabBody').offset().top + ($('#tabBody').height() / 2) - (height / 2);
		var left = $('#tabBody').offset().left + ($('#tabBody').width() / 2) - (width / 2);

		showEventMessage(top, left, 'Save Context', 'Save completed successfully.', 'disk.png', 2000);
	}
}