$(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() {
        $("<p></p>").addClass("tooltip").html($(this).attr("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;
    });

    // news updates on home page
    $(".newsupdates input:text").keydown(function(e) {
        if (e.keyCode == 13) // enter key
        {
            $("form").submit(function() {
                return false;
            });
            newform = $("<form action='" + navlead + "investors/EmailAlertConfirm.asp?compid=64426' method='post'></form>");
            $(".newsupdates").append(newform);
            $(".newsupdates fieldset").appendTo(newform);
            newform.submit();
        }
    });
    $(".newsupdates a").click(function() {
        newform = $("<form action='" + navlead + "investors/EmailAlertConfirm.asp?compid=64426' method='post'></form>");
        $(".newsupdates").append(newform);
        $(".newsupdates fieldset").appendTo(newform);
        newform.submit();
    });

    // single image on home page content area
    $("#why-choose .SingleImage1_plcContent2").insertAfter("#why-choose h2");

    // 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());

    // select the parent li in railroad template
    $(".railroad #secondary-nav a.selected").parents("li:last").addClass("selected");

    // remove all links in navigation that are already in the map
    var locationlinks = new Array();
    $(".railroad .locationlinks ul a").each(function(i) {
        locationlinks[i] = $(this).attr("href");
    });
    $(".railroad #secondary-nav li.selected a").each(function() {
        if (jQuery.inArray($(this).attr("href"), locationlinks) > -1)
            $(this).parent().remove();
    });
    // remove any empty ul's
    $(".railroad #secondary-nav ul:empty").remove();

    // move the location links into the navigation
    if ($(".locationlinks ul").children().length > 0)
        $(".railroad #secondary-nav ul a.selected").parent().append($(".locationlinks"));

    // any sub nav over 200px should be constrained
    $(".railroad #secondary-nav li.selected ul ul").each(function() {
        if ($(this).height() > 200) {
            $(this).addClass("tall");
            var container = $('<div class="railroad-container"></div>');
            $(this).wrap(container);
            $('<div class="nav"><img src="' + navlead + 'Themes/GWRR.Themes.Default/images/arrow_up_white_on_blue.png" alt="Scroll up" width="7" height="5" /></div>').insertBefore(this);
            $('<div class="nav"><img src="' + navlead + 'Themes/GWRR.Themes.Default/images/arrow_down_white_on_blue.png" alt="Scroll down" width="7" height="5" /></div>').insertAfter(this);
            scrollDiv(".railroad #secondary-nav li.selected .nav:eq(0)", ".railroad #secondary-nav li.selected ul ul.tall", -35);
            scrollDiv(".railroad #secondary-nav li.selected .nav:eq(1)", ".railroad #secondary-nav li.selected ul ul.tall", 35);
            $(this).jScrollPane({ scrollbarWidth: 0, scrollbarMargin: 0, animateTo: true, animateInterval: 40, animateStep: 3 });
        }
    });

    // any page head styles should be H2's and moved out of the freeform div
    $(".railroad #supporting-content span.PageHead").each(function() {
        $("<h2></h2>").html($(this).html()).insertBefore($(this).parents("div.freeform"));
        $(this).next("br").remove();
        $(this).remove();
    });

    // if there are more than 2 freeforms, it's a railroad template
    if (($(".railroad #supporting-content div.freeform").length > 2) && ($(".railroad #supporting-content div.freeform:has(iframe)").length == 0)) {
        $("div.freeform").show();
        $(".railroad #supporting-content div.freeform:eq(0)").addClass("contacts");
        $(".railroad #supporting-content div.freeform:eq(1)").addClass("region");

        // place a link on the map back to the country's page if there are links in the Nearby railroads box
        if ($(".railroad #supporting-content div.region a").length > 0) {
            // hide links to same page in region box
            $(".railroad #supporting-content div.region a[href^=" + $("#secondary-nav a.selected").attr("href") + "]").next().andSelf().remove();
            // if the last link in the breadcrumb has the same name as the page, we want the 2nd last link
            if ($("#breadcrumb a:last").text() == $("#title h1").text())
                $("<a id='mapbreadcrumb'></a>").text("View full " + $("#breadcrumb a").slice(-2).html() + " map").attr("href", $("#breadcrumb a").slice(-2).attr("href")).appendTo("#body");
            else
                $("<a id='mapbreadcrumb'></a>").text("View full " + $("#breadcrumb a:last").text() + " map").attr("href", $("#breadcrumb a:last").attr("href")).appendTo("#body");
        }
        else {
            // hide the Nearby box
            $(".railroad #supporting-content div.region").prev("h2").hide();
            $(".railroad #supporting-content div.region").hide();
        }
    }

    // if there is text in the last freeform div
    if (($(".railroad #supporting-content div.freeform:last div").children().length > 1) && ($(".railroad #supporting-content div.freeform:has(iframe)").length == 0)) {
        // move last freeform div to the main content section
        $(".railroad #supporting-content div.freeform:last").attr("id", "content-continue").appendTo("#title");
        // add continue below link to last freeform div in supporting content
        $("<a href='#content-continue'>Continue below</a>").addClass("continue").appendTo(".railroad #supporting-content div.freeform:last");
    }
});

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");
    });   
}