// dom is ready, let's go...
$(document).ready(function () {

    // tabs
    $("body.Home #tabs").tabs().find("*").andSelf().removeClass("ui-helper-reset ui-widget ui-widget-content ui-corner-all ui-corner-top ui-corner-bottom ui-tabs-panel");

    // expand/collapse more content
    $("a.more-toggle").html("Expand for more").next().hide();
    $("a.more-toggle-link").append('<span class="more-toggle">Expand for more</span>').next().hide();
    $("a.more-toggle, a.more-toggle-link").click(function () {
        $(this).find("*").andSelf().filter(".more-toggle").toggleClass("more-expanded").html(function (i, h) {
            if (h == "Expand for more") return "Hide";
            else return "Expand for more";
        });
        if ($(this).next().is(":hidden")) $(this).next().slideDown(200);
        else $(this).next().slideUp(200);
        return false;
    });

    // footnote links
    $("#top")
		.append("<h2 class=\"printOnly\">Notes (for printed copy)</h2>")
		.append("<ol id=\"footnotes\" class=\"printOnly\"></ol>");
    footnote = 1;
    $("div#contentmiddle a[href],div#contentright a[href]").not(".more-toggle, .more-toggle-link, .ui-tabs-nav a").each(function () {
        $(this).after("<sup class=\"printOnly\">" + footnote + "</sup>");
        link = $(this).attr("href");
        if (link.indexOf("#") == 0) {
            link = document.location.href + link; // anchor link on current page
        } else if (link.indexOf("mailto:") >= 0) {
            link = link.substring(7); // email address			
        } else if ((link.indexOf("http") && link.indexOf("https")) < 0) {
            if (link.indexOf("/") == 0) {
                link = "http://" + document.domain + link; // root-relative link
            } else {
                link = "http://" + document.domain + "/" + link; // page-relative link	
            }
        }
        $("#footnotes").append("<li>" + link + "</li>");
        footnote++;
    });

    // add striping to tables and highlight a row on rollover
    $("tr")
		.mouseover(function () {
		    $(this).addClass("highlight");
		})
		.mouseout(function () {
		    $(this).removeClass("highlight");
		});
    $("tr:even").addClass("alt");

    // accordian effect
    $(".toggler").addClass("closed");
    $('#accordion').accordion({ header: ".toggler", selectedClass: "open", autoHeight: false });

    // open non-web documents in a new window without browser chrome
    $("a[href$='.doc'],a[href$='.pdf'],a[href$='.xls'],a[href$='.ppt'],a[href$='.dot'],a[href$='.pot']").each(function () {
        $(this).click(function () {
            window.open(
				$(this).attr("href"),
				$(this).text(),
				'height=500,width=700,left=100,top=75,resizable=yes,scrollbars=yes,toolbar=no,status=no'
			);
            return false;
        });
        $(this).after(" <small class=\"noprint\">(opens in a new window)</small>");
    });

    // also warn about "send to a friend" new window
    $('a.addthis_button').after(" <small class=\"noprint\">(opens in a new window)</small>");

    // local scrolling
    //$.localScroll();


});

function hideshow(hide, show) {
    document.getElementById(hide).style.display = "none";
    document.getElementById(show).style.display = "";
}

// page is fully loaded...
$(window).load(function () {
    // image captions
    $("img[class*='imgcaption']").each(function () {
        $div = $(this).wrap("<div></div>").parent();
        $div.addClass("imgcaption").css({ width: $(this).width() });

        var text = "";
        if ($(this).attr("title"))
            text = $(this).attr("title");
        else if ($(this).attr("alt"))
            text = $(this).attr("alt");

        if (text != "")
            $div.append("<p class=\"imgcaptionTXT\" style=\"" + $(this).width() + "px\">" + text + "</p>");

        if ($(this).hasClass("insetRight"))
            $div.addClass("insetRight");

        if ($(this).hasClass("insetLeft"))
            $div.addClass("insetLeft");

        $(this).attr("class", "imgcaptionIMG");
    });
});

