﻿$(document).ready(function() {
    $('.addnotes').click(function() {
        var css_id = $(this).parents('div:eq(0)').attr('id');
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/GetNotes",
            data: "{'ein':'" + css_id + "', 'brformat':'" + false + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#notes" + css_id).html("<textarea class='notesbox' id='text" + css_id + "' >" +
                msg.d + "</textarea><p class='submit'>Submit Changes</p>");
            },
            error: function(msg) {
            }
        });
    });
});
$(document).ready(function() {
$('.addgroup').click(function() {
    var thisclick = $(this).attr('id');
    //$("#" + thisclick).append("<ul id=\"groupmenu\" ><li>Bozo</li><li>Gretchen</li><li>Hannah</li></ul>");
    
    /*
        var css_id = $(this).parents('div:eq(0)').attr('id');
        var thisclick = $(this).attr('id');
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/AddGroup",
            data: "{'ein':'" + css_id + "', 'group':'" + "George" + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#" + thisclick).html("group: " + msg.d);
            },
            error: function(msg) {
            }
        });*/
    });
});
$('.submit')
    .live('click', function() {
        var css_id = $(this).parents('div:eq(0)').attr('id');
        var notes = $("textarea#text" + css_id).val();
        notes = notes.replace(/(['|\\])/g, "\\$1");
        $(this).html("<img src=\"../App_Themes/images/ajax-loader.gif\" />")
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/MakeNotes",
            data: "{'ein':'" + css_id + "', 'notes':'" + notes + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#notes" + css_id).html("<hr />" + msg.d);
            },
            error: function(msg) {
            }
        });
    });
$(document).ready(function() {
    $('.notes').click(function() {
        var css_id = $(this).parents('div:eq(0)').attr('id');
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/GetNotes",
            data: "{'ein':'" + css_id + "', 'brformat':'" + true + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#notes" + css_id).html("<hr />" + msg.d);
            }
        });
    });
});
$('.removemin')
    .live('click', function() {
        var thisclick = $(this).attr('id');
        var css_id = $(this).parents('div:eq(0)').attr('id');
        $(this).html("<img src=\"../App_Themes/images/ajax-loader.gif\" />")
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/RemoveFromPortfolio",
            data: "{'ein':'" + css_id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function() {
                if ($("#c" + css_id).is(":checked")) {
                    subtractministry(true);
                }
                else {
                    subtractministry(false);
                }
                $("#" + css_id).remove();
                $("#m" + css_id).remove();
                var oddnumber = true; //reorder background color
                $(".minblock").each(function(i) {
                    if (oddnumber) {
                        if ($(this).hasClass("alternat")) {
                            $(this).removeClass("alternat").addClass("alternate");
                        }
                    }
                    else {
                        if ($(this).hasClass("alternate")) {
                            $(this).removeClass("alternate").addClass("alternat");
                        }
                    }
                    oddnumber = !oddnumber;
                });
            },
            error: function() {
                $("#" + thisclick).html("Temporary database error, ministry could not be removed");
            }
        });
    });  
    $('.uncomparedmin')
    .live('click', function() {
        var thisclick = $(this).attr('id');
        var css_id = $(this).parents('div:eq(0)').attr('id');
        $(this).removeClass("uncomparedmin").html("<img src=\"../App_Themes/images/ajax-loader.gif\" />")
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/ToggleComparedMin",
            data: "{'ein':'" + css_id + "', 'add':'true'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                if (msg.d == "Added") {
                    $("#" + thisclick).html("Don't Compare").attr('class', 'comparedmin');
                }
                else {
                    $("#" + thisclick).attr('class', 'uncomparedmin').html("You cannot compare more than 6, remove one and try again");
                }
            },
            error: function() {
                $("#" + thisclick).html("Temporary database error, ministry could not be removed");
        }
    });
});
$('.comparedmin').live('click', function() {
        var thisclick = $(this).attr('id');
        var css_id = $(this).parents('div:eq(0)').attr('id');
        $(this).removeClass("comparedmin").html("<img src=\"../App_Themes/images/ajax-loader.gif\" />")
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/ToggleComparedMin",
            data: "{'ein':'" + css_id + "', 'add':'false'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function() {
                $("#" + thisclick).html("Compare").attr('class', 'uncomparedmin');
            },
            error: function() {
                $("#" + thisclick).html("Temporary database error, ministry could not be removed");
            }
        });
    });
    $('.checkbox').live('click', function() {
        
        var thisclick = $(this).attr('id');
        var css_id = $(this).parents('div:eq(0)').attr('id');
        var checked;
        if ($(this).is(":checked")) {
            checked = true;
        }
        else {
            checked = false;
        }
        //$(this).removeClass("comparedmin").html("<img src=\"../App_Themes/images/ajax-loader.gif\" />")
        $.ajax({
            type: "POST",
            url: "../MinistryWatch.asmx/CompareCheckbox",
            data: "{'ein':'" + css_id + "', 'boxchecked':'" + checked + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                if (msg.d == "Full") {
                    $("#msg" + css_id).html("<span style='color:red'>Full, 6 is max</span>");
                    $("#c" + css_id).get(0).checked = false;
                }
                else if (msg.d == "Removed") {
                    addsubtcompared(false);
                    $("#msg" + css_id).html("Removed");
                }
                else if (msg.d == "Added") {
                    addsubtcompared(true);
                    $("#msg" + css_id).html("Added");
                }
                else {
                    $("#msg" + css_id).html("<span style='color:red'>Not Logged on</span>");
                }
            },
            error: function() {
                $("#msg" + css_id).html("error");
            }
        });
    });
function subtractministry(compared) {
    if (compared == true) {
        $("#nummin").text($("#nummin").text() - 1);
        $("#numcomp").text($("#numcomp").text() - 1);
    }
    else {
        $("#nummin").text($("#nummin").text() - 1);
    }
    toggleCompButton();
};
function addsubtcompared(add) {
    if (add == true) {  //add
        $("#numcomp").text($("#numcomp").text()*1 + 1);
    }
    else { //subtract
        $("#numcomp").text($("#numcomp").text() - 1);
    }
    toggleCompButton();
};

function toggleCompButton() {
    if ($("#numcomp").text() * 1 > 1) {
        $("#comparebutton").fadeIn("slow");
        //$("#comparebutton").html("<br /><a href=\"compareministries.aspx\"><img border=\"0\" src=\"../App_Themes/images/Compare.png\" /></a>").fadeIn("slow");
    }
    else {
        $("#comparebutton").fadeOut("slow");
        //$("#comparebutton").html("");
    }
};