﻿var AllBooks;
$(function () {
	startLoad();
	BibleService.GetBooks(booksCallBack);
});

function startLoad(){
	$("#slNumber").change(function(){
		GetChapterText();
	});
	$("#slBooks").change(function(){
		updateChaptersinDropDown();
	});
	$("#chkFormation").click(function(){
		GetChapterText();
	})
	$("#slFont").change(function(){
	    setFont();
	});
     var originalFontSize = $('#BibleContect').css('font-size');
        $("#lnkResetFont").click(function(){
        $('#BibleContect').css('font-size', originalFontSize);
      });

      $("#lnkIncreaseFont").click(function(){
        var currentFontSize = $('#BibleContect').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum+1;
        if (newFontSize<30)
        $('#BibleContect').css('font-size', newFontSize);
        return false;
      });

      $("#lnkDecreaseFont").click(function(){
      
        var currentFontSize = $('#BibleContect').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum-1;
        if (newFontSize >10)
        $('#BibleContect').css('font-size', newFontSize);

        return false;
      });

    $("#lnkFontOptions").click(function(){
    if ($("#FontOptions").css("display")=='none')
        $("#FontOptions").show("slow");
    else
        $("#FontOptions").hide("slow");
    })
	$("#lnkIncreaseFont").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
    $("#lnkDecreaseFont").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
    $("#lnkResetFont").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

	BibleService.GetBooks(booksCallBack);
}



function setFont(){
    var fontName=$("#slFont")[0].options[$("#slFont")[0].selectedIndex].value;
    $("#BibleContect").css("font-family",fontName);
}




function GetChapterText(){
	$("#BibleContect").hide("fast");
	$("#dvLoader").fadeIn("fast");
	var bookId= AllBooks[$("#slBooks")[0].selectedIndex].Id;
	var formationEnabled= $("#chkFormation")[0].checked;
	BibleService.GetChapter(bookId,$("#slNumber")[0].selectedIndex+1,formationEnabled,chapterCallBack);	
}

function updateChaptersinDropDown(){	
	$("#slNumber").empty();
	for (i=1;i<=AllBooks[$("#slBooks")[0].selectedIndex].ChapterCount;i++)
	{
		$("#slNumber").append("<option>" + i + "</option>");
	}
	GetChapterText();
	//reDraw("#dropDowns");
}



function chapterCallBack(result){
	$("#BibleContect").empty();
	$("#BibleContect").append(result.Content);
	$("#dvLoader").fadeOut("fast");
	$("#BibleContect").show("fast");
}

function booksCallBack(result){
	AllBooks=result;
	for (var i = 0; i < result.length; i++) {
		$("#slBooks").append("<option>" + result[i].DisplayBook + "</option>");
	}
	updateChaptersinDropDown();
	
}

//function reDraw (selector){
//    // this function is used to fix the glitch for rtl in IE
//    var booktmp=$("#slBooks")[0].selectedIndex;
//    var chaptertmp= $("#slNumber")[0].selectedIndex;
//    alert (booktmp);
//    $(selector).html($(selector).html());
//    	$("#slNumber").change(function(){
//		GetChapterText();
//	});
//	$("#slBooks").change(function(){
//		updateChaptersinDropDown();
//	});
//	$("#chkFormation").click(function(){
//		GetChapterText();
//	})
//	
//	$("#slBooks").selectedIndex=booktmp;
//    $("#slNumber").selectedIndex=chaptertmp;
//}



