﻿$(document).ready(function() {
    /*Left module toggles that remember*/
    toggleStates = $.cookie('ETBToggles');
    toggleTargets = Array("#toggle1-target", "#toggle2-target", "#toggle3-target");
    if (!toggleStates) {
        $.cookie('ETBToggles', '0,0,0');
        toggleStates = '0,0,0';
    }
    toggleSets = toggleStates.split(',');
    for (y = 0; y < toggleTargets.length; y++) {
        if (toggleSets[y] < 1) { $(toggleTargets[y]).hide(0); }
    }
    $('#toggle1').click(function() { cookieToggle(0) });
    $('#toggle2').click(function() { cookieToggle(1) });
    $('#toggle3').click(function() { cookieToggle(2); });

    $('#functions-menu td.heart a').click(function() {
        if ($('#functions-menu td.heart a').hasClass('notUser')) {
            var divId = "myTBInline";
            message = "<div style='text-align:center'><p><strong>Add to My Books & Notes</strong><br/>is only available for<br/>registered Enter the Bible users.<br/></br></p>"
            message += "<strong><a href='/Bible.aspx?view=login'>Login</a> or <a href='/Bible.aspx?view=login'>Create an Account</a>";
            var divM = $("#" + divId);
            if (divM.length > 0) { divM.empty(); }
            divM.append(message);
            tb_show("Login Required", "#TB_inline?height=85&width=205&inlineId=" + divId, null);
        } else {
            checkToggle = toggleSets[0];
            if (checkToggle < 1) { cookieToggle(0); }
            loadNoteModule(ETBResourceID, "add");
        }
        return false;
    });
    $('#functions-menu td.print a').click(function() {
        window.print();
        return false;
    });
});

function cookieToggle(tabNum) {
    targetToToggle = "#toggle" + (tabNum + 1) + "-target";
    $(targetToToggle).slideToggle(500);
    if (toggleSets[tabNum] == 1) { toggleSets[tabNum] = 0 } else { toggleSets[tabNum] = 1 }
    $.cookie('ETBToggles', toggleSets.join());
}

function loadBookModule() {
    theURL = "/assets/ModBookList.aspx?sort=" + ($("#SSelect").val()) + "&testament=" + ($("#TSelect").val());
    $("#bookSort").load(theURL);
}

function loadNoteModule(rid, dowhat) {
    theURL = "/assets/ModNoteView.aspx?rid=" + rid;
    if (dowhat == "delete") {
        message = "Do you really want to delete <br/>this entire link and note?";
        theAction = "loadNoteModule(" + rid + ",'deletenow');tb_remove();";
        showMessage("Are you sure?", message, 75, 200, theAction);
    } else if (dowhat) {
        theURL = theURL + "&task=" + dowhat;
    }
    $("#myNotes").load(theURL);
    if (dowhat == "add" ) { $("#myNotes").load(theURL); }
}

function ClearRecent() {
    theAction = "window.location.replace('/Bible.aspx?view=user&task=recent&action=delete'); ";
    message = "Do you really want to delete <br/>ALL your notes?";
    showMessage("Are you sure?", message, 75, 200, theAction);
}

function DeletePath(Sid) {
    theAction = "window.location.replace('/Bible.aspx?view=user&task=study&action=delete&sid="+Sid+"'); ";
    message = "Do you really want to delete<br/>this study path?";
    showMessage("Are you sure?", message, 80, 200, theAction);
}

function DeleteProgress(Sid) {
    theAction = "window.location.replace('/Bible.aspx?view=user&task=study&action=clear&sid=" + Sid + "'); ";
    message = "Do you really want to clear<br/>your Study Path progress?";
    showMessage("Are you sure?", message, 80, 200, theAction);
}

function ClearNotes(Rid, RName) {
    if (RName) {
        message = "Do you really want to delete the resource<br/>" + RName + "?";
    } else {
        message = "Do you really want to delete <br/>ALL your recent history?";
    }
        theAction = "window.location.replace('/Bible.aspx?view=user&task=notes&action=delete&taskid="+Rid+"'); ";
    showMessage("Are you sure?", message, 80, 220, theAction);
}

function showMessage(title, message, height, width, theAction) {
    var divId = "myTBInline";
    message = "<div style='text-align:center'><p><strong>" + message + "</strong></p>"
    message += "<input id='DeleteSure' type='button' value='Delete Now' onclick=\"" + theAction + "\" > ";
    message += "<input id='CancelNow' type='button' value='Cancel' onclick='tb_remove();'>";
    var divM = $("#" + divId);
    if (divM.length > 0) { divM.empty(); } 
    divM.append(message);
    tb_show(title, "#TB_inline?height=" + height + "&width=" + width + "&inlineId=" + divId, null);
}

function postNote(rid) {
    var dataString = 'newnote=' + escape($("#NoteTextArea").val()) + "&rid=" + rid;
    $.ajax({
        type: "POST",
        url: "assets/ModNoteView.aspx",
        data: dataString,
        success: function(msg) {
            loadNoteModule(rid);
        }
    });
    return false;
}