﻿$().ready(function() {
    $("#BscAjaxForms").load("../../MyAjaxPages/Static/BabysittingCoop.htm");
    wire_PageMenuItem();
    wire_ChangeCoop();
});

function wire_PageMenuItem() {
    $("a.pagemenuitem").unbind().click(function() {
        $("#BscCoopSelect").fadeOut("slow");
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        try {
            url += "?BscCoopId=" + $("[@jqid$=\"ddlBscCoop\"]").attr("value");
        }
        catch (err) {
            alert("Invalid dropdown value.");
            url = $(this).attr("href");
        }
        $("#BscAjaxForms").children().remove();
        $("#BscAjaxForms").load(url, function() {
            //Wire loaded forms
            if (title == "bscCoop.aspx") wire_Coop();
            if (title == "bscJoinCoop.aspx") wire_JoinCoop();
            if (title == "bscMasterSchedule.aspx") wire_MasterSchedule();
            if (title == "bscPostHistory.aspx") wire_PostHistory();
            if (title == "bscBulletinBoard.aspx") wire_BulletinBoard();
            if (title == "bscScheduleTool.aspx") wire_ScheduleTool();
            if (title == "bscCoopStatus.aspx") wire_CoopStatus();
            if (title == "bscPointManager.aspx") wire_PointManager();
            if (title == "bscSponsorTool.aspx") wire_SponsorMembers();
            if (title == "bscMemberDirectory.aspx") wire_MembersDirectory();
            $("#BscAjaxForms").fadeIn("slow");
        });
        try {
            var coop = $("[@jqid$=\"ddlBscCoop\"] option:selected").text();
            $("#ChangeCoop").html("<em>Current Coop: " + coop + "</em>").fadeIn("slow");
        }
        catch (err) {}
        return false;
    });
};

function wire_CoopStatus() {
    $("[@value=\"Revoke\"]").unbind().click(function() {
        var postData = "BscCoopId=" + $(this).attr("id");
        $.post("../../MyHandlers/RevokeBscMembership.ashx", postData, function(response) {
            if (response != "ok") {
                alert(response);
            }
            else {
                window.location = "../../MyPages/Public/BabysittingCoop.aspx";
            }
        });
    });
};

function wire_ChangeCoop() {
    $("#ChangeCoop").unbind().click(function() {
        $(this).fadeOut("slow");
        $("#BscCoopSelect").fadeIn("slow");
        $("#BscAjaxForms").fadeOut("slow");
        return false;
    });
};

function wire_Coop() {
    $("#btnUpdateCoop").unbind().click(function() {
        try {
            var newCoop = false;
            if ($("$hdnBscCoopId").attr("value") == '00000000-0000-0000-0000-000000000000')
                newCoop = true;
            var postData = $("#frmUpdateCoop").serialize();
            $.post("../../MyHandlers/UpdateCoop.ashx?", postData, function(response) {
                if (response != "ok") alert(response);
                if (newCoop) window.location = "../../MyPages/Public/BabysittingCoop.aspx";
            });
        }
        catch (err) {
            alert(response);
        }
        return false;
    });
    $("#btnClearUpdateCoop").unbind().click(function() {
        $("#BscAjaxForms").load("../../MyAjaxPages/Dynamic/bscCoop.aspx", function() {
            wire_Coop();
        });
        return false;
    });
};

