$(function() {
    var navlead = $("#logo-link").attr("href");
    fixSecondaryNav();

    // tooltip box for images' alt text
    $("#content, #supporting-content, #homeimage, #why-choose").find("img[alt!=' '][alt!='']").hover(function() {
		var alt = $(this).attr('alt');
		if (alt != undefined) {
			$("<p></p>").addClass("tooltip").html(alt).appendTo("body");
			$(this).attr("alt", ""); // for IE
        }
    }, function() {
        $(this).attr("alt", $("p.tooltip").html()); // for IE
        $("p.tooltip").remove();
    }).mousemove(function(e) {
        var offset = 8;
        $("p.tooltip").css({ top: e.clientY + offset, left: e.clientX + offset });
    });

    // move find railroad to after footer
    $("#find-railroad").appendTo("#body");

    // search box
    $("#search-box input:text").keydown(function(e) {
        if (e.keyCode == 13) // enter key
        {
            $("form").submit(function() {
                return false;
            });
            // logo-link will give us navlead
            window.location.href = $("#logo-link").attr("href") + 'search/-' + escape($(this).val());
            return false;
        }
    });
    $("#search-box a").click(function() {
        $("form").submit(function() {
            return false;
        });
        // logo-link will give us navlead
        window.location.href = $("#logo-link").attr("href") + 'search/-' + escape($("#search-box input:text").val());
        return false;
    });

    // find railroad dropdown
    $("#find-railroad .railroad-container .railroads").jScrollPane({ scrollbarWidth: 0, scrollbarMargin: 0, animateTo: true, animateInterval: 40, animateStep: 3 });
    // scroll with up arrow
    scrollDiv("#find-railroad .nav:eq(0)", "#find-railroad .railroads", -35);
    // scroll with down arrow
    scrollDiv("#find-railroad .nav:eq(1)", "#find-railroad .railroads", 35);

    if ($.browser.msie && ($.browser.version == 7.0)) {
        $("#find-railroad .railroad-container").css("visibility", "visible");
        $("#find-railroad .railroad-container").css("display", "none");
        $("#find-railroad").mouseover(function() {
            $("<div id='ie7fix'></div>").mouseover(function() {
                $("#find-railroad .railroad-container").css("display", "none");
                $(this).remove();
            }).prependTo("#header");
            $("#find-railroad .railroad-container").css("display", "block");
        });
    }
    else if ($.browser.msie && ($.browser.version == 6.0)) {
        $("#find-railroad").hover(function() {
            $("#find-railroad .railroad-container").css("visibility", "visible");
        }, function() {
            $("#find-railroad .railroad-container").css("visibility", "hidden");
        });
    }
    else {
        $("#find-railroad .railroad-container").css("visibility", "visible");
        $("#find-railroad .railroad-container").css("display", "none");
        $("#find-railroad").hover(function() {
            $("#find-railroad .railroad-container").css("display", "block");
        }, function() {
            $("#find-railroad .railroad-container").css("display", "none");
        });
    }

    // move the caption on the landing page to the right column
    var $leftCaption = $("#content p.singleimage-caption");
    if ($leftCaption.html() != " ") {
        $leftCaption.insertBefore("#supporting-content p");
    }
    $("#supporting-content p.singleimage-caption:first").addClass("arrow");

    // move the caption on the landing page with photo gallery to the right column
    $("#content div.multiimage div.caption").addClass("arrow").appendTo("#supporting-content").css("marginTop", $("#content div.multiimage div.thumbnails").height());
});

function fixSecondaryNav() {
    var correctnav = $("<div></div>");
    if ($("#secondary-nav a.selected").length == 1) {
        // append the last parent of the selected page to the h2
        $("<h2></h2>").append($("#secondary-nav a.selected").parents("ul:last").find("li:has(a.selected) > a:first").clone()).prependTo(correctnav);
        // if one of the first tier of links is selected, add the child pages to the corrected list
        if ($("#secondary-nav ul:first > li > a.selected").length == 1)
        {
            // if there are no children, hide the nav
            if ($("a.selected").parent().find("ul").length == 0)
                correctnav = null;
            else
                $("a.selected").parent().find("ul").appendTo(correctnav);
        }
        else 
        {
            // add the second to last parent ul to the list
            $("#secondary-nav a.selected").parents("ul:eq(" + ($("#secondary-nav a.selected").parents("ul").length - 2) + ")").appendTo(correctnav);
        }
    }
    // if there is no selected link, just make the first link an h2
    else {
        // append the first page to the h2
        $("<h2></h2>").append($("#secondary-nav ul:first > li > a:first").clone()).prependTo(correctnav);
        $("#secondary-nav ul:first > li:first").find("ul").appendTo(correctnav);
    }
    if (correctnav != null)
        $("#secondary-nav").empty().append(correctnav).css("visibility", "visible");
}

function scrollDiv(trigger, div, verticalpixels) {
    var t;
    $(trigger).hover(function() {
        t = window.setInterval(function() {
            $(div)[0].scrollBy(verticalpixels);
        }, 100);
        $(trigger).addClass("hover");
    }, function() {
        window.clearTimeout(t);
        $(trigger).removeClass("hover");
    });   
}