﻿
var lastBookChapterKey = '';

//----------------------------------------
// When a node in the tree is selected
// we notify the callback objects in their
// respective containers.
//----------------------------------------
function ChapterSelected(s, e) {
    UpdateAllBut('', s.focusedKey);
}

//----------------------------------------------------
// When a tab is selected in a REF, COM or Bible
// we get what was last shown and show that by calling
// the callback object in that tab
//-----------------------------------------------------
function TabChanged(s, e) {

    panelCount = 0; //Star fresh on all loading panels

    var selectedTab = e.tab.index;
    var keyToGet = selectedTab + '|' + lastBookChapterKey;
    if (s.name.indexOf('tabREF') > 0) {
        callbackREFs[selectedTab].PerformCallback(keyToGet);
    }

    if (s.name.indexOf('tabCOM') > 0) {
        callbackCOMs[selectedTab].PerformCallback(keyToGet);
    }

    if (s.name.indexOf('tabBibles') > 0) {
        callbackBibles[selectedTab].PerformCallback(keyToGet);
    }
}

//---------------------------------------------
// When the OLD and NEW tabs as clicked
// we update the display to what is current
// in the chapter selection
//--------------------------------------------- 
function NavigationTabChanged(s, e) {
    if (e.tab.index == 1) Full(treeNew.GetFocusedNodeKey());
    if (e.tab.index == 0) Full(treeOld.GetFocusedNodeKey());
}

//-------------------------------------------
// Intercept the link clicks and process them
//-------------------------------------------

//Update all tabs unless a specific COM or REF link
function Full(link) {

    panelCount = 0; //Star fresh on all loading panels

    var isCOM = link.indexOf('COM|') == 0; //This is a COM link only
    var isREF = link.indexOf('REF|') == 0; //This is a REF link only
    var isBible = link.indexOf('|') == -1;

    //Update the Main Texts
    if (isBible) {
        UpdateAllBut('', link);
    }
    else if (isCOM) {
        var currentCOMTab = tabCOM.activeTabIndex;
        callbackCOMs[currentCOMTab].PerformCallback(link);
    }
    else if (isREF) {
        var currentREFTab = tabREF.activeTabIndex;
        callbackREFs[currentREFTab].PerformCallback(link);
    }
}

//Updates all the but tab listed
function UpdateAllBut(id, bookChapterKey) {
    panelCount = 0; //Start fresh on all loading panels

    //Remember the last chosen entry
    lastBookChapterKey = bookChapterKey;

    //RESOURCEID:book:chapter in zero based format
    //The active RESOURCEID tabs are the same one recorded in CoreInfoCollections
    var bibleTab = tabBibles.activeTabIndex;
    var comTab = tabCOM.activeTabIndex;
    var refTab = tabREF.activeTabIndex;

    //RESOURCEID:book:chapter in zero based format
    var bibleBookChapterKey = bibleTab + '|' + lastBookChapterKey;
    var comBookChapterKey = comTab + '|' + lastBookChapterKey;
    var refBookChapterKey = refTab + '|' + lastBookChapterKey;

    //Update the Main Texts
    callbackBibles[bibleTab].PerformCallback(bibleBookChapterKey);
    callBackFootNote.PerformCallback(bibleBookChapterKey);
    if (id != 'COMs') callbackCOMs[comTab].PerformCallback(comBookChapterKey);
    if (id != 'REFs') callbackREFs[refTab].PerformCallback(refBookChapterKey);
    if (id != 'CROSS') callBackCrossReference.PerformCallback(bibleBookChapterKey);
}

function ShowAdvancedSearch(s, e) {
    popSearch.Show();
}

function ShowPopUpCDAdvert() {
    if (getCookie('HasShownCDAd') == false) {
        setCookie('HasShownCDAd', true);
        popAdCD.Show();

    }
}


//------------------------------------
// Cookie Support
//------------------------------------

function setCookie(c_name, value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 100);
    document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toUTCString();
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function getIfCookieExists(c_name) {
    x = getCookie(c_name);
    if (x != null && x != "") {
        return true;
    }
    return false;

}


//------------------------------------
// Unified Call Back Panel for tabs
//------------------------------------
var panelCount = 0;
function BeginCallBack(s, e) {
    RotateBanners();
    panelCount = panelCount + 1;
    pnlLoading.Show();
    setTimeout('panelCount=0; HideLoadingPanel;', 6000);  //In case the AJAX fails, hide the 'loading' after 6 seconds
}

function EndCallBack(s, e) {
    HideLoadingPanel();
    //ScrollToFindsAfterAjax(s);
    //InjectAds(s);
}