$(function () {
    $("ul.subnav>li").each(function (b, a) {
        if (location.href.indexOf($("a", a)[0].href) == -1) {
            $("ul", a).hide();
        }
    });
    var lowIE = false;
    if (jQuery.browser.msie) {
        if (parseInt(jQuery.browser.version) < 7) {
            lowIE = true;
        }
    }
    /*
    // wrap forms with shadows
    if (!lowIE) {
    $("#form-container")
    .before("<img id='top' class='plain' alt='Horizontal separator' src='/_includes/images/form.top.png'/>")
    .after("<img id='bottom' class='plain' alt='Horizontal separator' src='/_includes/images/form.bottom.png'/>")
    .css("margin-bottom", "0");
    }*/

    function updateTotal(total) {
        var gift_total = 0.00;
        if (total != null) {
            gift_total = total;
        } else {
            $("input[name^='GIFT_AMOUNT']:checked").each(function (i) {
                gift_total = (parseFloat($(this).attr("value")) + parseFloat(gift_total)).toFixed(2);
            });
        }
        $("#gift_total").text("$" + gift_total);
    }
    function changeAmt() {
        $("input[name^='GIFT_AMOUNT']")
			.change(function () {
			    updateTotal();
			})
			.click(function () {
			    updateTotal();
			})
			.select(function () {
			    updateTotal();
			})
			.keypress(function () {
			    updateTotal();
			});
    }
    function givingPageConfig() {
        $("#gift-total").prepend("<h3>Gift Total: <span id='gift_total'>$0.00</span></h3>");
        $("label[class*='gift_other_text']").each(function (i, j) {
            $(this).css("display", "none");
        });
        $("input[id^='gift_other_']").each(function (i) {
            $(this).attr({ value: 0.00 });
        });
    }
    function customAmt() {
        $("input[id^='gift_value_']").each(function (i, j) {
            $(j).blur(function (k) {
                if ($(j).attr("value").match(new RegExp(/^([0-9]?){10}\.?([0-9]?){2}$/))) {
                    $("#givingErrorNum" + i).empty();
                    if ($(j).attr("value") != "") {
                        $($("input[id^='gift_other_']")[i]).attr({ checked: 'checked', value: ($(j).attr("value")) });
                    } else {
                        $($("input[id^='gift_other_']")[i]).attr({ value: 0.00 });
                    }
                } else {
                    if (!$("#givingErrorNum" + i).hasClass("error")) {
                        $($("input[id^='gift_other_']")[i]).attr({ value: 0.00 });
                        $(j).after(" <em class='error' id='givingErrorNum" + i + "'>Please enter a valid dollar amount</em>");
                    }
                }
                updateTotal();
            })
        });
    }
    function customAmtRadio() {
        $("input[id^='gift_other_']").each(function (i, j) {
            $(j)
			.change(function (k) {
			    $($("label[class*='gift_other_text']")[i]).show().focus();
			})
			.click(function (k) {
			    $($("label[class*='gift_other_text']")[i]).show().focus();
			})
        });
    }
    function notCustomAmt() {
        $("input[name^='FUND']").each(function (a, b) {
            $("input[name='GIFT_AMOUNT" + (a + 1) + "']:not([id^='gift_other'])").each(function (i, j) {
                $(j).change(function (k) {
                    $($("label[class*='gift_other_text']")[a]).hide();
                });
            });
        });
    }
    function submitCheck() {
        $("form[id='gift_form']").bind("submit", function (e) {
            var check = true;
            $("input[name^='GIFT_AMOUNT']:checked").each(function (i) {
                if ($(this).attr("value").match(new RegExp(/^([0-9]?){10}\.?([0-9]?){2}$/))) {

                } else {
                    check = false;
                }
            });
            $("input[id^='gift_other_']:checked").each(function (i, j) {
                if ($("em[class*='error']").length > 0) {
                    check = false;
                }
            });
            if (!check) {
                return false;
            }
        });
    }

    givingPageConfig();
    customAmt();
    notCustomAmt();
    customAmtRadio();
    changeAmt();
    submitCheck();

    $("input[type='reset']").click(function () {
        updateTotal("0.00");
    });
});

