$(document).ready(function() {

    // Show/hide default form field text
    $("input[title]").hint();

    // The main search form has very light default text
    $("#headerquery").focus(function() {
        $(this).css("color", "black");
    });

    // Zebra striping
    $("table.zebra").each(function() {
        $(this).find("tr:odd").addClass("striped");
    });

    // Make files and external links open in a new window
    $("#content a[href*=.pdf], #content a[href*=.doc], #content a[href*=.docx], #content a[href*=.ppt], #content a[href*=.pptx], #content a[href*=.xls], #content a[href*=.xlsx], #content a[href*=.xlt], #content a[rel~=external]").click(function() {
        window.open(this.href);
        return false;
    });
    $("#content a[href*=.pdf]").attr("title", "View PDF file");
    $("#content a[href*=.doc], #content a[href*=.docx]").attr("title", "View Microsoft Word document");
    $("#content a[href*=.ppt], #content a[href*=.pptx]").attr("title", "View Microsoft PowerPoint presentation");
    $("#content a[href*=.xls], #content a[href*=.xlsx], #content a[href*=.xlt]").attr("title", "View Microsoft Excel spreadsheet");
    $("#content a[rel~=external], #content a[target=_blank]").attr("title", "This link opens in a new window");

    // Setting the dropdown widths manually is not maintainable if the links are changed — the widths would have to be recalculated.
    // We instead calculate the dropdown widths on the fly.
    // To do that we have to find the widest navigation link in the dropdown lists because the lists themselves have no implicit width because they are floated left
    $("#mega_personal, #mega_business, #mega_wealth, #mega_about").each(function() {
        $(this).addClass("unhidden");       // We have to briefly un-hide the dropdowns so that their list items can be rendered by the browser to determine their widths
        var widest = 0;
        $(this).find("ul.mega li").each(function() {    // Find widest list item
            if($(this).width() > widest) {
                widest = $(this).width();
            }
        });
        $(this).width(widest + $(this).find("div.heading").width() + 55);   // the width of the widest list item + 120px for the right column is always 54 pixels narrower than it needs to be. I think the 4 or 5 is from the borders, but the 50 is a mystery. This is a magic number but it works.
        $(this).removeClass("unhidden");    // Hide the dropdown again
    });

    // We also have to make the lists' heights extend to the bottom of their dropdowns to make the white background extend to the bottom, too
    $("#mega_personal, #mega_personal ul.mega").equalHeights();
    $("#mega_business, #mega_business ul.mega").equalHeights();
    $("#mega_wealth, #mega_wealth ul.mega").equalHeights();
    $("#mega_about, #mega_about ul.mega").equalHeights();

    // Then we have to add 6px to the dropdown height so that the white and green border look is maintained at the bottom
    $("#mega_personal").height($("#mega_personal").height() + 6);
    $("#mega_business").height($("#mega_business").height() + 6);
    $("#mega_wealth").height($("#mega_wealth").height() + 6);
    $("#mega_about").height($("#mega_about").height() + 6);

    // There's a 1px border between the large footer's first and second columns, so we make the column heights equal to ensure the border fills the vertical height
    $("#footer1 .col0, #footer1 .col1, #footer1 .col2").equalHeights();

    // Homepage slideshow
    $("ul#slideshow").before('<div id="slideshow_nav">')
    .cycle({
        timeout: 10000,
        speed: 400,
        random: true,
        pager: "#slideshow_nav",
        cleartypeNoBg: true
    });

    // Pause the slideshow when a thumbnail is clicked
    $("#slideshow_nav a").click(function() {
        $("ul#slideshow").cycle('pause');
    });

    // Homepage boxes need to be the same height
    $("#box1, #box2").each(function() {
        $(this).height($(this).height() + 42);    // the boxes' heights don't take the buttons into account because the buttons are positioned absolutely
    }).equalHeights();



    $("div.gallery_full").addClass("clearfix");
    $("div.gallery_cluster:first").css("width", "660px");
    $("div.gallery_cluster:first .gallery_img a img.gallery_overlay").css("left", "-39px");
    $("img.gallery_overlay").css("visibility", "hidden").attr("src", "/images/play.png").css("visibility", "visible");


    // IE6 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
        // add :hover pseudo-class support to certain things
        $("#topnav li").hover(function() {
            $(this).addClass("ie6_hovering");
        }, function() {
            $(this).removeClass("ie6_hovering");
        });

        // IE doesn't support :last-child
        $("#topnav div.mega div.container ul.mega > li:last-child").addClass("last_child");

        // IE6 doesn't support adjacent sibling selector
        $("h3 + h3").css("margin-top", "15px");

        // IE6 doesn't need the extra 6px in the dropdown heights
        $("#mega_personal").height($("#mega_personal").height() - 6);
        $("#mega_business").height($("#mega_business").height() - 6);
        $("#mega_wealth").height($("#mega_wealth").height() - 6);
        $("#mega_about").height($("#mega_about").height() - 6);

        // Generate PDF icons next to links to PDF files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.pdf]").css("padding-right", "25px").append('<img style="position: absolute; width: 18px; height: 18px; padding-left: 3px;" src="/images/icon_pdf_small.png" alt="PDF file" />');

        // Generate Word icons next to links to DOC files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.doc], #content a[href*=.docx]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 1px; padding-left: 4px;" src="/images/icon_word_small.png" alt="Microsot Word document" />');

        // Generate PowerPoint icons next to links to PPT files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.ppt], #content a[href*=.pptx]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 1px; padding-left: 4px;" src="/images/icon_powerpoint_small.png" alt="Microsoft PowerPoint presentation" />');

        // Generate Excel icons next to links to XLS files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.xls], #content a[href*=.xlsx], #content a[href*=.xlt]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 1px; padding-left: 4px;" src="/images/icon_excel_small.png" alt="Microsoft Excel spreadsheet" />');

        // Generate external link icons next to links with target="_blank" or rel="external"
        $("#content a[rel~=external], #content a[target=_blank]").css("padding-right", "15px").append('<img style="position: absolute; width: 10px; height: 10px; padding: 4px 0 0 5px;" src="/images/icon_external_link_small.png" alt="External link" />');
        $("#content #slideshow_container a.button img, #content #sidebar a.button img").css("display", "none"); // don't show the external link icon on the homepage slideshow or on the loan application popup window link
        //$("#content .pressRelease span.body a").css("padding-right", "0");  // don't show the blue arrows in a press release synopsis
        //$("#content .pressRelease span.body a img").css("display", "none");
    }

    // IE7 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) {
        // IE doesn't support :last-child
        $("#topnav div.mega div.container ul.mega > li:last-child").addClass("last_child");

        // Generate PDF icons next to links to PDF files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.pdf]").css("padding-right", "25px").append('<img style="position: absolute; width: 18px; height: 18px; padding-left: 3px;" src="/images/icon_pdf_small.png" alt="PDF file" />');

        // Generate Word icons next to links to DOC files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.doc], #content a[href*=.docx]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 3px; padding-left: 4px;" src="/images/icon_word_small.png" alt="Microsot Word document" />');

        // Generate PowerPoint icons next to links to PPT files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.ppt], #content a[href*=.pptx]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 3px; padding-left: 4px;" src="/images/icon_powerpoint_small.png" alt="Microsoft PowerPoint presentation" />');

        // Generate Excel icons next to links to XLS files (:after and :content CSS pseudo-classes handle this in IE8 and other browsers)
        $("#content a[href*=.xls], #content a[href*=.xlsx], #content a[href*=.xlt]").css("padding-right", "25px").append('<img style="position: absolute; width: 16px; height: 16px; padding-top: 3px; padding-left: 4px;" src="/images/icon_excel_small.png" alt="Microsoft Excel spreadsheet" />');

        // Generate external link icons next to links with target="_blank" or rel="external"
        $("#content a[rel~=external], #content a[target=_blank]").css("padding-right", "15px").append('<img style="position: absolute; width: 10px; height: 10px; padding: 4px 0 0 5px;" src="/images/icon_external_link_small.png" alt="External link" />');
        $("#content #slideshow_container a.button img, #content #sidebar a.button img").css("display", "none"); // don't show the external link icon on the homepage slideshow or on the loan application popup window link
        //$("#content .pressRelease span.body a").css("padding-right", "0");  // don't show the blue arrows in a press release synopsis
        //$("#content .pressRelease span.body a img").css("display", "none");
    }

    // IE8 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 8) {
        // IE doesn't support :last-child
        $("#topnav div.mega div.container ul.mega > li:last-child").addClass("last_child");
    }
});