function HideLoadingPanel() {
    if (panelCount < 2) {
        pnlLoading.Hide();
        panelCount = 0;
    }
    panelCount = panelCount - 1;
}

function InjectAds(s) {

    //    var google = '<script type="text/javascript"><!--\n';
    //    google += 'google_ad_client = "pub-0574756881927996";\n';
    //    google += '/* 468x15, Link Unit */\n';
    //    google += 'google_ad_slot = "9025302205";\n';
    //    google += 'google_ad_width = 468;\n';
    //    google += 'google_ad_height = 15;\n';
    //    google += '//-->\n';
    //    google += '</script>\n';
    //    google += '<script type="text/javascript"\n';
    //    google += 'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">\n';
    //    google += '</script>';
    //    var divContent = s.GetMainElement();
    //    var id = divContent.id;

    //    var adDIV = $('#' + id + ' #AdHere');
    //    if (adDIV != null) {
    //        var size = adDIV.size()
    //        if (size > 0) {
    //            for (var i = 0; i < size; i++) {
    //                adDIV[0].innerHTML = '<scr' + 'ipt>alert("999");</sc' + 'ript>';
    //              }
    //        }
    //    }
}


function ScrollToFindsAfterAjax(s) {
    // var divContent = s.GetMainElement();
    // var id = divContent.id;

    // var theAnchorToScrollTo = $('#' + id + ' a[name=theFind]');
    // if (theAnchorToScrollTo != null) {
    //     var size = theAnchorToScrollTo.size()
    //     if (size > 0)
    //         theAnchorToScrollTo[0].scrollIntoView(true);
    // }
}

//-------------------------------
// Search Helper
//-------------------------------

function DoSearch() {
    panelCount = 0; //Start fresh on all loading panels
    HideLoadingPanel();
    RotateBanners();

    //Set the Search Tab Sctive
    var query = txtSearch.GetText();
    var searchTab = tabAdendum.GetTab(2);
    tabAdendum.SetActiveTab(searchTab);

    //Do the search on the server
    //BibleID|Query|SearchScope|SearchKind|TotalRecords|Book List (comma)
    var bibleTab = tabBibles.activeTabIndex;
    var q = bibleTab + '|' + query + '|' + cboSearchScope.GetText() + '|' + cboSearchKind.GetText() + '|' + cboTotalRecords.GetValue() + '|';

    callBackSearch.PerformCallback(q);
}

function DoAdvancedSearch() {
    panelCount = 0; //Start fresh on all loading panels
    popSearch.Hide();
    HideLoadingPanel();
    RotateBanners();

    //Set the Search Tab Sctive
    var query = popTxtSearch.GetText();
    var searchTab = tabAdendum.GetTab(2);
    tabAdendum.SetActiveTab(searchTab);

    //Get the books selected . These are INPUT tags with IDs ending in _I
    //var list = $("input[id$=_I]:checked");
    var list = $('input[id^=search_popSearch_chk]:checked').not('input[id$=_S]');
    var size = list.size();

    var listOfBooksToSearch = "";
    if (size > 0) {
        for (x = 0; x < size; x++) {
            listOfBooksToSearch = listOfBooksToSearch + list[x].id.match(/\d+/) + ',';
        }
    }

    //Do the search on the server
    //BibleID|Query|SearchScope|SearchKind|TotalRecords
    var bibleTab = tabBibles.activeTabIndex;
    var q = bibleTab + '|' + query + '|' + cboAdvancedSearchScope.GetText() + '|' + cboAdvancedSearchKind.GetText() + '|' + cboAdvancedTotalRecords.GetValue() + '|' + listOfBooksToSearch;

    callBackSearch.PerformCallback(q);

}

function AdvancedSearchChecker(s, e) {
    var booksToCheck = s.GetText();

    var listOfCheckBoxes = $('input[id^=search_popSearch_chk]').not('input[id$=_S]');

    var size = listOfCheckBoxes.size();
    for (x = 0; x < size; x++) {
        listOfCheckBoxes[x].checked = false;
    }

    if (booksToCheck == 'All') {
        for (x = 0; x < size; x++) {
            listOfCheckBoxes[x].checked = true;
        }
    }

    if (booksToCheck == 'Old') {
        for (a = 0; a < 39; a++) {
            listOfCheckBoxes[a].checked = true;
        }
    }

    if (booksToCheck == 'New') {
        for (b = 39; b < 66; b++) {
            listOfCheckBoxes[b].checked = true;
        }
    }

    if (booksToCheck == 'Gospels') {
        for (c = 39; c < 43; c++) {
            listOfCheckBoxes[c].checked = true;
        }
    }

    if (booksToCheck == 'Pentateuch') {
        for (d = 0; d < 5; d++) {
            listOfCheckBoxes[d].checked = true;
        }
    }
}