function wire_JoinCoop() {
    $(".bsccoopitem").unbind().click(function() {
        try {
            $("#spnJoinCoop").remove();
            var html = "<span id=\"spnJoinCoop\">"
            html += "<input id=\"btnJoinCoop\" type=\"submit\" class=\"button\" value=\"Join Now\"";
            html += "BscCoopId=\"" + $(this).attr("id") + "\"/>";
            html += "<input id=\"btnCancelJoinCoop\" type=\"submit\" class=\"button\" value=\"Cancel\"/>";
            html += "</span>";
            $(html).insertAfter(this);
            $("#btnJoinCoop").unbind().click(function() {
                var bscCoopId = $(this).attr("BscCoopId");
                var postData = "BscCoopId=" + bscCoopId;
                $.post("../../MyHandlers/JoinCoop.ashx?", postData, function(response) {
                    if (response != "ok") alert(response); else alert("Your request has been sent.");
                    if (bscCoopId == "a90370a3-daa4-474b-a9c2-5960397b8e5a")
                        window.location = "../../MyPages/Public/BabysittingCoop.aspx";
                    $("#spnJoinCoop").remove();
                });
                return false;
            });
            $("#btnCancelJoinCoop").unbind().click(function() {
                $("#spnJoinCoop").remove();
                return false;
            });
        }
        catch (err) {
            alert(response);
        }
        return false;
    });
};

function wire_MasterSchedule() {
    wire_posteditor();
    wire_postsendemail();
};

function wire_BulletinBoard() {
    wire_posteditor();
    wire_postsendemail();
};

function wire_PostHistory() {
    wire_posteditor();
};

function wire_posteditor() {
    $("a.posteditor").unbind();
    $("a.posteditor").unbind().click(function() {
        var postId = "bscPostId=" + $(this).attr("id");
        var url = "../../MyAjaxPages/Dynamic/bscScheduleTool.aspx?" + postId;
        $("#BscAjaxForms").children().remove();
        $("#BscAjaxForms").load(url, function() {
            wire_ScheduleTool();
            $("#BscAjaxForms").fadeIn("slow");
        });
    });
};

function wire_postsendemail() {
    $("a.postsendemail").unbind();
    $("a.postsendemail").unbind().click(function() {
        var postId = "BscPostId=" + $(this).attr("id");
        var url = "../../MyAjaxPages/Dynamic/Mailer.aspx?" + postId;
        $("#BscAjaxForms").children().remove();
        $("#BscAjaxForms").load(url, function() {
            $("#BscAjaxForms").fadeIn("slow");
        });
    });
};

function wire_ScheduleTool() {
    wire_needinfo();
    $("#btnScheduleTool").unbind().click(function() {
        var postData = $("#frmScheduleTool").serialize();
        $.post("../../MyHandlers/UpdateSchedule.ashx?", postData, function(response) {
            if (response != "ok") {
                alert(response);
            }
            else {
                var url = "../../MyAjaxPages/Dynamic/bscMasterSchedule.aspx?";
                url += "BscCoopId=" + $("#hdnBscCoopId").attr("value");
                $("#BscAjaxForms").load(url, function() {
                    wire_MasterSchedule();
                });
            }
        });
        return false;
    });
};

function wire_PointManager() {
    $("#btnPointManager").unbind().click(function() {
        var postData = $("#frmPointManager").serialize();
        $.post("../../MyHandlers/SendPoints.ashx?", postData, function(response) {
            if (response == "ok")
                refresh_PointLedger();
            else
                alert(response);
        });
        return false;
    });
};

function refresh_PointLedger() {
    var postData = "BscCoopId=" + $("#hdnBscCoopId").attr("value");
    $.post("../../MyHandlers/PointLedger.ashx", postData, function(response) {
        $("#tbodyPointManager").html(response);
    });
    $.post("../../MyHandlers/PointBalance.ashx", postData, function(response) {
        $("#tdBalance").html(response);
    });
};

function wire_SponsorMembers() {
    $("#tbodySponsorTool input.cbx").unbind().click(function() {
        var queryString = "BscMemberShipId=" + $(this).attr("value");
        queryString += "&IsSponsor=" + this.checked;
        $.getJSON("../../MyHandlers/SponsorMember.ashx?", queryString, function(json) {
            var sc = "#sc" + json.BscMembershipId;
            $(sc).html(json.SponsorCount);
        });
    });
};

function wire_MembersDirectory() {

};