function txtSearch_KeyPress(s, e) {
    if (e.htmlEvent.keyCode == 13) {
        DoSearch();
    }
    return false;
}

function txtAdvancedSearch_KeyPress(s, e) {
    if (e.htmlEvent.keyCode == 13) {
        DoAdvancedSearch();
    }
    return false;
}

function SubmitBlocker(event) {
    var keyCode = 0;

    //Internet Explorer
    if (window.event) {
        keyCode = window.event.keyCode;
        if (keyCode == 13) {
            event.returnValue = false;
            event.cancel = true;
            if (event.srcElement.id.match(/popTxt/))
                DoAdvancedSearch();
            else
                DoSearch();
            return false;
        }
    }
    //Other
    else if (event.which) {
        keyCode = event.which;
        if (keyCode == 13) {
            event.stopPropagation();
            if (event.srcElement.id.match(/popTxt/))
                DoAdvancedSearch();
            else
                DoSearch();
            return false;
        }
    }
    return true;
}

//----------------------------
// Gospel Comparison Tree/callback
//----------------------------

function OnGospel(s, e) {
    //Callback the panel holding the gospel tabs
    //Payload BibleID|GospelStoryID

    var payLoad = tabBibles.activeTabIndex + '|' + s.focusedKey;
    callBackGospelCompare.PerformCallback(payLoad);
}

function onGospelCallBackDone(s, e) {
    var myWidth = 0, myHeight = 0;
    myWidth = getWindowsWidth();
    myHeight = getWindowsHeight();

    var newHeight = (myHeight - 180) + 'px';
    $('[id$=divMatthew]:first').css('height', newHeight);
    $('[id$=divMark]:first').css('height', newHeight);
    $('[id$=divLuke]:first').css('height', newHeight);
    $('[id$=divJohn]:first').css('height', newHeight);

}

//---------------------------------
// Commands in the tabs
//---------------------------------

function SyncAllTabsWithLastCalledBibleReference() {
    Full(lastBookChapterKey);
}

//---------------------------------
// RotateAddBanners
//---------------------------------
var adRotateCount = 0;
function RotateBanners() {
    if (adRotateCount > 4) {

        var adLeftNav = $('#divAdVerticalHolder')[0];
        var oldHTML = adLeftNav.innerHTML;
        adLeftNav.innerHTML = "&nbsp;";
        adLeftNav.innerHTML = oldHTML;

        var adTop = $('#divTopAds')[0];
        oldHTML = adTop.innerHTML;
        adTop.innerHTML = "&nbsp;";
        adTop.innerHTML = oldHTML;


        adRotateCount = -1;
    }
    adRotateCount = adRotateCount + 1;
}

//---------------------------------
// Menu Commands
//---------------------------------

function GospelCompare() {

    var myWidth = 0, myHeight = 0;
    myWidth = getWindowsWidth();
    myHeight = getWindowsHeight();

    popGospelCompare.SetSize(myWidth - 50, myHeight - 50);

    var divHeight = (myHeight - 180) + 'px';
    var treeHeight = (myHeight - 110) + 'px';

    $('[id$=divGospelTree]:first').css('height', treeHeight);
    $('[id$=divGospelTabs]:first').css('height', treeHeight);
    $('[id$=divMatthew]:first').css('height', divHeight);
    $('[id$=divMark]:first').css('height', divHeight);
    $('[id$=divLuke]:first').css('height', divHeight);
    $('[id$=divJohn]:first').css('height', divHeight);
    popGospelCompare.ShowAtPos(25, 25);

    //Load the data dynamically
    var payLoad = tabBibles.activeTabIndex + '|' + treeGospelCompare.GetFocusedNodeKey();
    callBackGospelCompare.PerformCallback(payLoad);
}

function menuColors(s, e) {
    //We control the URL here so that the Page_OnInit gets the command. 
    var theme = e.item.name;
    window.location.href = window.location.pathname + '?theme=' + theme;
}

//----------------------------------
// Display Helpers
//-----------------------------------

function getWindowsWidth() {
    var w = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        w = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        w = document.body.clientWidth;
    }
    return w;
}

function getWindowsHeight() {
    var h = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        h = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        h = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        h = document.body.clientHeight;
    }
    return h;
}



//--------------------------------
// Page Load starts off the GETs
//--------------------------------       

function pageLoad() {
    Full(treeOld.GetFocusedNodeKey());
    setInterval("RotateBanners()", 240000);
    ShowPopUpCDAdvert();

}



